Details
-
Bug
-
Resolution: Invalid
-
Not Evaluated
-
None
-
5.10.0
-
None
-
Qt 5.10, macOS High Sierra (10.13.2)
Description
When mixing C++ and QML, triggering a QMessageBox from QML crashes the application in a certain use case:
- C++ defines a custom class MyGlobalObject, which offers the openMessageBox slot to show a QMessageBox
- MyGlobalObject instance is registered to QML root context with setContextProperty
- In QML, the application crashes if myGlobalObject.openMessageBox is triggered in a Changed-Handler while also a Behavior/Animation is ongoing
QML Code:
ApplicationWindow { visible: true width: 640 height: 480 title: qsTr("Hello World") Component.onCompleted: { rect.opacity = 1 } Rectangle { id :rect anchors.fill: parent color: "grey" opacity: 0 visible: opacity > 0 // this leads to a crash if the animation below is running and openMessageBox is executed onVisibleChanged: myGlobalObject.openMessageBox() // comment this behavior and the messagebox will work Behavior on opacity { NumberAnimation { duration: 1000 } } } }
main.cpp:
#include <QApplication> #include <QQmlApplicationEngine> #include <QQmlContext> #include <myglobalobject.h> int main(int argc, char *argv[]) { #if defined(Q_OS_WIN) QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); #endif QApplication app(argc, argv); QQmlApplicationEngine engine; // add context property for global object MyGlobalObject *obj = new MyGlobalObject(); engine.rootContext()->setContextProperty("myGlobalObject",obj); // run qml engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); if (engine.rootObjects().isEmpty()) return -1; return app.exec(); }
myglobalobject.h:
#ifndef MYGLOBALOBJECT_H #define MYGLOBALOBJECT_H #include <QObject> class MyGlobalObject : public QObject { Q_OBJECT public: explicit MyGlobalObject(QObject *parent = nullptr); signals: public slots: void openMessageBox(); }; #endif // MYGLOBALOBJECT_H
myglobalobject.cpp:
#include "myglobalobject.h"
#include <QMessageBox>
MyGlobalObject::MyGlobalObject(QObject *parent) : QObject(parent) { }
void MyGlobalObject::openMessageBox() {
QMessageBox msgBox;
msgBox.exec();
}
A quick fix would be highly appreciated, as we have similar functionality in our library, which may be used in any QML project of our customers.