-1

我想将一些信息发布到 php 并从该 php 获取结果。我的 POST 应该如下所示: 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>)));

代码不执行任何这些插槽。
有人对问题所在有任何想法吗?
谢谢

4

1 回答 1

4

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

如果没有,试试这个:

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();

这样做效果更好吗?

于 2013-10-04T12:01:11.440 回答