gpt4 book ai didi

c++ - Qt 的新信号/槽语法出现问题 - 连接到一个简单的函数

转载 作者:行者123 更新时间:2023-11-28 01:32:10 54 4
gpt4 key购买 nike

如所述here: ,您可以将信号连接到一个简单的函数,如下所示:

connect(
sender, &Sender::valueChanged,
someFunction
);

这是我的程序:

#include <QCoreApplication>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>



void handleReply(QNetworkReply *reply)
{
QString replyStr = reply->readAll();
}

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

QNetworkAccessManager manager;
connect(&manager, &QNetworkAccessManager::finished(QNetworkReply*), handleReply(QNetworkReply*));

return a.exec();
}

但是我遇到了一些错误:

main.cpp:18: error: call to non-static member function without an object argument
connect(&manager, &QNetworkAccessManager::finished(QNetworkReply*), handleReply(QNetworkReply*));
~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~

main.cpp:18: error: 'QNetworkReply' does not refer to a value
connect(&manager, &QNetworkAccessManager::finished(QNetworkReply*), handleReply(QNetworkReply*));
^
main.cpp:18: error: expected expression
connect(&manager, &QNetworkAccessManager::finished(QNetworkReply*), handleReply(QNetworkReply*));
^

main.cpp:18: error: 'QNetworkReply' does not refer to a value
connect(&manager, &QNetworkAccessManager::finished(QNetworkReply*), handleReply(QNetworkReply*));
^
/usr/include/x86_64-linux-gnu/qt5/QtNetwork/qnetworkreply.h:62: declared here
class Q_NETWORK_EXPORT QNetworkReply: public QIODevice

main.cpp:18: error: expected expression
connect(&manager, &QNetworkAccessManager::finished(QNetworkReply*), handleReply(QNetworkReply*));
^
^

如果有任何帮助,我将不胜感激。谢谢。

最佳答案

当你使用新的连接语法时,没有必要指明参数的类型,所以只需使用:

#include <QCoreApplication>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>

#include <QDebug>

static void handleReply(QNetworkReply *reply)
{
QString replyStr = reply->readAll();
// do stuff
qDebug()<< replyStr;
reply->deleteLater();
}

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

QNetworkAccessManager manager;
QObject::connect(&manager, &QNetworkAccessManager::finished, &handleReply);
manager.get(QNetworkRequest(QUrl("https://www.google.com")));

return a.exec();
}

注意:如果你在一个继承自 QObject 的类中,你应该只使用 connect(...),如果是 main 你应该使用 QObject::connect (...)

关于c++ - Qt 的新信号/槽语法出现问题 - 连接到一个简单的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51052276/

54 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com