Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QCoreApplication and Network

QCoreApplication and Network

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Fripp
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • F Offline
        F Offline
        Fripp
        wrote on last edited by
        #3

        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) :(

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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 ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved