Qnetworkaccesmanager没有发出“完成”的信息。信号 [英] Qnetworkaccesmanager doesn`t emit "finished" signal

查看:129
本文介绍了Qnetworkaccesmanager没有发出“完成”的信息。信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个发送请求并将数据存储在QString中的方法,但似乎信号永远不会发出,因为程序在等待信号时停止。



你能查看我的代码,看看我是不是错了什么?我用不同的URL(http和https)和2个不同的网络对它进行了测试,这样就不会有问题。



PS:我还应该提到建筑(我使用qmake)我收到警告:



I`ve written a method to send a request and store data in a QString, but it seems that the signal is never emitted because the program stops at the point of waiting for signal.

Can you check my code and see if I`m wrong or something? I`ve tested it with different URLs (http and https) and 2 different networks so that shouldn`t be a problem.

P.S: I should also mention that while building (I use qmake) I get a warning:

warning: ‘manager’ may be used uninitialized in this function [-Wmaybe-uninitialized]
     manager->get (QNetworkRequest (QUrl (url)));





我是什么尝试过:





What I have tried:

void ReqProcessor::getUpdates () //send request
{
    QString url = baseURL;
    QNetworkAccessManager *manager = new QNetworkAccessManager;
    QNetworkReply *reply;
    url.append ("getupdates?timeout=100");
    qDebug() << url; //Debug
    reply = manager->get (QNetworkRequest (QUrl (url)));
    connect (manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished()));
    qDebug() << reply->error(); //debug
    while (!ready)
    {
        continue;
    }
    updates = reply->readAll ();
    qDebug() << updates; //Debug
}







void ReqProcessor::replyFinished ()
{
    qDebug() << "Ready";
    ready = 1;
}

推荐答案

已完成 signal和 replyFinished 插槽有不同的签名。尝试使用与信号相同的签名来定义插槽。类似于:

The finished signal and the replyFinished slot have different signatures. Try to define your slot with the same signature as your signal. Something like:

void ReqProcessor::replyFinished (QNetworkReply* reply)
{
    //...
}


默认Qt 连接类型 AutoConnection 这意味着如果发出的信号来自另一个线程,则当控制返回到当前线程的事件循环时,它的插槽将被排队并调用(如 QueuedConnection )。由于你的线程中有一个阻塞循环,控件永远不会返回到线程的事件循环......


The default Qt connection type is AutoConnection which means that if the emitted signal is from another thread, it's slot is queued and invoked when control returns to the current thread's event loop (like a QueuedConnection). Since you have a blocking loop in your thread, the control is never returns to the thread's event loop...

尝试用 DirectConnection连接你的插槽。类似于:

connect (manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished()), Qt::DirectConnection);


它最终使用QEventLoop工作。这是改进的代码(并删除了该槽,因为它不再是必要的)



It finally got to work using a QEventLoop. here is the improved code (and that slot is removed, since it`s no more neccesary)

void ReqProcessor::getUpdates ()
{
    QString url = baseURL;
    QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    QNetworkReply *reply;
    QEventLoop loop;
    url.append ("getupdates?timeout=100");
    connect (manager, SIGNAL(finished(QNetworkReply*)), &loop, SLOT(quit()), Qt::DirectConnection);
    reply = manager->get (QNetworkRequest (QUrl (url)));
    loop.exec();
    updates = reply->readAll ();
}





还有一个错误,我的程序缺少QApplication或QCoreApplication。



And aother mistake was, my program was lacking a QApplication or QCoreApplication.


这篇关于Qnetworkaccesmanager没有发出“完成”的信息。信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆