Analog of modal QDialog in QML



  • How can i do this only in QML without QDIalog?

    @
    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);

    QDialog dialog;
    QVBoxLayout layout(&dialog);
    QPushButton button1(QObject::tr("button1"), &dialog);
    QPushButton button2(QObject::tr("button2"), &dialog);
    
    layout.addWidget(&button1);
    layout.addWidget(&button2);
    
    QString windowPath;
    
    QObject::connect(&button1, &QPushButton::clicked, [&dialog, &windowPath] {
        dialog.accept();
        windowPath = QStringLiteral("qrc:///window1.qml");
    });
    
    QObject::connect(&button2, &QPushButton::clicked, [&dialog, &windowPath] {
        dialog.accept();
        windowPath = QStringLiteral("qrc:///window2.qml");
    });
    
    if (dialog.exec() == QDialog::Rejected)
        return 0;
    
    QQmlApplicationEngine engine;
    engine.load(QUrl(windowPath));
    
    return app.exec();
    

    }
    @



  • My guess would be you could create a "QML Window":http://qt-project.org/doc/qt-5/qml-qtquick-window-window.html and set its "modality property":http://qt-project.org/doc/qt-5/qml-qtquick-window-window.html#modality-prop to one of Qt.WindowModal or Qt.ApplicationModal. I've never done more than one-window QML myself though.



  • The question is fuzzy: do you mean

    "how do I embed a QML dialog in a C++ app?"

    or "how do I get the same blocking semantics as an exec() call?"

    If you mean the former, then read the docs under "embedding" or "interfacing C++ to QML." And then you can embed a QML Window or QuickControl Dialog.

    If you mean the latter, its not as easy as setting the modality property of the QML Window or Dialog:

    bq. Modality does not mean that there are any blocking calls to wait for the dialog to be accepted or rejected: only that the user will be prevented from interacting with the parent window or the application windows until the dialog is dismissed.

    In other words, after your embedding C++ code invokes the dialog (say by sending a signal to a delegate that calls QML Dialog.open()), I think your embedding code (your C++ event loop) continues, it doesn't wait for the QML dialog to be accepted or canceled by the user.

    In other words, there is no equivalent in QML to the QDialog.exec() C++ call. To get the same semantics, you must connect the accepted/canceled signals. I could be wrong.



  • The question is fuzzy: do you mean

    "how do I embed a QML dialog in a C++ app?"

    or "how do I get the same blocking semantics as an exec() call?"

    If you mean the former, then read the docs under "embedding" or "interfacing C++ to QML." And then you can embed a QML Window or QuickControl Dialog.

    If you mean the latter, its not as easy as setting the modality property of the QML Window or Dialog:

    bq. Modality does not mean that there are any blocking calls to wait for the dialog to be accepted or rejected: only that the user will be prevented from interacting with the parent window or the application windows until the dialog is dismissed.

    In other words, after your embedding C++ code invokes the dialog (say by sending a signal to a delegate that calls QML Dialog.open()), I think your embedding code (your C++ event loop) continues, it doesn't wait for the QML dialog to be accepted or canceled by the user.

    In other words, there is no equivalent in QML to the QDialog.exec() C++ call. To get the same semantics, you must connect the accepted/canceled signals. I could be wrong.



  • I just read "this post":http://www.qtcentre.org/archive/index.php/t-41193.html?s=4f0051811b94e81a668de909e4c9df15, which suggested that to get the blocking semantics you should implement a QDialog with blocking semantics, having your QML as the contents of the QDialog. That might give the blocking semantics, but I don't think it would give you the benefit of a native dialog look-and-feel that QML Dialog ought to give you.



  • I just read "this post":http://www.qtcentre.org/archive/index.php/t-41193.html?s=4f0051811b94e81a668de909e4c9df15, which suggested that to get the blocking semantics you should implement a QDialog with blocking semantics, having your QML as the contents of the QDialog. That might give the blocking semantics, but I don't think it would give you the benefit of a native dialog look-and-feel that QML Dialog ought to give you.


Log in to reply
 

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