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. Can Qt make a Synchronous network download without Qeventloop?
Forum Updated to NodeBB v4.3 + New Features

Can Qt make a Synchronous network download without Qeventloop?

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 4.9k 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.
  • C Offline
    C Offline
    chenjie4255
    wrote on last edited by
    #1

    good evening guys.
    with QEventLoop, the code look like these:
    @
    class UrlDownloader : public QObject
    {
    Q_OBJECT
    public:
    UrlDownloader(QUrl url, QObject parent = 0)
    : QObject(parent)
    {
    connect(&m_WebCtrl, SIGNAL(finished(QNetworkReply
    )), this, SLOT(fileDownloaded(QNetworkReply*)));
    QNetworkRequest request(url);
    m_WebCtrl.get(request);
    }
    signals:
    void downloadEnd();

    private slots:
    void fileDownloaded(QNetworkReply* pReply)
    {
    m_DownloadedData = pReply->readAll();
    QFile downLoadFile("D:/test.doc");
    downLoadFile.open(QIODevice::ReadWrite);
    downLoadFile.write(m_DownloadedData);
    downLoadFile.close();

        pReply->deleteLater();
        emit downloadEnd();
    }
    

    private:
    QNetworkAccessManager m_WebCtrl;
    QByteArray m_DownloadedData;
    };

    void foo()
    {
    QEventLoop loop;
    UrlDownloader d(QUrl("http://qt-project.org"));
    connect(&d, SIGNAL(downloadEnd()), &loop, SLOT(quit()));
    loop.exec();
    ...
    }

    but those code would entry the eventloop , I want to avoid this usage, Can Qt has a way to download url file Synchronous without QEventLoop?

    thanks.

    @

    1 Reply Last reply
    0
    • C Offline
      C Offline
      chenjie4255
      wrote on last edited by
      #2

      wow, It is better if a timeout could be set in here...

      1 Reply Last reply
      0
      • raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #3

        AFAIK thats not possible.
        What is wrong with the usage of the QEventLoop like you already do?

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        0
        • C Offline
          C Offline
          chenjie4255
          wrote on last edited by
          #4

          for that I want to block all event until the download is finished.I has try to create QNetworkRequest's work in QThread's run function, but it seem doesn't work.
          [quote author="raven-worx" date="1377785758"]AFAIK thats not possible.
          What is wrong with the usage of the QEventLoop like you already do?[/quote]

          1 Reply Last reply
          0
          • K Offline
            K Offline
            KA51O
            wrote on last edited by
            #5

            if you want to block until a signal is received you could use "this approach":http://developer.nokia.com/Community/Wiki/How_to_wait_synchronously_for_a_Signal_in_Qt, but be aware that this has pitfalls and is not really recommended.

            For a discussion about the use of this method see "this thread":http://qt-project.org/forums/viewthread/24109/ .

            1 Reply Last reply
            0
            • JeroentjehomeJ Offline
              JeroentjehomeJ Offline
              Jeroentjehome
              wrote on last edited by
              #6

              Is it really the events you want to kill, or only the possibility of the user pressing buttons etc?
              To to that you might want to create a simple qdialog derived class as modal and show a "download" progressBar. Grey out the ok button until done or timeout and overwrite the closeEvent (x on the window) when not done yet. That way the user is unable to press any button on other widgets.
              Everything in modern OS runs on event and not previous on time! So killing the event loop is very highly discouraged!!! Also the user might think the program has crashed because the GUI is "dead"

              Greetz, Jeroen

              1 Reply Last reply
              0
              • C Offline
                C Offline
                chenjie4255
                wrote on last edited by
                #7

                I has ever try this method, this code would make the programe into the evenloop which i don't wanted it be . I just want it be fully blocked until download finished or timeout.
                Not I try to use QThread to exec a download , the code is look like these:

                @
                class UrlCacheFileDownloader : public QThread
                {
                Q_OBJECT
                public:
                UrlCacheFileDownloader(QUrl url, QSharedPointer<QWaitCondition> waitCondition, QObject parent = 0)
                : QThread(parent)
                {
                m_url = url;
                m_spWaitCondition = waitCondition;
                }
                protected:
                void run(){
                QNetworkRequest request(m_url);
                QNetworkAccessManager mgr;
                connect(&mgr, SIGNAL(finished(QNetworkReply
                )), this, SLOT(fileDownloaded(QNetworkReply*)), Qt::DirectConnection);
                mgr.get(request);
                exec();
                };
                .............
                private slots:
                void fileDownloaded(QNetworkReply* pReply)
                {
                m_DownloadedData = pReply->readAll();
                pReply->deleteLater();
                buildCacheFile();
                m_spWaitCondition->wakeAll();
                }
                private:
                .....
                };

                void download()
                {
                QMutex mutex;
                mutex.lock();
                QSharedPointer<QWaitCondition> spWaitCondition(new QWaitCondition());
                UrlCacheFileDownloader d(QUrl("http://qt-project.org/"), spWaitCondition);
                d.start();
                spWaitCondition->wait(&mutex, 5000);
                filename = d.downloadFileName();
                mutex.unlock();
                qDebug()<<"file:"<< filename;
                }
                @

                but these code just work once, program crash at the second time.What's wrong with it ?

                [quote author="KA51O" date="1377844353"]if you want to block until a signal is received you could use "this approach":http://developer.nokia.com/Community/Wiki/How_to_wait_synchronously_for_a_Signal_in_Qt, but be aware that this has pitfalls and is not really recommended.

                For a discussion about the use of this method see "this thread":http://qt-project.org/forums/viewthread/24109/ .[/quote]

                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