Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread.
Forum Update on Monday, May 27th 2025

ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread.

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
6 Posts 4 Posters 4.6k Views
  • 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.
  • M Offline
    M Offline
    mdma2
    wrote on 27 Mar 2018, 13:26 last edited by
    #1

    There's a simple code that just add empty QwtPlot (plot library qwt) in QQuickPaintedItem:

    /* qmlqwtplotter.h */
    
    #include <QQuickPaintedItem>
    #include <qwt_plot.h>
    
    class QmlQwtPlotter : public QQuickPaintedItem
    {
        Q_OBJECT
    
    public:
        QmlQwtPlotter(QQuickItem* parent = 0);
        ~QmlQwtPlotter();
    
        void paint(QPainter* painter) override;
    
    private:
        QwtPlot* qwtPlot;
    };
    
    /* qmlqwtplotter.cpp */
    
    #include "qmlqwtplotter.h"
    
    QmlQwtPlotter::QmlQwtPlotter(QQuickItem* parent) :
        QQuickPaintedItem(parent)
    {
        qwtPlot = new QwtPlot;
    }
    
    QmlQwtPlotter::~QmlQwtPlotter() {
        delete qwtPlot;
    }
    
    void QmlQwtPlotter::paint(QPainter *painter) {
        qwtPlot->render(painter);  // !!!!
    }
    
    /* main.cpp */
    
    #include <QGuiApplication>
    #include <QApplication>
    #include <QQmlApplicationEngine>
    #include "qmlqwtplotter.h"
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
    
        qmlRegisterType<QmlQwtPlotter>("QmlQwt",1,0,"QmlQwtPlotter");
    
        QQmlApplicationEngine engine;
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    
        return app.exec();
    }
    
    /* main.qml */
    
    import QtQuick 2.5
    import QtQuick.Window 2.2
    import QtCharts 2.0
    import QmlQwt 1.0
    
    Window {
        visible: true
        width: 640
        height: 320
    
        QmlQwtPlotter {
            anchors.fill: parent // !!!!
        }
    }
    

    The application can't be executed - the runtime error is appeared:

    ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread. Current thread 0x0x3a050108. Receiver '' (of type 'QwtPlot') was created in thread 0x0x3fc728", file kernel\qcoreapplication.cpp, line 589
    

    The error is disappeared if I comment strings qwtPlot->render(painter) in qmlqwtplotter.cpp or anchors.fill: parent in main.qml - just painting the white empty window.

    The most strange thing is that the problem exists on Windows 8.1 and doesn't exist on Linux Mint 18.2 (same versions of Qt and Qt Creator).

    What can it be?

    J D 2 Replies Last reply 27 Mar 2018, 21:13
    0
    • M mdma2
      27 Mar 2018, 13:26

      There's a simple code that just add empty QwtPlot (plot library qwt) in QQuickPaintedItem:

      /* qmlqwtplotter.h */
      
      #include <QQuickPaintedItem>
      #include <qwt_plot.h>
      
      class QmlQwtPlotter : public QQuickPaintedItem
      {
          Q_OBJECT
      
      public:
          QmlQwtPlotter(QQuickItem* parent = 0);
          ~QmlQwtPlotter();
      
          void paint(QPainter* painter) override;
      
      private:
          QwtPlot* qwtPlot;
      };
      
      /* qmlqwtplotter.cpp */
      
      #include "qmlqwtplotter.h"
      
      QmlQwtPlotter::QmlQwtPlotter(QQuickItem* parent) :
          QQuickPaintedItem(parent)
      {
          qwtPlot = new QwtPlot;
      }
      
      QmlQwtPlotter::~QmlQwtPlotter() {
          delete qwtPlot;
      }
      
      void QmlQwtPlotter::paint(QPainter *painter) {
          qwtPlot->render(painter);  // !!!!
      }
      
      /* main.cpp */
      
      #include <QGuiApplication>
      #include <QApplication>
      #include <QQmlApplicationEngine>
      #include "qmlqwtplotter.h"
      
      int main(int argc, char *argv[])
      {
          QApplication app(argc, argv);
      
          qmlRegisterType<QmlQwtPlotter>("QmlQwt",1,0,"QmlQwtPlotter");
      
          QQmlApplicationEngine engine;
          engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
      
          return app.exec();
      }
      
      /* main.qml */
      
      import QtQuick 2.5
      import QtQuick.Window 2.2
      import QtCharts 2.0
      import QmlQwt 1.0
      
      Window {
          visible: true
          width: 640
          height: 320
      
          QmlQwtPlotter {
              anchors.fill: parent // !!!!
          }
      }
      

      The application can't be executed - the runtime error is appeared:

      ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread. Current thread 0x0x3a050108. Receiver '' (of type 'QwtPlot') was created in thread 0x0x3fc728", file kernel\qcoreapplication.cpp, line 589
      

      The error is disappeared if I comment strings qwtPlot->render(painter) in qmlqwtplotter.cpp or anchors.fill: parent in main.qml - just painting the white empty window.

      The most strange thing is that the problem exists on Windows 8.1 and doesn't exist on Linux Mint 18.2 (same versions of Qt and Qt Creator).

      What can it be?

      J Offline
      J Offline
      jeremy_k
      wrote on 27 Mar 2018, 21:13 last edited by
      #2

      @mdma2 said in ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread.:

      void QmlQwtPlotter::paint(QPainter *painter) {
      qwtPlot->render(painter); // !!!!
      }
      [...]
      The application can't be executed - the runtime error is appeared:

      ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread. Current thread 0x0x3a050108. Receiver '' (of type 'QwtPlot') was created in thread 0x0x3fc728", file kernel\qcoreapplication.cpp, line 589
      

      The problem is that QQuickPaintedItem::paint() is called from the scene graph thread, but QWidget::render() expects to be called from the widget's associated thread. The widget attempts to send itself events to process as part of the rendering process. QCoreApplication::sendEvent() checks the widget's thread affinity, and fails.

      The most strange thing is that the problem exists on Windows 8.1 and doesn't exist on Linux Mint 18.2 (same versions of Qt and Qt Creator).

      What can it be?

      Is there a custom scene graph renderer in use?

      Asking a question about code? http://eel.is/iso-c++/testcase/

      M 1 Reply Last reply 28 Mar 2018, 06:07
      2
      • J jeremy_k
        27 Mar 2018, 21:13

        @mdma2 said in ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread.:

        void QmlQwtPlotter::paint(QPainter *painter) {
        qwtPlot->render(painter); // !!!!
        }
        [...]
        The application can't be executed - the runtime error is appeared:

        ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread. Current thread 0x0x3a050108. Receiver '' (of type 'QwtPlot') was created in thread 0x0x3fc728", file kernel\qcoreapplication.cpp, line 589
        

        The problem is that QQuickPaintedItem::paint() is called from the scene graph thread, but QWidget::render() expects to be called from the widget's associated thread. The widget attempts to send itself events to process as part of the rendering process. QCoreApplication::sendEvent() checks the widget's thread affinity, and fails.

        The most strange thing is that the problem exists on Windows 8.1 and doesn't exist on Linux Mint 18.2 (same versions of Qt and Qt Creator).

        What can it be?

        Is there a custom scene graph renderer in use?

        M Offline
        M Offline
        mdma2
        wrote on 28 Mar 2018, 06:07 last edited by
        #3

        @jeremy_k said in ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread.:

        Is there a custom scene graph renderer in use?

        Nope. As far as I understand your message, QwtPlot doesn't have its own custom scene graph renderer. If I tried to render usual QWidget in paint(), the compiler would give the same error.

        Well, is there a right way to render QWidget in QQuickPaintedItem::paint()? Or is there an other way to represent QWidget object in QML?

        J 1 Reply Last reply 29 Mar 2018, 16:38
        0
        • M mdma2
          28 Mar 2018, 06:07

          @jeremy_k said in ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread.:

          Is there a custom scene graph renderer in use?

          Nope. As far as I understand your message, QwtPlot doesn't have its own custom scene graph renderer. If I tried to render usual QWidget in paint(), the compiler would give the same error.

          Well, is there a right way to render QWidget in QQuickPaintedItem::paint()? Or is there an other way to represent QWidget object in QML?

          J Offline
          J Offline
          jeremy_k
          wrote on 29 Mar 2018, 16:38 last edited by jeremy_k
          #4

          @mdma2 said in ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread.:

          @jeremy_k said in ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread.:

          Is there a custom scene graph renderer in use?

          Nope. As far as I understand your message, QwtPlot doesn't have its own custom scene graph renderer. If I tried to render usual QWidget in paint(), the compiler would give the same error.

          Well, is there a right way to render QWidget in QQuickPaintedItem::paint()? Or is there an other way to represent QWidget object in QML?

          There's a general solution for widgets in quick 1, which is based on graphics view. Depending on your needs, that may be a workable solution.

          With quick 2, the widget can be rendered to a QImage or QPixmap in the GUI thread which is then used by the scene graph thread via QQuickPaintedItem::paint().

          Asking a question about code? http://eel.is/iso-c++/testcase/

          1 Reply Last reply
          0
          • M mdma2
            27 Mar 2018, 13:26

            There's a simple code that just add empty QwtPlot (plot library qwt) in QQuickPaintedItem:

            /* qmlqwtplotter.h */
            
            #include <QQuickPaintedItem>
            #include <qwt_plot.h>
            
            class QmlQwtPlotter : public QQuickPaintedItem
            {
                Q_OBJECT
            
            public:
                QmlQwtPlotter(QQuickItem* parent = 0);
                ~QmlQwtPlotter();
            
                void paint(QPainter* painter) override;
            
            private:
                QwtPlot* qwtPlot;
            };
            
            /* qmlqwtplotter.cpp */
            
            #include "qmlqwtplotter.h"
            
            QmlQwtPlotter::QmlQwtPlotter(QQuickItem* parent) :
                QQuickPaintedItem(parent)
            {
                qwtPlot = new QwtPlot;
            }
            
            QmlQwtPlotter::~QmlQwtPlotter() {
                delete qwtPlot;
            }
            
            void QmlQwtPlotter::paint(QPainter *painter) {
                qwtPlot->render(painter);  // !!!!
            }
            
            /* main.cpp */
            
            #include <QGuiApplication>
            #include <QApplication>
            #include <QQmlApplicationEngine>
            #include "qmlqwtplotter.h"
            
            int main(int argc, char *argv[])
            {
                QApplication app(argc, argv);
            
                qmlRegisterType<QmlQwtPlotter>("QmlQwt",1,0,"QmlQwtPlotter");
            
                QQmlApplicationEngine engine;
                engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
            
                return app.exec();
            }
            
            /* main.qml */
            
            import QtQuick 2.5
            import QtQuick.Window 2.2
            import QtCharts 2.0
            import QmlQwt 1.0
            
            Window {
                visible: true
                width: 640
                height: 320
            
                QmlQwtPlotter {
                    anchors.fill: parent // !!!!
                }
            }
            

            The application can't be executed - the runtime error is appeared:

            ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread. Current thread 0x0x3a050108. Receiver '' (of type 'QwtPlot') was created in thread 0x0x3fc728", file kernel\qcoreapplication.cpp, line 589
            

            The error is disappeared if I comment strings qwtPlot->render(painter) in qmlqwtplotter.cpp or anchors.fill: parent in main.qml - just painting the white empty window.

            The most strange thing is that the problem exists on Windows 8.1 and doesn't exist on Linux Mint 18.2 (same versions of Qt and Qt Creator).

            What can it be?

            D Offline
            D Offline
            Diracsbracket
            wrote on 13 Jun 2020, 09:00 last edited by Diracsbracket
            #5

            @mdma2
            I have the same problem where I use a QQuickPaintedItem to render a QWidget-derived object: calling the QWidget's render() method inside the QQuickPaintedItem's paint() triggers the exception in Raspbian Buster (i.e. on my Raspberry Pi3B+), but not on Debian Buster (i.e. on my PC VM), both using Qt 5.15.

            This used to work on Raspbian Stretch however using earlier Qt releases.

            1 Reply Last reply
            0
            • T Offline
              T Offline
              Tuong
              wrote on 12 Oct 2020, 02:09 last edited by
              #6

              I have the same problem when I use a QQuickPaintedItem to render a QWidget with Qt5.15 iOS. Did you have a solution for this @Diracsbracket ?

              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