Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-65742

Application crashes on macOS when QMessageBox is shown from QML signal handler while Behavior/Animation is running

    XMLWordPrintable

    Details

    • Platform/s:
      macOS

      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. 

        Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

            Assignee:
            qt.team.quick.subscriptions Qt Quick and Widgets Team
            Reporter:
            GTDev Günther Teufl
            Votes:
            2 Vote for this issue
            Watchers:
            4 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:

                Gerrit Reviews

                There are no open Gerrit changes