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. ASSERT failure in QCoreApplication::sendEvent: 'Cannot send events to objects owened by a different thread
Forum Updated to NodeBB v4.3 + New Features

ASSERT failure in QCoreApplication::sendEvent: 'Cannot send events to objects owened by a different thread

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 5.2k Views 2 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.
  • D Offline
    D Offline
    Domenico
    wrote on 13 Feb 2019, 15:49 last edited by kshegunov
    #1

    hello to the whole community Qt Forum,
    I need a help I can not understand the problem that is reporting in my code .. as the codes of my application is too long .. I bring here a similar code and I know that the error comes from there .. I can you tell what's wrong? thank you very much to those who will help me

    MainWindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    #include "afxwin.h"
    #include <iostream>
    #include <thread>
    #include <QDebug>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
        static void MainThread(LPVOID lp);
    
    public slots:
        void stopLoop();
    
    private:
        Ui::MainWindow *ui;
        std::thread t;
    };
    
    #endif // MAINWINDOW_H
    

    MainWindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        t = std::thread(MainThread,(void*)this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::MainThread(LPVOID lp)
    {
        MainWindow *parent = (MainWindow*)lp;
    
        qDebug()<<"Hello Concurrent World\n";
        parent->stopLoop();
    }
    
    void MainWindow::stopLoop()
    {
        t.detach();
        ui->pushButton->setEnabled(false);
    }
    

    main.cpp

    #include "mainwindow.h"
    #include <QApplication>
    
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
       return a.exec();
    }
    

    the error that brings me back is:

    0_1550073026942_04481009-3c74-4094-b7f1-97fda384b06e-image.png

    what's wrong?

    [Added code tags ~kshegunov]

    K 1 Reply Last reply 13 Feb 2019, 16:20
    0
    • K kshegunov
      13 Feb 2019, 16:28

      Start reading here. There are also examples throughout the documentation how threads are to interact with the GUI (and in the forum as well).

      In a nutshell you have to emit a signal that's connected to a slot, which in turn is going to disable the button. Before doing that, however, you need a QObject that's going to emit it, and which is living in the worker thread.

      PS. Here's some skeleton code how to spin a thread Qt-style.

      D Offline
      D Offline
      Domenico
      wrote on 13 Feb 2019, 16:51 last edited by
      #5

      @kshegunov

      Thank you very much!

      I simply replaced

      QMetaObject:invokeMethod(parent,"stopLoop",Qt::QueueConnection);

      on

      parent->stopLoop();

      and the problem is solved!

      1 Reply Last reply
      0
      • D Domenico
        13 Feb 2019, 15:49

        hello to the whole community Qt Forum,
        I need a help I can not understand the problem that is reporting in my code .. as the codes of my application is too long .. I bring here a similar code and I know that the error comes from there .. I can you tell what's wrong? thank you very much to those who will help me

        MainWindow.h

        #ifndef MAINWINDOW_H
        #define MAINWINDOW_H
        
        #include <QMainWindow>
        
        #include "afxwin.h"
        #include <iostream>
        #include <thread>
        #include <QDebug>
        
        namespace Ui {
        class MainWindow;
        }
        
        class MainWindow : public QMainWindow
        {
            Q_OBJECT
        
        public:
            explicit MainWindow(QWidget *parent = nullptr);
            ~MainWindow();
        
            static void MainThread(LPVOID lp);
        
        public slots:
            void stopLoop();
        
        private:
            Ui::MainWindow *ui;
            std::thread t;
        };
        
        #endif // MAINWINDOW_H
        

        MainWindow.cpp

        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        
        MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
            t = std::thread(MainThread,(void*)this);
        }
        
        MainWindow::~MainWindow()
        {
            delete ui;
        }
        
        void MainWindow::MainThread(LPVOID lp)
        {
            MainWindow *parent = (MainWindow*)lp;
        
            qDebug()<<"Hello Concurrent World\n";
            parent->stopLoop();
        }
        
        void MainWindow::stopLoop()
        {
            t.detach();
            ui->pushButton->setEnabled(false);
        }
        

        main.cpp

        #include "mainwindow.h"
        #include <QApplication>
        
        
        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
            MainWindow w;
            w.show();
        
           return a.exec();
        }
        

        the error that brings me back is:

        0_1550073026942_04481009-3c74-4094-b7f1-97fda384b06e-image.png

        what's wrong?

        [Added code tags ~kshegunov]

        K Offline
        K Offline
        kshegunov
        Moderators
        wrote on 13 Feb 2019, 16:20 last edited by
        #2

        This:

        ui->pushButton->setEnabled(false);
        

        is a race condition. You mustn't touch any UI object from a thread different than the GUI one. As a matter of fact, you shouldn't execute methods of GUI classes in different threads, that's basically asking for trouble.

        Read and abide by the Qt Code of Conduct

        D 1 Reply Last reply 13 Feb 2019, 16:24
        4
        • K kshegunov
          13 Feb 2019, 16:20

          This:

          ui->pushButton->setEnabled(false);
          

          is a race condition. You mustn't touch any UI object from a thread different than the GUI one. As a matter of fact, you shouldn't execute methods of GUI classes in different threads, that's basically asking for trouble.

          D Offline
          D Offline
          Domenico
          wrote on 13 Feb 2019, 16:24 last edited by
          #3

          @kshegunov

          Thanks for the reply! so how can I disable the pushButton?

          K 1 Reply Last reply 13 Feb 2019, 16:28
          0
          • D Domenico
            13 Feb 2019, 16:24

            @kshegunov

            Thanks for the reply! so how can I disable the pushButton?

            K Offline
            K Offline
            kshegunov
            Moderators
            wrote on 13 Feb 2019, 16:28 last edited by kshegunov
            #4

            Start reading here. There are also examples throughout the documentation how threads are to interact with the GUI (and in the forum as well).

            In a nutshell you have to emit a signal that's connected to a slot, which in turn is going to disable the button. Before doing that, however, you need a QObject that's going to emit it, and which is living in the worker thread.

            PS. Here's some skeleton code how to spin a thread Qt-style.

            Read and abide by the Qt Code of Conduct

            D 1 Reply Last reply 13 Feb 2019, 16:51
            1
            • K kshegunov
              13 Feb 2019, 16:28

              Start reading here. There are also examples throughout the documentation how threads are to interact with the GUI (and in the forum as well).

              In a nutshell you have to emit a signal that's connected to a slot, which in turn is going to disable the button. Before doing that, however, you need a QObject that's going to emit it, and which is living in the worker thread.

              PS. Here's some skeleton code how to spin a thread Qt-style.

              D Offline
              D Offline
              Domenico
              wrote on 13 Feb 2019, 16:51 last edited by
              #5

              @kshegunov

              Thank you very much!

              I simply replaced

              QMetaObject:invokeMethod(parent,"stopLoop",Qt::QueueConnection);

              on

              parent->stopLoop();

              and the problem is solved!

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on 13 Feb 2019, 19:40 last edited by SGaist
                #6

                Hi,

                Except that it's the wrong thing to do. You are currently just lucky it works. As already stated, don't modify GUI elements from another thread.

                Sorry, I misunderstood what you wrote. invokeMethod is a solution.

                However, you should still consider clean separation between UI updates and thread processing.

                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

                1/6

                13 Feb 2019, 15:49

                • Login

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