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. Create model QML Dialog from C++
Forum Updated to NodeBB v4.3 + New Features

Create model QML Dialog from C++

Scheduled Pinned Locked Moved QML and Qt Quick
qt5qtquickqml
8 Posts 2 Posters 8.3k Views 2 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.
  • P Offline
    P Offline
    PhTe
    wrote on last edited by
    #1

    I have a simple QML window with a table view in it.

    ...
    QQmlApplicationEngine engine;
    PropertyTable propertyTable(&engine);
    engine.rootContext()->setContextProperty("propertyModel", propertyTable.getModel());
    engine.load(QUrl("qrc:/main.qml"));
    QList<QObject*> temp = engine.rootObjects();
    
    QObject *topLevel = temp.value(0);
    QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);
    QObject *tableView = window->findChild<QObject*>("tableView1");
    QObject::connect(tableView, SIGNAL(rowDoubleClicked(int)), &propertyTable, SLOT(onRowDoubleClicked(int)));
    
    if ( !window ) {
        qWarning("Error: Your root item has to be a Window.");
        return -1;
    }
    
    // Display the main.qml
    window->show();
    ...
    

    This works so far. The window is shown and i get the doubleclick events in my PropertyTable class.
    But now i would like to start a modal QML dialog (for editing the table cells, show extended information and so on) from my C++ PropertyTable class.

    Can someone give me an advice how this is possible?
    Is there also a smaller way to show the main window instead of all the code above?

    p3c0P 1 Reply Last reply
    0
    • P PhTe

      I have a simple QML window with a table view in it.

      ...
      QQmlApplicationEngine engine;
      PropertyTable propertyTable(&engine);
      engine.rootContext()->setContextProperty("propertyModel", propertyTable.getModel());
      engine.load(QUrl("qrc:/main.qml"));
      QList<QObject*> temp = engine.rootObjects();
      
      QObject *topLevel = temp.value(0);
      QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);
      QObject *tableView = window->findChild<QObject*>("tableView1");
      QObject::connect(tableView, SIGNAL(rowDoubleClicked(int)), &propertyTable, SLOT(onRowDoubleClicked(int)));
      
      if ( !window ) {
          qWarning("Error: Your root item has to be a Window.");
          return -1;
      }
      
      // Display the main.qml
      window->show();
      ...
      

      This works so far. The window is shown and i get the doubleclick events in my PropertyTable class.
      But now i would like to start a modal QML dialog (for editing the table cells, show extended information and so on) from my C++ PropertyTable class.

      Can someone give me an advice how this is possible?
      Is there also a smaller way to show the main window instead of all the code above?

      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi @PhTe

      • Create a new QML file which will contain the modal Dialog.
      • Use QQmlComponent to load that QML file
      • Specify a parent to it using setParentItem

      157

      P 1 Reply Last reply
      0
      • p3c0P p3c0

        Hi @PhTe

        • Create a new QML file which will contain the modal Dialog.
        • Use QQmlComponent to load that QML file
        • Specify a parent to it using setParentItem
        P Offline
        P Offline
        PhTe
        wrote on last edited by
        #3

        Hi @p3c0

        Create a new QML file which will contain the modal Dialog.

        OK, i created a simple dialog:

        import QtQuick 2.0
        import QtQuick.Dialogs 1.2
        
        Dialog {
            title: "Test dialog"
            visible: true;
        
            contentItem: 
                Rectangle {
                    id: inputDialog
                    width: 320
                    height: 128
                    color: "#cdcdcd"
                    radius: 10
                    border.width: 1
        
                TextInput {
                    id: valueInput
                    x: 29
                    y: 30
                    width: 266
                    height: 20
                    text: qsTr("")
                    z: 1
                    font.pixelSize: 12
                }
            }
        }
        

        Use QQmlComponent to load that QML file

        I tried:

        QQmlComponent comp(engine, QUrl("qrc:/PropertiesComponentInputDialog.qml"),QQmlComponent::PreferSynchronous);
        if(comp.isReady()) {
            QQuickView *view = qobject_cast<QQuickView*>(comp.create());
            QQmlEngine::setObjectOwnership(view, QQmlEngine::CppOwnership);
            view->setParent((QQuickWindow*)engine->rootObjects().value(0));
            view->show();
        }
        else {
            qDebug() << "Dialog not loaded";
        }
        

        and some other variations, but i get a segmentation fault if i call setParent.

        Do you have an example code of how i can load and show an QQmlComponent as window/dialog? That would really, really help me.

        p3c0P 1 Reply Last reply
        0
        • P PhTe

          Hi @p3c0

          Create a new QML file which will contain the modal Dialog.

          OK, i created a simple dialog:

          import QtQuick 2.0
          import QtQuick.Dialogs 1.2
          
          Dialog {
              title: "Test dialog"
              visible: true;
          
              contentItem: 
                  Rectangle {
                      id: inputDialog
                      width: 320
                      height: 128
                      color: "#cdcdcd"
                      radius: 10
                      border.width: 1
          
                  TextInput {
                      id: valueInput
                      x: 29
                      y: 30
                      width: 266
                      height: 20
                      text: qsTr("")
                      z: 1
                      font.pixelSize: 12
                  }
              }
          }
          

          Use QQmlComponent to load that QML file

          I tried:

          QQmlComponent comp(engine, QUrl("qrc:/PropertiesComponentInputDialog.qml"),QQmlComponent::PreferSynchronous);
          if(comp.isReady()) {
              QQuickView *view = qobject_cast<QQuickView*>(comp.create());
              QQmlEngine::setObjectOwnership(view, QQmlEngine::CppOwnership);
              view->setParent((QQuickWindow*)engine->rootObjects().value(0));
              view->show();
          }
          else {
              qDebug() << "Dialog not loaded";
          }
          

          and some other variations, but i get a segmentation fault if i call setParent.

          Do you have an example code of how i can load and show an QQmlComponent as window/dialog? That would really, really help me.

          p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by p3c0
          #4

          @PhTe
          QQuickView *view = qobject_cast<QQuickView*>(comp.create());

          Cast it to QObject instead. Set modality to Qt.ApplicationModal instead of default Qt.WindowModal for the Dialog. It doesn’t work on some platforms.

          If it is crashing at setParent then first check whether the casting works and that object is really accessible.

          157

          P 1 Reply Last reply
          0
          • p3c0P p3c0

            @PhTe
            QQuickView *view = qobject_cast<QQuickView*>(comp.create());

            Cast it to QObject instead. Set modality to Qt.ApplicationModal instead of default Qt.WindowModal for the Dialog. It doesn’t work on some platforms.

            If it is crashing at setParent then first check whether the casting works and that object is really accessible.

            P Offline
            P Offline
            PhTe
            wrote on last edited by
            #5

            @p3c0
            If i cast it to QObject, how can i show the dialog? QObject has no function called show or similiar.

            p3c0P 1 Reply Last reply
            0
            • P PhTe

              @p3c0
              If i cast it to QObject, how can i show the dialog? QObject has no function called show or similiar.

              p3c0P Offline
              p3c0P Offline
              p3c0
              Moderators
              wrote on last edited by p3c0
              #6

              @PhTe I think you are misunderstanding the concept. Dialog is not a QML viewer its just a component. Following example might help:

              QQmlApplicationEngine engine;
              engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
              
              QQuickWindow *itm = qobject_cast<QQuickWindow*>(engine.rootObjects().value(0));
              
              QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:/MyDialog.qml")));
              QObject *childItem = qobject_cast<QObject*>(component.create());
              childItem->setParent(itm);
              

              As soon as it is create'ed it is shown.
              MyDialog.qml is same as your example. But it has modality set to Qt.ApplicationModal
              main.qml contains just the default Window component.

              157

              1 Reply Last reply
              0
              • P Offline
                P Offline
                PhTe
                wrote on last edited by
                #7

                Oh, ok. Now i get it :)
                It's simpler then i thought.

                As soon as it is create'ed it is shown.

                I didn't know that.

                Thank you

                p3c0P 1 Reply Last reply
                0
                • P PhTe

                  Oh, ok. Now i get it :)
                  It's simpler then i thought.

                  As soon as it is create'ed it is shown.

                  I didn't know that.

                  Thank you

                  p3c0P Offline
                  p3c0P Offline
                  p3c0
                  Moderators
                  wrote on last edited by
                  #8

                  @PhTe Well that too because you have visible set to true for Dialog.
                  You're Welcome. Happy Coding..

                  157

                  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