downloadProgress not emitted from QNetworkReply - qt

I'm trying to build a module which downloads a binary file in Qt, using QNetworkAccessManager. I use the same approach detailed in the documentation (see below), but while I do get readyRead signals, downloadProgress never arrives.
Everything happens on the same thread (the project is big so I cannot paste it all).
Any ideas?
QNetworkRequest request;
request.setUrl("http://XXX.s3.amazonaws.com/XXX.exe");
request.setRawHeader("User-Agent", "MyOwnBrowser 1.0");
QNetworkAccessManager * m_manager = new QNetworkAccessManager( this );
m_reply = m_manager->get(request);
m_reply->setParent(this);
connect(m_reply, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
connect(m_reply, SIGNAL(downloadProgress(qint64 bytesReceived, qint64 bytesTotal)),
this, SLOT(replyDownloadProgress(qint64 bytesReceived, qint64 bytesTotal)));

ok found it
wow what a simple mistake.
the answer is syntactic:
connect(m_reply, SIGNAL(downloadProgress(qint64 bytesReceived, qint64 bytesTotal)),
this, SLOT(replyDownloadProgress(qint64 bytesReceived, qint64 bytesTotal)));
is an error
it should be:
connect(m_reply, SIGNAL(downloadProgress(qint64 , qint64 )),
this, SLOT(replyDownloadProgress(qint64 , qint64 ));
once i changed it, i got the signal.
QT DOES NOT CHECK SYNTAX ERRORS IN ITS PREPROCESSOR (note to self)

I've patched my Qt to use a qFatal() instead of qWarning(), so the app asserts instead of printing error messages (that cannot be seen when linking against a release-build Qt). YMMV.

Related

QNetworkAccessManager sends GET two times

I've got some class to interfere with HTTP-server.
Here is meaningfull code parts:
const QString someClass::BASEURL = QString("http://127.0.0.1:8000/?");
someClass::someClass():
manager(new QNetworkAccessManager(this))
{
}
QNetworkReply *someClass::run(QString request)
{
qDebug() << request;
QEventLoop loop;
QObject::connect(manager, SIGNAL(finished(QNetworkReply*)), &loop, SLOT(quit()));
QNetworkReply *res = manager->get(QNetworkRequest(QUrl(BASEURL + request)));
loop.exec();
return res;
}
When I call method run(), sometimes (not every time) the are two identical GET-requests
(I looked with tcpdump). qDebug() executes 1 time.
Is there some error in my code? I can't see any possible explanation.
UPDATE:
After some tcpdump ouptut research.
After second request it sends packet with RST flag as an answer to FIN.
But I still can see no difference in TCP-streams that triggers the problem and that doesn't.
F.e. here is wireshark's output. Stream 8 went well. Stream 11 was duplicated with Stream 12.
I'm stuck with this. Maybe it's some protocol errors from server-size, I'm not sure. Or maybe it's a bug in QNetworkAccessManager.
Have you tried rewriting you code to be more asynchronous without using QEventLoop in a local scope? Your code looks good to me, but there might be some weird QT bug that you running into in the way it queues up requests for processing and using QEventLoop in the local scope. I usually use QNetworkAccessManager in the following manner to send GET and POST requests:
void someClass::run(QString request)
{
qDebug() << request;
QObject::connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(on_request_complete(QNetworkReply*)));
QNetworkReply *res = manager->get(QNetworkRequest(QUrl(BASEURL + request)));
}
void someClass::on_request_complete(QNetworkReply* response)
{
// Do stuff with your response here
}

How to start video-suite in MeeGo / Nokia N9 from Qt code?

I am having problems with launching Nokia's own video player from my application that I just don't seem to be able to solve.
My first attempt included calling
Qt.openUrlExternally(url)
from QML and that seemed to do the trick just fine, except that it opened the browser every time and used it instead of the video-suite (native player).
Next I tried cuteTube -approach where I start new process like this:
QStringList args;
args << url;
QProcess *player = new QProcess();
connect(player, SIGNAL(finished(int, QProcess::ExitStatus)), player, SLOT(deleteLater()));
player->start("/usr/bin/video-suite", args);
That worked, except that it required video-suite to be closed upon calling player->start, otherwise it did nothing.
My third attempt involved starting the video-suite via QDBus, but that didn't work any better:
QList<QVariant> args;
QStringList urls;
urls << url;
args.append(urls);
QDBusMessage message = QDBusMessage::createMethodCall(
"com.nokia.VideoSuite",
"/",
"com.nokia.maemo.meegotouch.VideoSuiteInterface",
"play");
message.setArguments(args);
message.setAutoStartService(true);
QDBusConnection bus = QDBusConnection::sessionBus();
if (bus.isConnected()) {
bus.send(message);
} else {
qDebug() << "Error, QDBus is not connected";
}
The problem with this is that it requires video-suite to be up and running - autoStartService parameter didn't help either. If video-suite isn't running already, the call opens it just fine but, alas, no video starts to play.
Eventually I tried using also VideoSuiteInterface, but even having the program compile with it seemed to be difficult. When I eventually managed to compile and link all relevant libraries, the results didn't differ from option 3 above.
So, is there a way to use either VideoSuiteInterface directly or via DBus so that it would start video playback regardless of the current state of the application?
The solution was actually simpler than I really thought initially; the VideoSuiteInterface -approach worked after all. All it took was to use it properly. Here are the full sources should anyone want to try it themselves.
player.h:
#ifndef PLAYER_H
#define PLAYER_H
#include <QObject>
#include <maemo-meegotouch-interfaces/videosuiteinterface.h>
class Player : public QObject {
Q_OBJECT
private:
VideoSuiteInterface* videosuite;
public:
Player(QObject *parent = 0);
Q_INVOKABLE void play(QString url);
};
#endif // PLAYER_H
player.cpp:
#include "player.h"
#include <QObject>
#include <QStringList>
#include <QtDeclarative>
Player::Player(QObject *parent) : QObject(parent) {}
void Player::play(QString url) {
QList<QVariant> args;
QStringList urls;
urls << url;
args.append(urls);
videosuite = new VideoSuiteInterface();
videosuite->play(urls);
}
In addition you may want to connect some signals to make the UI more responsive, but basically that should do the trick.
Finally, you need to remember to add following to your .pro file and you are good to go:
CONFIG += videosuiteinterface-maemo-meegotouch

Trying to upload more than 600mb file to server using Qt app

I am trying to develop file uploader using Qt. Here is my code :
QNetworkAccessManager * manager = new QNetworkAccessManager(this);
QNetworkRequest request(url);
QByteArray line;
QFile file(//path);
while(!file.atEnd())
{
line.append(file.readLine());
}
file.close();
QObject::connect(manager,SIGNAL(finished(QNetworkReply *)), this, SLOT(error_On_File_Send(QNetworkReply *)));
manager->post(request, line);
it works ok for small files. But it is not working in large file and gives std:bad_alloc error. What should i do
Use QNetworkAccessManager::post ( const QNetworkRequest & request, QIODevice * data ). It will automaticly read data when it is needed from any QIODevice. QFile is QIODevice so you should only slightly change your code. Note that you have to manage attached QIODevice by yourself (delete it after finished() signal)

QNetworkReply wait for finished

I am using Qt 4.6.3 and the following not-working code
QStringList userInfo;
QNetworkRequest netRequest(QUrl("http://api.stackoverflow.com/1.1/users/587532"));
QNetworkReply *netReply = netman->get(netRequest);
// from here onwards not working
netReply->waitForReadyRead(-1);
if (netReply->isFinished()==true)
{userInfo << do sth to reply;}
return userInfo;
as this function returns an empty QStringList, the app crashes. How to wait until the request has finished and then process the reply within one function
You can use event loop:
QEventLoop loop;
connect(netReply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
// here you have done.
Also you should consider adding some shorter then network timeout (20s?). I'm not sure if finished is called even if an error occured. So it is possible, that you have connect to error signal also.
First I recommend you to read the relevant documentation from the Qt Documentation Reference that you can find here: http://doc.qt.nokia.com/latest/classes.html.
Looking at your code sample it seems that you already have, along side with QNetworkRequest and QNetworkReply, a QNetworkAccessManager. What you need is to connect a slot to the finished(QNetworkReply *) signal. This signal is emitted whenever a pending network reply is finished.
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(replyFinished(QNetworkReply*)));
manager->get(QNetworkRequest(QUrl("http://api.stackoverflow.com")));
Now, in your slot, you can read the data which was sent in response to your request. Something like:
void MyClass::MySlot(QNetworkReply *data) {
QFile file("dataFromRequest");
if (!file.open(QIODevice::WriteOnly))
return;
file.write(data->readAll());
file.close();
}
EDIT:
To wait synchronously for a signal use QEventLoop. You have an example here
http://wiki.forum.nokia.com/index.php/How_to_wait_synchronously_for_a_Signal_in_Qt
These replies here are all using old syntax and do not apply to the latest QT.
To wait for the network request to finish:
QEventLoop loop;
connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();

Whats the problem with my code (http post)

i am developing a simple application which uploads image to yfrog.com.(These images will be reflected in twitter account). Here is my code. but it is not working. I am not getting response from server.
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
QNetworkRequest request(QUrl("http://yfrog.com/api/uploadAndPost"));
QByteArray data;
QUrl params,params1;
QFile file("some image path");
QString boundary("-----abcde12345");
QString body = "\r\n--" + boundary + "\r\n";
params.addQueryItem("username",twitterusername);
params.addQueryItem("password",twitterpassword);
params.addQueryItem("message",some message...);
params.addQueryItem("key",mydeveloperkey);
data.append(body);
data.append(params.toString());
QByteArray ba;
ba=file.readAll();
QString body1(ba);
params1.addQueryItem("media",body1);
data.append(params1.toString());
data.append(body);
request.setRawHeader("Content-Type","multipart/form-data; boundary=-----abcde12345");
request.setHeader(QNetworkRequest::ContentLengthHe ader,data.size());
QNetworkReply *reply = manager->post(request,data);
reply->waitForReadyRead(-1);
qDebug() << "replay :"<<reply->readAll();
If i checked the requested TCP packets from wireshark, it is giving a error message like 'malformed packets'.
For reference : http://code.google.com/p/imageshacka...GuploadAndPost
Please any body help regarding this. Where i am doing wrong?
QNetworkReply::waitForReadyRead does not have an implementation so it always refers the base class waitForReadyRead() (in this case QIODevice). In the base class, you will see that waitForReadyRead always returns FALSE.
From the docs, you would have to instead use the readRead() signal in QNetworkReply and read the data when the slot is called.
QNetworkReply *reply = manager->get(request);
connect(reply, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));

Resources