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. Dynamically add a qml file to an existing qml file...
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 1 Posters 791 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.
  • E Offline
    E Offline
    embersyc
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • E Offline
      E Offline
      embersyc
      wrote on last edited by
      #2

      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/

      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