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. [SOLVED] moveToThread and QEventLoop
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] moveToThread and QEventLoop

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

    I have made a class which queries data from the net. I have used QEventLoop to wait until QNetworkReply is finished. And it works greatly. Here is example.

    @
    void myclass::queryData()
    {
    QNetworkAccessManager *m_manager = new QNetworkAccessManager();
    QNetworkReply *reply = m_manager->get(QNetworkRequest( QUrl( "http://url...." )));

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

    QByteArray b = reply->readAll();
    // .... etc.
    }
    @

    But now I should use this class with multithreading. So I think to use moveToThread

    @
    QThread *thread = new QThread();
    myclass *mc = new myclass();
    mc->moveToThread(thread);
    connect(thread,&QThread::started,mc,&myclass::queryData);
    thread->start();
    @

    But now QEventLoop seems to be problem. It doesn't continue executing. So how to solve this problem? I wouldn't like to use signal/slot to wait QNetworkReply because I have many consecutive queries in the same class.

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      You need to assign a parent to QThread object, for example:
      @
      QThread *thread = new QThread(this);
      @

      In general, it should work, even in a thread. Although, of course, using signals and slots would be better.

      (Z(:^

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jimcad
        wrote on last edited by
        #3

        Unfortunately, it didn't help.

        EDIT: hmmm... the same problem seems to be even signal and slot is used. Why QNetworkReply::finished signal not emitted when using moveToThread?

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

          Why do you want to have eventLoop ? You are making the program to wait since you are doing exec. I presume that you want to wait for reading till download finished. If that is the case, you can just work with readyRead, downloadProgress or finished signals.

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          0
          • JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote on last edited by
            #5

            It's not immediately obvious what the problem is, but I suspect that some of the member variables in myclass were not moved to the other thread, because you didn't give them a parent.

            See the documentation on Thread Affinity: http://qt-project.org/doc/qt-5/QObject.html#thread-affinity

            "A QObject's member variables do not automatically become its children. The parent-child relationship must be set by either passing a pointer to the child's constructor, or by calling setParent(). Without this step, the object's member variables will remain in the old thread when moveToThread() is called."

            Just to check, do you see any warning messages at the console output?

            [quote author="jimcad" date="1408001067"]
            @
            QNetworkReply *reply = m_manager->get(QNetworkRequest( QUrl( "http://url...." )));

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

            QByteArray b = reply->readAll();
            // .... etc.
            @
            [/quote]By the way, you don't need an extra event loop. You can use a lambda instead:

            @
            QNetworkReply *reply = m_manager->get(QNetworkRequest( QUrl( "http://url...." )));

            connect(reply,&QNetworkReply::finished, [=]
            {
            QByteArray b = reply->readAll();
            reply->deleteLater();
            // .... etc.
            }
            @

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            1 Reply Last reply
            0
            • J Offline
              J Offline
              jimcad
              wrote on last edited by
              #6

              Thanks. Indeed the problem must be one of member variable. I created a new test class and it seems to work now.

              And thanks for the tip about lambda slot.

              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