开发者

Qt wont send POST data

I am trying to send POST data to a php page using QT. My code is as follows:

#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;

}

Now, This code will not compile. These are the errors:

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

and

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

Now according to this How can I POST data to a url using QNetworkAccessManager and the QT documentation here http://doc.qt.io/qt-5/qnetworkaccessmanager.html I am doing everything right. Heck I can even copy and paste the code from the QT Docs site and get the same error. What am I missing here?

EDIT if I try the post method shown here How to send data back from PHP after a HTTP Post in Qt? I get this:

QObject::connect: Cannot connect (null)::configurationAdded(QNetworkConfiguration) to QNetworkConfigurationManager::configurationAdded(开发者_JAVA技巧QNetworkConfiguration)

please someone help

EDIT2: Thanks to VitaminP my code now compiles. But now this issue is happening:

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)


It's this line:

QNetworkAccessManager *networkManager = new QNetworkAccessManager(this);

This is wrong, you're trying to construct it with a pointer to Transmissions (through "this"), which I'm guessing is one of your classes. Replace it with:

QNetworkAccessManager *networkManager = new QNetworkAccessManager;

You can only pass the this pointer if it points to a QObject (see the docs you linked on Qt). Alternatively, you can have your class extend QObject.

Since Transmissions doesn't extend QObject, then the connect(...) won't work when you pass in "this" either.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜