QObject连接函数

2023-01-22C/C++开发问题
4

本文介绍了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连接函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6