使用类似http://example.com的URL调用函数CheckSite(),它初始化QNetworkAccessManager对象并连接()插槽和信号。
manger-> get()调用似乎有效(它会生成http流量),但不会在请求结束时调用插槽replyFinished()。
这段代码出了什么问题?
#include <QtCore>
#include <QtNetwork>
class ClientHandler : public QObject
{
Q_OBJECT
QNetworkAccessManager *manager;
private slots:
void replyFinished(QNetworkReply *);
public:
void CheckSite(QString url);
};
void ClientHandler::replyFinished(QNetworkReply *reply) { qDebug() << "DONE"; }
void ClientHandler::CheckSite(QString url) {
QUrl qrl(url);
manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
manager->get(QNetworkRequest(qrl));
}
答案 0 :(得分:1)
无。我把它包起来,因此功能齐全,工作正常:
// placed in client.cpp
#include <QtDebug>
#include <QCoreApplication>
/* YOUR CODE */
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
ClientHandler handler;
handler.CheckSite("www.google.com");
return app.exec();
}
#include "client.moc"
按预期输出“DONE”。也许你正在检查的网站真的没有回来?可能需要authentication或正在制作ssl errors?
答案 1 :(得分:0)
你有什么代码?你在某处旋转事件循环吗?例如qapp.exec()?