2011-09-16 92 views
0

我想發送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) 
+0

什麼是您嘗試連接的對象/方法? (null):: configurationAdded看起來像對象創建失敗,或者甚至它不是MOCed(你想連接的所有對象必須擴展QObject並在類定義中有Q_OBJECT宏) – VitaminP

+1

我認爲這個問題(來自Edit2)是你的插槽不是你的'Transmissions'類的成員,它只是一個獨立的函數,使你的頭文件看起來像:'class Transmissions:public QObject(){slots:void configurationAdded(..) ; void configurationRemoved(...);}'etc. – docsteer

+0

@docsteer,你的權利。我從來沒有把這些放在我的頭文件中。我在這裏找到了一些文檔doc.qt.nokia.com/stable/qnetworkconfigurationmanager.html看起來我沒有我不懂信號和插槽 – CountMurphy

回答

3

這是這一行: QNetworkAccessManager * NETWORKMANAGER =新QNetworkAccessManager(本);

這是錯的,你試圖用一個指向Transmissions的指針(通過「this」)來構造它,我猜測它是你的一個類。替換爲: QNetworkAccessManager * networkManager = new QNetworkAccessManager;

如果指針指向一個QObject,則只能傳遞此指針(請參閱您在Qt中鏈接的文檔)。另外,你可以讓你的類擴展QObject。

由於傳輸不會擴展QObject,因此當您傳入「this」時連接(...)也不起作用。

+0

你先生是聖人,有一個問題,我得到一堆錯誤,我更新了我的問題。 – CountMurphy