QObject连接函数

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

问题描述

我检查了其他类似的问题并尝试了他们的解决方案,但它们对我不起作用.

I checked other similar questions and tried their solutions but they don't work for me.

我基本上是在尝试制作一个仅发出发布请求的 http 客户端.为了做到这一点,我需要将 QNetworkManager 的完成信号连接到某个回调槽.

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.

这是我的代码.

h 文件:

...
public slots:
   void finishedSlot(QNetworkReply* reply);
private:
    QNetworkAccessManager *network_manager;
...

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
}
...

如您所见,该插槽肯定存在,并且它是在头文件中的公共插槽下声明的.所以我不知道为什么会这样.我已经尝试过清理、运行 qmake 和重建.

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.

错误信息是:

"QObject::connect: No such slot QObject::finishedSlot(QNetworkReply*)"

"QObject::connect: No such slot QObject::finishedSlot(QNetworkReply *)"

有什么想法吗?

推荐答案

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

You probably forgot to use the Q_OBJECT macro. 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.

这篇关于QObject连接函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:Qt:插槽返回值的含义? 下一篇:为什么 QVector::size 返回 int?

相关文章

最新文章