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. Layouting QQuickItem
Qt 6.11 is out! See what's new in the release blog

Layouting QQuickItem

Scheduled Pinned Locked Moved Solved QML and Qt Quick
9 Posts 2 Posters 1.2k 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.
  • L Offline
    L Offline
    Luckless
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      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.

      (Z(:^

      L 2 Replies Last reply
      2
      • sierdzioS sierdzio

        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.

        L Offline
        L Offline
        Luckless
        wrote on last edited by Luckless
        #3

        @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.

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4
          • 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

          (Z(:^

          L 1 Reply Last reply
          0
          • sierdzioS sierdzio
            • 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
            L Offline
            L Offline
            Luckless
            wrote on last edited by Luckless
            #5

            @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.

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              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.

              (Z(:^

              1 Reply Last reply
              0
              • sierdzioS sierdzio

                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.

                L Offline
                L Offline
                Luckless
                wrote on last edited by
                #7

                @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 ?

                sierdzioS 1 Reply Last reply
                0
                • L Luckless

                  @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 ?

                  sierdzioS Offline
                  sierdzioS Offline
                  sierdzio
                  Moderators
                  wrote on last edited by
                  #8

                  @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.

                  (Z(:^

                  L 1 Reply Last reply
                  1
                  • sierdzioS sierdzio

                    @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.

                    L Offline
                    L Offline
                    Luckless
                    wrote on last edited by
                    #9

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

                    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