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

Layouting QQuickItem



  • I have a main qml that defines areas in a layout, where Area1 and Area2 are defined in other qml in the same directory.

    Item{
    Row{
    Area1{}
    Area2{}
    }}

    In another class I have made a qml component, cast it to QQuickItem and returned the QQuickItem* pointer to the main class where I have loaded my main.qml in a QQuickView.
    How to add this QQuickItem to Area1 of my main qml.


  • Moderators

    Add objectName to your Row element, then in C++ search for it using QObject::findChild(), then insert your QQuickItem into the children list of that object.

    Note, however, that adding QML components manually from C++ is totally not recommended and unsupported.



  • @sierdzio
    Thanks, I will try this.

    What would you suggest I must use, if I am making qml components in different C++ libraries and want to merge them in a layout / stacked in a main application where I include these libraries?

    How , should I approach component based project development, since my project is really big and dividing it into components / libraries helps manage the project effectively.


  • Moderators

    • you can create your own QML modules
    • in your libraries you can create QRC resources with your QML files inside them, then load these use Q_INIT_RESOURCE in your main app


  • @sierdzio
    I was aware of existence of QML modules and loading resource files. But, I didnt use them because I was doing setContextProperty (to set QAbstractItemModel) to the qml in the library. And if I simply share the resource file it was giving me error " model not defined ". I m not sure if QML modules would be of any help here.


  • Moderators

    Hm indeed, that complicates things.

    You could, perhaps, add an API to your library(ies) that would take a context parameter and add properties there. Something like:

    class MyLibrary {
      public:
        registerProperties(QQmlContext *context) {
          if (context) {
            context->setContextProperty(m_model);
          }
        }
    
    };
    

    Pseudo code of course but hopefully you get the idea.



  • @sierdzio said in Layouting QQuickItem:

    Add objectName to your Row element, then in C++ search for it using QObject::findChild(), then insert your QQuickItem into the children list of that object.

    Note, however, that adding QML components manually from C++ is totally not recommended and unsupported.

    I used QObject::findChild() and was able to get the children but was unable to add my QQuickItem to this list of children.
    Can I get an example code or a link on how to do this ?


  • Moderators

    @Luckless said in Layouting QQuickItem:

    @sierdzio said in Layouting QQuickItem:

    Add objectName to your Row element, then in C++ search for it using QObject::findChild(), then insert your QQuickItem into the children list of that object.

    Note, however, that adding QML components manually from C++ is totally not recommended and unsupported.

    I used QObject::findChild() and was able to get the children but was unable to add my QQuickItem to this list of children.
    Can I get an example code or a link on how to do this ?

    Oh, I had something working but can't find the code now :/

    It should be enough to call setParent() on your child and give it the object you have found. Then it may be necessary to force the parent to redraw / mark it dirty.



  • @sierdzio
    It works now. It was actually a silly mistake coz of which I wasn't getting desired output. Redraw wasnt necessary.
    Thanks


Log in to reply