Qt:插槽返回值的含义?

时间:2023-01-22
本文介绍了Qt:插槽返回值的含义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

根据文档,插槽的返回值没有任何意义.
然而在生成的 moc 代码中,我看到如果一个插槽返回一个值,这个值用于某些东西.知道它有什么作用吗?

According to the documentation the return value from a slot doesn't mean anything.
Yet in the generated moc code I see that if a slot returns a value this value is used for something. Any idea what does it do?

这是我所谈论的一个例子.这是取自 moc 生成的代码.'message' 是一个不返回任何内容的槽,'selectPart' 被声明为返回 int.

Here's an example of what I'm talking about. this is taken from code generated by moc. 'message' is a slot that doesn't return anything and 'selectPart' is declared as returning int.

case 7: message((*reinterpret_cast< const QString(*)>(_a[1])),(*reinterpret_cast< int(*)>(_a[2]))); break;
case 8: { int _r = selectPart((*reinterpret_cast< AppObject*(*)>(_a[1])),(*reinterpret_cast< int(*)>(_a[2])));
    if (_a[0]) *reinterpret_cast< int*>(_a[0]) = _r; }  break;

推荐答案

返回值仅在您想将插槽作为普通成员函数调用时才有用:

The return value is only useful if you want to call the slot as a normal member function:

class MyClass : public QObject {
    Q_OBJECT
public:
    MyClass(QObject* parent);
    void Something();
public Q_SLOTS:
    int Other();
};

void MyClass::Something() {int res = this->Other();...}

void MyClass::Something() { int res = this->Other(); ... }

似乎这不是可以使用返回值的唯一方法,QMetaObject::invokeMethod 方法可用于调用插槽并获取返回值.虽然看起来做起来有点复杂.

It seems that's not the only way the return value can be used, the QMetaObject::invokeMethod method can be used to call a slot and get a return value. Although it seems like it's a bit more complicated to do.

这篇关于Qt:插槽返回值的含义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:我如何获取网页的内容 下一篇:QObject连接函数

相关文章

最新文章