我想發送POST數據到使用QT的PHP頁面。我的代碼如下:Qt不會發送POST數據
#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網站的代碼,並獲得相同的錯誤。我在這裏錯過了什麼?
編輯 如果我嘗試在這裏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)
什麼是您嘗試連接的對象/方法? (null):: configurationAdded看起來像對象創建失敗,或者甚至它不是MOCed(你想連接的所有對象必須擴展QObject並在類定義中有Q_OBJECT宏) – VitaminP
我認爲這個問題(來自Edit2)是你的插槽不是你的'Transmissions'類的成員,它只是一個獨立的函數,使你的頭文件看起來像:'class Transmissions:public QObject(){slots:void configurationAdded(..) ; void configurationRemoved(...);}'etc. – docsteer
@docsteer,你的權利。我從來沒有把這些放在我的頭文件中。我在這裏找到了一些文檔doc.qt.nokia.com/stable/qnetworkconfigurationmanager.html看起來我沒有我不懂信號和插槽 – CountMurphy