QNetworkAccessManager 正确用法



我想将一些信息发布到php并从该php获取结果。我的帖子应该看起来像:www.sample.com/test.php?un=user&pass=123345我正在尝试所有变体,但没有成功:

m_NetworkAccessMNGR = new QNetworkAccessManager(this);
connect(m_NetworkAccessMNGR, SIGNAL(finished(QNetworkReply*)),
        this,SLOT(slotFinishRequest(QNetworkReply*)));
QNetworkRequest networkRequest;
networkRequest.setUrl(QUrl("http://www.sample.com/test.php?un=user&pass=123345"));
QNetworkReply *reply = m_NetworkAccessMNGR->get(networkRequest);
connect(reply, SIGNAL(readyRead()),
        this, SLOT(slotReadyRead()));
connect(reply, SIGNAL(finished()),
        this, SLOT(slotOnRequestCompleted()));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
        this, SLOT(slotOnRequestError(QNetworkReply::NetworkError)));
connect(reply, SIGNAL(sslErrors(QList<QSslError>)),
         this, SLOT(slotSslErrors(QList<QSslError>)));

代码不会执行任何这些槽。
有没有人对问题是什么有任何想法?
谢谢

你的代码中其他地方是否有事件循环?

如果没有,请尝试以下操作:

QEventLoop loop;
m_NetworkAccessMNGR = new QNetworkAccessManager(this);
QNetworkRequest networkRequest;
networkRequest.setUrl(QUrl("http://www.sample.com/test.php?un=user&pass=123345"));
QNetworkReply *reply = m_NetworkAccessMNGR->get(networkRequest);
connect(reply, SIGNAL(readyRead()),
        this, SLOT(slotReadyRead()));
connect(reply, SIGNAL(finished()),
        this, SLOT(slotOnRequestCompleted()));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
        this, SLOT(slotOnRequestError(QNetworkReply::NetworkError)));
connect(reply, SIGNAL(sslErrors(QList<QSslError>)),
        this, SLOT(slotSslErrors(QList<QSslError>)));
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();

这样效果更好吗?

相关内容

  • 没有找到相关文章

最新更新