Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. show modal qml-dialog from c++
Forum Updated to NodeBB v4.3 + New Features

show modal qml-dialog from c++

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 1 Posters 1.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    themts
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • T Offline
      T Offline
      themts
      wrote on last edited by themts
      #2

      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.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved