Important: Please read the Qt Code of Conduct -

Create model QML Dialog from C++

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

    QQmlApplicationEngine engine;
    PropertyTable propertyTable(&engine);
    engine.rootContext()->setContextProperty("propertyModel", propertyTable.getModel());
    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

    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?

  • Moderators

    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

  • 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;
            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);
    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.

  • Moderators

    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.

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

  • Moderators

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

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

    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.

  • 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

  • Moderators

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

Log in to reply