QCoreApplication and Network
-
Hello,
I am trying to create a command line tool that fetches a file from the internet, but somehow it does not work :( I guess it's something with the event processing, but it simply drops an empty QByteArray .
Here's my code:
Main:
@#include <QCoreApplication>
#include "mainworker.h"
int main(int argc, char *argv[])
{
//Neccessary for event processing
QCoreApplication app(argc, argv);
MainWorker m(app.arguments());
m.start();
QObject::connect(&m, SIGNAL(finished()), &app, SLOT(quit()));
app.exec();
}
@MainWorker (Subclass of QThread)(partly):
@MainWorker::MainWorker(QStringList av)
{
argv = av;
}
void MainWorker::run() {
//call downloader here, for example like this (ugly, I know ;):
FileDownloader fd(QUrl::fromEncoded("http://example.com/"));
while(fd.downloadedData() == NULL) {}
foreach (char c, fd.downloadedData()) {
cout << c;
}
}@
I tried the example "FileDownloader" code as well as my own attempt, I even experimented with QMutex to wait for the downloader to finish (which worked quite well as the downloader class never finished) and tried out busy-waiting, too. I even played around with putting delays here and there to prevent the downloader from being called while not having started the even loop, but nothing helps.
Btw, here is the downloader I tried last:
@#include "filedownloader.h"
FileDownloader::FileDownloader(QUrl imageUrl, QObject parent) :
QObject(parent)
{
QTimer t;
t.singleShot(10000, parent, SLOT(fileDownloaded(QNetworkReply)));
connect(&m_WebCtrl, SIGNAL(finished(QNetworkReply*)),
SLOT(fileDownloaded(QNetworkReply*)));QNetworkRequest request(imageUrl); m_WebCtrl.get(request); qDebug("request started");
}
FileDownloader::~FileDownloader()
{}
void FileDownloader::fileDownloaded(QNetworkReply* pReply)
{
m_DownloadedData = pReply->readAll();
//emit a signal
pReply->deleteLater();
emit downloaded();
qDebug("finished");
qDebug(pReply->errorString().toStdString().c_str()); //ugly, but should work
}
QByteArray FileDownloader::downloadedData() const
{
return m_DownloadedData;
}
@
and the header:
@#ifndef FILEDOWNLOADER_H
#define FILEDOWNLOADER_H
#include <QObject>
#include <QByteArray>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QTimer>class FileDownloader : public QObject
{
Q_OBJECT
public:
explicit FileDownloader(QUrl imageUrl, QObject parent = 0);
virtual ~FileDownloader();
QByteArray downloadedData() const;
signals:
void downloaded();
private slots:
void fileDownloaded(QNetworkReply pReply);
private:
QNetworkAccessManager m_WebCtrl;
QByteArray m_DownloadedData;};
#endif // FILEDOWNLOADER_H
@
This piece of code outputs the "request started", but never reached the "finished", whatever I tried to do.
Thanks in advance and sorry for that much text
Fripp -
Hi and welcome to devnet,
@
MainWorker m(app.arguments());
m.start();
QObject::connect(&m, SIGNAL(finished()), &app, SLOT(quit()));@You are quitting the application as soon as finished is emitted. So it's very likely that you are not even seeing the emit before the application finishes
-
Well, finished() is emitted when closing the MainWorker thread m, which tells the download to start and
@while(fd.downloadedData() == NULL) {}@
there should wait for the download to do at least anything.
As said I tried out other thongs like QMutex to wait for the download, but it seems the download never even starts (->QMutex or the loop above halt for ever) :( -
I missed the while loop. With that loop you are blocking the event management thus no download occurs. You could use a QEvenLoop for that but why do you want to block things like that ?