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



  • 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?



  • @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?



  • @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?



  • @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().


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.