0

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(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)
Community
  • 1
  • 1
CountMurphy
  • 1,086
  • 2
  • 18
  • 39
  • What are the objects/methods you're trying to connect? (null)::configurationAdded looks like either the object creation failed, or perhaps even it wasn't MOCed (all objects that you want to connect have to extend QObject and have the Q_OBJECT macro in the class definition. – VitaminP Sep 16 '11 at 14:18
  • 1
    I think the problem (from Edit2) is that your slots aren't a member of your `Transmissions` class, it's just a standalone function. Make your header file look like : `class Transmissions : public QObject() { slots: void configurationAdded(..); void configurationRemoved(...);}` etc. – docsteer Sep 16 '11 at 14:27
  • @docsteer, your right. I never put those in my header. I found some documentation here doc.qt.nokia.com/stable/qnetworkconfigurationmanager.html Looks like I didn't do my reading on signals and slots. – CountMurphy Sep 16 '11 at 16:31

1 Answers1

3

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.

AAEM
  • 1,837
  • 2
  • 18
  • 26
VitaminP
  • 113
  • 5