C++ QObject连接函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19886878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
QObject connection function
提问by Charlemagne
I checked other similar questions and tried their solutions but they don't work for me.
我检查了其他类似的问题并尝试了他们的解决方案,但它们对我不起作用。
I'm basically trying to make a http client that only makes post requests. In order to do this, I need to connect QNetworkManager
's finished signal to some callback slot.
我基本上是在尝试制作一个仅发出发布请求的 http 客户端。为了做到这一点,我需要将QNetworkManager
的完成信号连接到某个回调槽。
Here's my code.
这是我的代码。
h file:
.h 文件:
...
public slots:
void finishedSlot(QNetworkReply* reply);
private:
QNetworkAccessManager *network_manager;
...
cpp file:
.cpp 文件:
...
Class1::Class1(){
network_manager = new QNetworkAccessManager(this);
QObject::connect(network_manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(finishedSlot(QNetworkReply *)));
}
...
void Class1::finishedSlot(QNetworkReply* reply)
{
// some logic with reply
}
...
As you can see, the slot is definitely present and it is declared under public slots in header file. So I have no idea why this is happening. I already tried clean, run qmake, and rebuild.
如您所见,该插槽肯定存在,并且它在头文件中的公共插槽下声明。所以我不知道为什么会这样。我已经尝试过清理、运行 qmake 和重建。
The error message is:
错误信息是:
"QObject::connect: No such slot QObject::finishedSlot(QNetworkReply *)"
“QObject::connect: No such slot QObject::finishedSlot(QNetworkReply *)”
Any idea?
任何的想法?
回答by Zeta
回答by TheDarkKnight
One thing to note; since you're using Qt 5, there's a new signal slot connection syntax, which will allow you to specify any function and not just those defined as slots.
需要注意的一件事;由于您使用的是 Qt 5,因此有一个新的信号槽连接语法,它允许您指定任何函数,而不仅仅是那些定义为槽的函数。
In this situation you can do this: -
在这种情况下,您可以这样做:-
connect(network_manager, &QNetworkAccessManager::finished, this, &Class1::finishedSlot);
What's great about this syntax is that you just specify the address of the function and don't bother about the parameters, so if you change them in a function, you don't need to update them in the connect statements.
这种语法的优点在于,您只需指定函数的地址,而不必理会参数,因此如果您在函数中更改它们,则无需在连接语句中更新它们。
You still should be using the Q_OBJECT macro though and you can read more about the new syntax here.
您仍然应该使用 Q_OBJECT 宏,您可以在此处阅读有关新语法的更多信息。
回答by AndersonMeng
I share another possible problem here as this post is the top most in google search.
我在这里分享另一个可能的问题,因为这篇文章是谷歌搜索中最热门的。
In addition to add Q_OBJECT
, you must also add public slots:
or public Q_SLOTS:
for your customized event. Otherwise, you'll still encounter the QObject::connect: No such slot
error.
除了 add Q_OBJECT
,您还必须 为您的自定义事件添加public slots:
或public Q_SLOTS:
。否则,您仍然会遇到QObject::connect: No such slot
错误。
I give a brief summary here according to Zeta's postand the other post
To solve “No such slot” error
, you must check..
要解决“No such slot” error
,您必须检查..
- Check if your class inherits
QObject
or any derived class from QObject- Append
Q_OBJECT
macro inside the class definition- Append
slots
orQ_SLOTS
after your private/protected/public keyword for your event- If you do check 1-3, then clean, run qmake, and rebuild again to make sure all your things in 1-3 are generated by moc.
- 检查您的类是否
QObject
从 QObject继承或任何派生类Q_OBJECT
在类定义中附加宏- 在您的活动的私人/受保护/公共关键字之后附加
slots
或Q_SLOTS
之后- 如果您确实检查了 1-3,那么clean,运行 qmake,然后重新构建以确保 1-3 中的所有内容都是由moc生成的。
Finally, a example here:
最后,这里有一个例子:
class MyClass: public QObject { //check 1
Q_OBJECT //check 2
public slots: //check 3
void onEvent(int);
};
Hope this saves others' life
希望这能挽救他人的生命