Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

show modal qml-dialog from c++



  • Hey guys,

    I want to show a modal qml dialog from c++.
    What I did:

    • created a qmlObject which represents my dialog
    • created a static c++ class and forward the object to it
    void NMessageBox::initMessageBox(QObject *dialogComponent)
    {
        NMessageBox::getInstance().m_dialogComponent = dialogComponent;
        QObject::connect(NMessageBox::getInstance().m_dialogComponent, SIGNAL(confirm(int)), &NMessageBox::getInstance(), SLOT(dialogConfirmed(int)));
    }
    
    • now I can call nShowMessage() which looks like this:
    MessageBoxResult nShowMessage(MessageBoxType messageBoxType, MessageBoxButtons buttons,
                                   const QString &title, const QString &message, const QString &description)
    {    
        QEventLoop loop;
        loop.connect(&NMessageBox::getInstance(), SIGNAL(dialogClosed()), SLOT(quit()));
        if (NMessageBox::getInstance().showMessage(messageBoxType, buttons, title, message, description) == 0)
            loop.exec();
    
        return NMessageBox::getInstance().lastResult();
    }
    
    • the show Message-function does this:
        QMetaObject::invokeMethod(m_dialogComponent, "show", Q_RETURN_ARG(QVariant, returnedValue),
                                  Q_ARG(QVariant, buttons.testFlag(MessageBoxButton::Ok)),
                                  Q_ARG(QVariant, buttons.testFlag(MessageBoxButton::Cancel)),
                                  Q_ARG(QVariant, buttons.testFlag(MessageBoxButton::No)),
                                  Q_ARG(QVariant, buttons.testFlag(MessageBoxButton::Yes)));
    

    Problem:
    Somehow this QEventLoop seems to be buggy:
    Lets say you press a button(a) which invokes a c++ function which would use my nShowMessage-function to show the dialog.
    Now the dialog shows up, but mouseclicks are somehow forwarded to my button(a), no matter where I click.
    When you click around like crazy it suddenly works again.

    As soon as I remove the QEventLoop everything works as expected.
    I hope someone can follow me as this problem is hard to debug and I cannot give you more information at this time.

    CU
    mts



  • Hey guys,

    I know this post is quite old but I still have this problem (QT 5.7 now).

    ->EDIT:
    ok, problem found.
    I didn't know that a c++ function is always called from qml as directConnection.
    That way I was interrupting my main message-loop. I had to add a helper function which I invoke with Qt::QueuedConnection.


Log in to reply