Qt不会发送POST数据

时间:2011-09-16 03:13:35

标签: qt post

我正在尝试使用QT将POST数据发送到php页面。我的代码如下:

#include <QHttp>
#include <QUrl>
#include <QString>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <iostream>
#include <QNetworkAccessManager>
#include <QObject>
....
void Transmissions::Send()
{
 QUrl serviceUrl = QUrl("http://192.168.1.138/postTest.php");
 QByteArray postData;
 QString username="user="+User.Email()+"&";
 QString Passwd="password="+User.Pass();
 postData.append(username);
 postData.append(Passwd);

 QNetworkAccessManager *networkManager = new QNetworkAccessManager(this);
 QObject::connect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(serviceRequestFinished(QNetworkReply*)));
 networkManager->post(QNetworkRequest(serviceUrl), postData);
}
....
void serviceRequestFinished(QNetworkReply *reply)
{
 QString data = reply->readAll();
    cerr << data.toStdString()<<endl;

}

现在,这段代码无法编译。这些是错误:

error: no matching function for call to ‘QNetworkAccessManager::QNetworkAccessManager(Transmissions* const)’

error: no matching function for call to ‘QObject::connect(QNetworkAccessManager*&, const char*, Transmissions* const, const char*)’

现在根据此How can I POST data to a url using QNetworkAccessManager和此处的QT文档http://doc.qt.io/qt-5/qnetworkaccessmanager.html我正在做的一切正确。哎呀我甚至可以从QT Docs网站复制并粘贴代码并得到同样的错误。我在这里缺少什么?

EDIT 如果我尝试这里显示的post方法How to send data back from PHP after a HTTP Post in Qt? 我明白了:

QObject::connect: Cannot connect (null)::configurationAdded(QNetworkConfiguration) to QNetworkConfigurationManager::configurationAdded(QNetworkConfiguration)

请有人帮忙

EDIT2 : 感谢VitaminP,我的代码现在编译。但现在这个问题正在发生:

QObject::connect: Cannot connect (null)::configurationAdded(QNetworkConfiguration) to QNetworkConfigurationManager::configurationAdded(QNetworkConfiguration)
QObject::connect: Cannot connect (null)::configurationRemoved(QNetworkConfiguration) to QNetworkConfigurationManager::configurationRemoved(QNetworkConfiguration)
QObject::connect: Cannot connect (null)::configurationUpdateComplete() to QNetworkConfigurationManager::updateCompleted()
QObject::connect: Cannot connect (null)::onlineStateChanged(bool) to QNetworkConfigurationManager::onlineStateChanged(bool)
QObject::connect: Cannot connect (null)::configurationChanged(QNetworkConfiguration) to QNetworkConfigurationManager::configurationChanged(QNetworkConfiguration)

1 个答案:

答案 0 :(得分:3)

这就是这条线: QNetworkAccessManager * networkManager = new QNetworkAccessManager(this);

这是错误的,你试图用一个指向Transmissions(通过“this”)的指针来构造它,我猜这是你的一个类。替换为: QNetworkAccessManager * networkManager = new QNetworkAccessManager;

如果它指向QObject,你只能传递this指针(参见你在Qt上链接的文档)。或者,您可以让您的类扩展QObject。

由于Transmissions不扩展QObject,因此当您传入“this”时,connect(...)将不起作用。