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

Dynamically add a qml file to an existing qml file...



  • Hello,

    Working on something for work where I need to build screens completely at runtime from prebuilt qml components:

    Trying to follow some examples I see, but so far things aren't quite working how I'd like. Here is my code:

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

    QQmlEngine engine;
    QQmlComponent blackRectComponent(&engine, QUrl::fromLocalFile("qml/GUIManager/BlackRectangle.qml"));
    QObject *blackRectObject = blackRectComponent.create();
    
    QQuickView view;
    view.setSource(QUrl::fromLocalFile("qml/GUIManager/main.qml"));
    
    QObject *mainStage = view.rootObject();
    blackRectObject->setParent(mainStage);
    
    view.show();
    
    return app.exec();
    

    }@

    The main.qml loads and displays correctly, all the code executes without error, but I never see my blackRectObject actually get added to the view.

    What I am doing wrong?

    Thanks



  • Never mind, figured it out on my own thanks to solution in another thread:

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

    QQuickView view;
    view.setSource(QUrl::fromLocalFile("qml/GUIManager/main.qml"));
    
    QQuickItem *mainStage = view.rootObject();
    
    QQmlComponent blackRectComponent(view.engine(), QUrl::fromLocalFile("qml/GUIManager/BlackRectangle.qml"));
    QObject *blackRectObject = blackRectComponent.create();
    QQuickItem *blackRectItem = qobject_cast<QQuickItem*>(blackRectObject);
    blackRectItem->setParentItem(mainStage);
    
    view.show();
    
    return app.exec();
    

    }@

    http://qt-project.org/forums/viewthread/31318/


Log in to reply