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

QDeclarativeItem creation observing

Scheduled Pinned Locked Moved QML and Qt Quick
13 Posts 3 Posters 6.3k 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.
  • F Offline
    F Offline
    feldifux
    wrote on last edited by
    #1

    Hi,
    is there a way to observe when a new declarative item was dynamically created with Qt.createComponent()?
    I would like to track these changes, similar to what QDeclarativeViewInspector or QDeclarativeDebugService does, but I could not find a public API that allows me to achieve it.

    One approach I tried was listening to childrenChanged signal, but the problem there is it gets called before the properties of the created item are valid, and since componentComplete() is not a public function I could not determine whether the item was successfully created or not.

    I know I could implement own items derived from QDeclarativeItem and then override the protected function itemChanged and responding to ItemChildAddedChange, but I would prefer a solution without custom items just introduced for the introspection purpose.

    I appreciate your help,
    cheers

    Founder of Felgo SDK - http://felgo.com/qt

    Felgo simplifies

    • Mobile App Dev with Qt esp. iOS & Android
    • Game Development with Qt

    What others say

    Felgo scored #1 in Cross-Platform App Development Tools Report - see why: https://goo.gl/rgp3rq

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dmcr
      wrote on last edited by
      #2

      hello,

      In the documentation componentComplete () is tagged as pure virtual, so if you instancied it, didn't you get what you need?

      dmcr

      dmcr

      1 Reply Last reply
      0
      • F Offline
        F Offline
        feldifux
        wrote on last edited by
        #3

        No I dont, because it is only usable for custom items derived from QDeclarativeItem where you could overwrite componentComplete() as you have full control of it. But since I cant change the source of the provided qml items (like QDeclarativeImage), componentComplete() cant be used for observing non-custom items in the qml tree.

        chris

        Founder of Felgo SDK - http://felgo.com/qt

        Felgo simplifies

        • Mobile App Dev with Qt esp. iOS & Android
        • Game Development with Qt

        What others say

        Felgo scored #1 in Cross-Platform App Development Tools Report - see why: https://goo.gl/rgp3rq

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dmcr
          wrote on last edited by
          #4

          Ha yes you are right :)
          On solution could be to put some code in Component.onCompleted :.....inside the component dynamicly created, but i imagine this is the same problem for you, you want to do this for all items.

          dmcr

          1 Reply Last reply
          0
          • F Offline
            F Offline
            feldifux
            wrote on last edited by
            #5

            you're right ;)

            Founder of Felgo SDK - http://felgo.com/qt

            Felgo simplifies

            • Mobile App Dev with Qt esp. iOS & Android
            • Game Development with Qt

            What others say

            Felgo scored #1 in Cross-Platform App Development Tools Report - see why: https://goo.gl/rgp3rq

            1 Reply Last reply
            0
            • F Offline
              F Offline
              feldifux
              wrote on last edited by
              #6

              ... I am looking for a non-intrusive way to observe existing declarative items, so without having to introduce custom-derived QDeclarativeItem classes and no custom components written in QML (where I could use your suggestion with onCompleted, which is basically the same like overriding componentComplete() in C++ classes).

              Founder of Felgo SDK - http://felgo.com/qt

              Felgo simplifies

              • Mobile App Dev with Qt esp. iOS & Android
              • Game Development with Qt

              What others say

              Felgo scored #1 in Cross-Platform App Development Tools Report - see why: https://goo.gl/rgp3rq

              1 Reply Last reply
              0
              • F Offline
                F Offline
                feldifux
                wrote on last edited by
                #7

                After further investigation, I can imagine to get this working by connecting the Component.onCompleted signal handler every time a childrenChanged-signal is received from any observed item in the qml tree.. But how can I connect the signal onCompleted from the attached component "Component" from C++ with a slot of my choice?

                As this works:
                @Item {
                Component.onCompleted: ...
                }@

                is there a way to call something like this:

                @connect(declarativeItem, SIGNAL(?what goes in here - something like Component::onCompleted()?), observingObject, SLOT(myDeclarativeItemCompletedSlot());@

                Founder of Felgo SDK - http://felgo.com/qt

                Felgo simplifies

                • Mobile App Dev with Qt esp. iOS & Android
                • Game Development with Qt

                What others say

                Felgo scored #1 in Cross-Platform App Development Tools Report - see why: https://goo.gl/rgp3rq

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  feldifux
                  wrote on last edited by
                  #8

                  Connecting the completed-signal of the attached component of a QDeclarativeItem works like this:
                  1.) Iterate over all children of the item and find the one inheriting QDeclarativeComponentAttached
                  2.) connect the completed-signal with any other slot you like

                  So something like this:
                  @
                  QObjectList objectList = children();
                  for(int j=0;j<objectList.length(); j++) {
                  QObject *child = objectList.at(j);
                  qDebug() << "child" << child->objectName() << ":" << child;
                  if(child->inherits("QDeclarativeComponentAttached")) {

                          QObject *attachedComponent = child;
                  
                          qDebug() << "attachedComponent:" << attachedComponent << "of child:" << child->objectName();
                          
                          bool connectionSuccess = connect( attachedComponent, SIGNAL(completed()), this, SLOT(childCompleted()) );@
                  

                  The order the custom slot childCompleted() gets called, is AFTER the JavaScript handler code in Component.onCompleted: {...} was executed

                  Founder of Felgo SDK - http://felgo.com/qt

                  Felgo simplifies

                  • Mobile App Dev with Qt esp. iOS & Android
                  • Game Development with Qt

                  What others say

                  Felgo scored #1 in Cross-Platform App Development Tools Report - see why: https://goo.gl/rgp3rq

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    jombi
                    wrote on last edited by
                    #9

                    Hello,

                    I'm looking for similar way for the attached properties, to get notified when the attachee is completed. Searching the forum I found your solution, however this works only when your component has "Component.onCompleted:" declared.

                    Is there any other way to check whether a component is completed from dynamically created items or from attached properties?

                    Thanks!
                    Cheers

                    1 Reply Last reply
                    0
                    • F Offline
                      F Offline
                      feldifux
                      wrote on last edited by
                      #10

                      Hi,
                      not that I'm aware of - I was not aware that it only works when Component.onCompleted() is declared; appearently, I always declared it..

                      What you could try as well, is this approach:
                      Call qmlRegisterType() from C++ and register your own Item under the same name you want to track for completion. I.e. when you want to observe all "Item" components, create an own class inheriting from QDeclarativeItem and register that under the name "Item". That way, your QDeclarativeItem gets created whenever an Item appears in QML, and you can overwrite componentComplete.

                      However, that approach is not possible with private classes like QDeclarativeImage or QDeclarativeRectangle.

                      Does that help for your use-case?

                      If you find another approach, you're very welcome to share it.

                      cheers

                      Founder of Felgo SDK - http://felgo.com/qt

                      Felgo simplifies

                      • Mobile App Dev with Qt esp. iOS & Android
                      • Game Development with Qt

                      What others say

                      Felgo scored #1 in Cross-Platform App Development Tools Report - see why: https://goo.gl/rgp3rq

                      1 Reply Last reply
                      0
                      • J Offline
                        J Offline
                        jombi
                        wrote on last edited by
                        #11

                        Well, I tried that, the problem is that I need to catch completion of every attachee, like Text, Rectangle, etc. I also tried to derive my object from QQmlParserStatus, but these attached objects do not get the componentComplete() called.

                        I'll keep hunting, till then will use your solution :) And let you know if I find something.

                        cheers!

                        1 Reply Last reply
                        0
                        • F Offline
                          F Offline
                          feldifux
                          wrote on last edited by
                          #12

                          well, the pain here is that the order of the completion-calls is not defined in QML. Regarding the doc:
                          bq. The order of running the onCompleted scripts is undefined.

                          However, what this means is that within the same Qt version the order IS defined, but that may change with future Qt versions (I did not check on Qt5 for example, if it changed there).

                          however, the C++ componentComplete order of the base item is the last one called, i.e. all children got called C++ their componentComplete() before and are fully initialized. With QML's Component.onCompleted() it's the other way around:
                          base, child2, subchild2, child1, subchild1

                          with this test scenario:

                          @
                          Item {
                          id: base

                          Item {
                          id: child1
                          Item { id: subchild1 }
                          }

                          Item {
                          id: child2
                          Item { id: subchild2 }
                          }
                          }
                          @

                          So it depends whether you depend on initialization in Component.onComplete - if you are not, you can just use the C++ componentComplete() function.

                          Founder of Felgo SDK - http://felgo.com/qt

                          Felgo simplifies

                          • Mobile App Dev with Qt esp. iOS & Android
                          • Game Development with Qt

                          What others say

                          Felgo scored #1 in Cross-Platform App Development Tools Report - see why: https://goo.gl/rgp3rq

                          1 Reply Last reply
                          0
                          • J Offline
                            J Offline
                            jombi
                            wrote on last edited by
                            #13

                            Luckily my attached properties do not depend on children initialization, so the solution is good enough at the moment. All I need to know is when the attachee reaches the point of being initialized, so I can trigger the logic I have in the attached property.

                            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