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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:13:56  来源:igfitidea点击:

QObject connection function

c++qtqt5qt-signals

提问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

You probably forgot to use the Q_OBJECTmacro. Every class that implements its own slots/signals needs that macro. Don't forget that you need to add your header/source file to the .pro file.

您可能忘记使用Q_OBJECT宏。每个实现自己的插槽/信号的类都需要该宏。不要忘记您需要将头文件/源文件添加到 .pro 文件中。

回答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 sloterror.

除了 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

我根据Zeta的帖子其他帖子在这里做一个简短的总结

To solve “No such slot” error, you must check..

要解决“No such slot” error,您必须检查..

  1. Check if your class inherits QObjector any derived class from QObject
  2. Append Q_OBJECTmacro inside the class definition
  3. Append slotsor Q_SLOTSafter your private/protected/public keyword for your event
  4. 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.
  1. 检查您的类是否QObject从 QObject继承或任何派生类
  2. Q_OBJECT在类定义中附加宏
  3. 在您的活动的私人/受保护/公共关键字之后附加slotsQ_SLOTS之后
  4. 如果您确实检查了 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

希望这能挽救他人的生命