問題描述
根據(jù)文檔,插槽的返回值沒有任何意義.
然而在生成的 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?
這是我所談?wù)摰囊粋€例子.這是取自 moc 生成的代碼.'message' 是一個不返回任何內(nèi)容的槽,'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;
推薦答案
返回值僅在您想將插槽作為普通成員函數(shù)調(diào)用時才有用:
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 方法可用于調(diào)用插槽并獲取返回值.雖然看起來做起來有點復(fù)雜.
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.
這篇關(guān)于Qt:插槽返回值的含義?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!