PDA

View Full Version : Http request



kars
30th August 2011, 15:10
Hi

First of all I'm totally new in QT... my boss got a program (sourcecode) based on Qt 4.7.4 and asked me to change parts of that sourcecode. I'm actually a borland c++ programmer.

Well now my question:
I have to implement a http request (send a request, wait for it, and go on with the program). Of course I've browsed through the forum pages and found some sourcecode but I can't get it to work. It's very frustrating.
Here's the piece I found (it's from early 2010):
My questions are:
1) When I just use the connect() function I can't compile. Has something changed the last 1 1/2 years? I'm getting "Invalid conversion from QNetWorkReply* to SOCKET... I' tried QObject::connect function and it compiles correctly.

2) The application is running without a gui and somewhere I read that a Q(Core)Application is needed to work with QEventloop and QTimer... is that correct and how should I implement that?

// Create a QNetworkAccessManageer and start listening for it's finished signal //
QNetworkAccessManager *manager = new QNetworkAccessManager();

// create the URL which we sent over to the gribserver //
QUrl url(myUrl); // myUrl declared somewhere else

// and send the request for the grib //
QNetworkReply *reply = manager->get(QNetworkRequest(url));

QEventLoop loop;
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));

QTimer timer;
connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
timer.start(5000);

loop.exec();

QByteArray result = reply->readAll();

if (reply->error()!=QNetworkReply::NoError)
{
qDebug() << "Request failed:" << reply->errorString();
return false;
}
else
{
qDebug() << "Reply from gribserver: " << result;
}


Well, these questions sound pretty stupid but I just can't figure it out how the sourcecode should look like. Any help would be apprechiated.

Cheers
Kars

ChrisW67
31st August 2011, 00:06
First of all I'm totally new in QT... my boss got a program (sourcecode) based on Qt 4.7.4 and asked me to change parts of that sourcecode. I'm actually a borland c++ programmer.

Curious, the latest released Qt is 4.7.3. It's unlikely to have a bearing on this problem though.


My questions are:
1) When I just use the connect() function I can't compile. Has something changed the last 1 1/2 years? I'm getting "Invalid conversion from QNetWorkReply* to SOCKET... I' tried QObject::connect function and it compiles correctly.

The standard socket connect(), declared in sys/socket.h, and QObject::connect() are two different beasts. If you are using the Qt classes to do this transfer then you should not need the low-level socket code at all, i.e. remove "#include <sys/socket.h>'



2) The application is running without a gui and somewhere I read that a Q(Core)Application is needed to work with QEventloop and QTimer... is that correct and how should I implement that?

If the application code you started with was complete then it should already have that. Look for main() and it will probably look like:


int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);

// some object creation

return app.exec(); // the event loop
}


Well, these questions sound pretty stupid but I just can't figure it out how the sourcecode should look like. Any help would be apprechiated.

The docs for Qt a pretty good. You could start by looking at Network Examples

kars
31st August 2011, 09:35
Hi Chris

Tanks for your answers... I've got it working finally :)
Yesterday I installed QT at my CPU and the Qt Creator About screen really says the following:
Qt Creator 2.2.1
Based on Qt 4.7.4 (32 bit)
Built on Jun 15 2011 at 16:37:12

Cheers
Karsten

wysota
31st August 2011, 10:15
It means Creator is based on Qt 4.7.4 (which is quite normal as it always uses prereleases of Qt) and not the application.