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. QThread, signals and lambdas

QThread, signals and lambdas

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.4k 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
    Jeff Barnes
    wrote on last edited by
    #1

    It took me a while to figure out that this causes a crash. Linguistically, it is opaque to me. Could someone please tell me why this isn't good, please?

    MyClass::MyClass
        :   QWidget()
        ,   myObject(new MyObject)
    {
        // Causes memory corruption, intermittent crashes, QTimer warnings, etc.
    //    QObject::connect(myObject, MyObject::mySignal, []() {
    //        QMessageBox mb;
    //        mb.setText("Hello world");
    //        mb.exec();
    //    });
        //this works fine
        QObject::connect(myObject, MyObject::mySignal, this, &MyClass::handleMySignal);
        QThread *thr = new QThread;
        QObject::connect(myObject, MyObject::destroyed, thr, QThread::quit);
        QObject::connect(thr, &QThread, &QThread::finished, thr, &QThread::deleteLater);
        myObject->moveToThread(thr);
        thr->start();
    }
    
    void MyClass::handleMySignal()
    {
        QMessageBox mb;
        mb.setText("Hello world");
        mb.exec();
    }
    
    
    KroMignonK 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      This is because the lambda version is executed in the thread, not in the main thread which is not allowed for gui operations.
      Pass 'this' as third parameter so the lambda gets executed in the main thread (= where 'this' lives in)

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      J 1 Reply Last reply
      5
      • Christian EhrlicherC Christian Ehrlicher

        This is because the lambda version is executed in the thread, not in the main thread which is not allowed for gui operations.
        Pass 'this' as third parameter so the lambda gets executed in the main thread (= where 'this' lives in)

        J Offline
        J Offline
        Jeff Barnes
        wrote on last edited by
        #3

        @Christian-Ehrlicher Thanks

        1 Reply Last reply
        1
        • J Jeff Barnes

          It took me a while to figure out that this causes a crash. Linguistically, it is opaque to me. Could someone please tell me why this isn't good, please?

          MyClass::MyClass
              :   QWidget()
              ,   myObject(new MyObject)
          {
              // Causes memory corruption, intermittent crashes, QTimer warnings, etc.
          //    QObject::connect(myObject, MyObject::mySignal, []() {
          //        QMessageBox mb;
          //        mb.setText("Hello world");
          //        mb.exec();
          //    });
              //this works fine
              QObject::connect(myObject, MyObject::mySignal, this, &MyClass::handleMySignal);
              QThread *thr = new QThread;
              QObject::connect(myObject, MyObject::destroyed, thr, QThread::quit);
              QObject::connect(thr, &QThread, &QThread::finished, thr, &QThread::deleteLater);
              myObject->moveToThread(thr);
              thr->start();
          }
          
          void MyClass::handleMySignal()
          {
              QMessageBox mb;
              mb.setText("Hello world");
              mb.exec();
          }
          
          
          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by
          #4

          @Jeff-Barnes To solve the issue, you should use this as receiver of the signal, so the lambda will be executed in the right thread:

          QObject::connect(myObject, MyObject::mySignal, this, []() {
                QMessageBox mb;
                mb.setText("Hello world");
                mb.exec();
            });
          

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          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