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. Read of delegate objectName
Forum Updated to NodeBB v4.3 + New Features

Read of delegate objectName

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
19 Posts 5 Posters 2.0k Views
  • 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.
  • G Offline
    G Offline
    Galilio
    wrote on last edited by
    #3

    I want to write a unittest for the QML file.
    I could access all objects using ObjectName and check the property.

    at delegate I can not use objectname and my question how can I access the property within this delegate?

    S 1 Reply Last reply
    0
    • IntruderExcluderI Offline
      IntruderExcluderI Offline
      IntruderExcluder
      wrote on last edited by
      #4

      As I said, in fact your delegate isn't exist like an any item. ListView will instantiate such items by demand, you can access only really existed items.

      G 1 Reply Last reply
      0
      • IntruderExcluderI IntruderExcluder

        As I said, in fact your delegate isn't exist like an any item. ListView will instantiate such items by demand, you can access only really existed items.

        G Offline
        G Offline
        Galilio
        wrote on last edited by
        #5

        @intruderexcluder
        How can I then test the property of this delegate with Unittest?

        1 Reply Last reply
        0
        • IntruderExcluderI Offline
          IntruderExcluderI Offline
          IntruderExcluder
          wrote on last edited by
          #6

          Not sure, but if you want to test only objectName property you can create dummy component and then create object from this component. Get delegate property of your ListView and then create object via QQmlComponent or Qt.createComponent depends from which side you want to test your delegate. Hope this can help.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            Galilio
            wrote on last edited by
            #7

            I really did not understand that exactly.
            can you please explain to me more exactly with an example?

            many thanks

            1 Reply Last reply
            0
            • IntruderExcluderI Offline
              IntruderExcluderI Offline
              IntruderExcluder
              wrote on last edited by
              #8

              In case of QML you can create Rectangle object directly from delegate property:

                  ListView {
                      id: view
                      objectName: "listview"
                      delegate: Rectangle { objectName: "delegaterect" }
                  }
              
                  Component.onCompleted: {
                      let d = view.delegate.createObject(null);
                      console.log(d.objectName);
                      d.Component.destruction.connect(() => { console.log("READY TO BE DESTRUCTED"); });
                      d.destroy();
                  }
              

              In case of C++ side code can be something like:

                  auto lv = engine.rootObjects()[0]->findChild<QObject*>("listview");
                  auto component = lv->property("delegate").value<QQmlComponent*>();
                  auto delegate = component->create();
                  qDebug() << delegate->objectName();
                  delegate->deleteLater();
              
              1 Reply Last reply
              0
              • G Galilio

                Hello everybody,

                I have something like this:
                QML:

                ListView{
                id: listViewId
                objectName:"listViewObj"
                ....
                delegate: Rectangle {
                	id: rectnagleId
                	objectName: "rectangleDelegate"
                	....
                	}
                }
                

                and how can i get the property from the delegate?

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #9

                @galilio said in Read of delegate objectName:

                ListView{
                id: listViewId
                objectName:"listViewObj"
                ....
                delegate: Rectangle {
                	id: rectnagleId
                	objectName: "rectangleDelegate"
                	....
                	}
                }
                
                //to get item and access the properies
                
                function getItemAtIndex(index){
                    var item = listViewId.itemAtIndex(index);
                    if(item){
                          console.log(item.objectName)
                    }
                }
                

                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                G 1 Reply Last reply
                0
                • J.HilkJ J.Hilk

                  @galilio said in Read of delegate objectName:

                  ListView{
                  id: listViewId
                  objectName:"listViewObj"
                  ....
                  delegate: Rectangle {
                  	id: rectnagleId
                  	objectName: "rectangleDelegate"
                  	....
                  	}
                  }
                  
                  //to get item and access the properies
                  
                  function getItemAtIndex(index){
                      var item = listViewId.itemAtIndex(index);
                      if(item){
                            console.log(item.objectName)
                      }
                  }
                  
                  G Offline
                  G Offline
                  Galilio
                  wrote on last edited by
                  #10

                  @j-hilk
                  my qt version is 5.9.1

                  J.HilkJ 1 Reply Last reply
                  0
                  • G Galilio

                    @j-hilk
                    my qt version is 5.9.1

                    J.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on last edited by J.Hilk
                    #11

                    @galilio fair enough.

                    alright, trick 17 (untested)

                    ListView{
                    id: listViewId
                    objectName:"listViewObj"
                    var testObject: undefined
                    signal assignToObject(var id)
                    ....
                    delegate: Rectangle {
                    	id: rectnagleId
                    	objectName: "rectangleDelegate"
                            Connections{
                                 target: listViewId
                                 onAssignToObject: if(index === id) listViewId.testObject = rectnagleId
                            }
                    	....
                    	}
                    }
                    //to get item and access the properies
                            onTestObjectChanged:{
                                if(testObject)
                                    console.log(testObject.objectName)
                            }
                    
                    

                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    1 Reply Last reply
                    1
                    • G Offline
                      G Offline
                      Galilio
                      wrote on last edited by
                      #12

                      @j-hilk said in Read of delegate objectName:

                      onAssignToObject: if(index === id) listViewId.testObject = rectnagleId

                      what is your id here
                      Thanks

                      J.HilkJ 1 Reply Last reply
                      0
                      • G Galilio

                        @j-hilk said in Read of delegate objectName:

                        onAssignToObject: if(index === id) listViewId.testObject = rectnagleId

                        what is your id here
                        Thanks

                        J.HilkJ Offline
                        J.HilkJ Offline
                        J.Hilk
                        Moderators
                        wrote on last edited by J.Hilk
                        #13

                        @galilio said in Read of delegate objectName:

                        @j-hilk said in Read of delegate objectName:

                        onAssignToObject: if(index === id) listViewId.testObject = rectnagleId

                        what is your id here
                        Thanks

                        the argument of the signal
                        signal assignToObject(var id)


                        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                        Q: What's that?
                        A: It's blue light.
                        Q: What does it do?
                        A: It turns blue.

                        1 Reply Last reply
                        1
                        • G Offline
                          G Offline
                          Galilio
                          wrote on last edited by
                          #14
                          This post is deleted!
                          1 Reply Last reply
                          0
                          • G Offline
                            G Offline
                            Galilio
                            wrote on last edited by
                            #15

                            Sorry:
                            nothing happens
                            Slot was not called

                            J.HilkJ 1 Reply Last reply
                            0
                            • G Galilio

                              Sorry:
                              nothing happens
                              Slot was not called

                              J.HilkJ Offline
                              J.HilkJ Offline
                              J.Hilk
                              Moderators
                              wrote on last edited by
                              #16

                              @galilio
                              works fine in my test file

                              import QtQuick 2.12
                              import QtQuick.Window 2.12
                              import QtQuick.Controls 2.0
                              
                              Window {
                                  visible: true
                                  width: 400
                                  height: 750
                                  title: qsTr("Hello World")
                              
                                  id: root
                                  property int testIndex: 0
                                  Timer{
                                      running: true
                                      interval: 1000
                                      repeat: true
                                      onTriggered: {
                                          listViewId.assignToObject(root.testIndex)
                                          testIndex++
                                          if(testIndex >= 10)
                                              testIndex = 0
                                      }
                                  }
                              
                                  ListView{
                                      id: listViewId
                                      anchors.fill: parent
                                      objectName:"listViewObj"
                                      property var testObject: undefined
                                      onTestObjectChanged:{
                                          if(testObject)
                                              console.log(testObject.objectName)
                                      }
                                      signal assignToObject(var id)
                              
                                      model: 10
                                      spacing: 2
                              
                                      delegate: Rectangle {
                                          id: rectnagleId
                                          objectName: "rectangleDelegate" + index
                                          Text {
                                              text: modelData
                                              anchors.centerIn: parent
                                          }
                                          width: listViewId.width
                                          height:  20
                                          color: "green"
                                          Connections{
                                              target: listViewId
                                              onAssignToObject: if(index === id) listViewId.testObject = rectnagleId
                                          }
                                      }
                                  }
                              }
                              

                              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                              Q: What's that?
                              A: It's blue light.
                              Q: What does it do?
                              A: It turns blue.

                              1 Reply Last reply
                              3
                              • G Offline
                                G Offline
                                Galilio
                                wrote on last edited by
                                #17

                                @J-Hilk

                                and how is it if I want to test the delegate with unit test (in cpp) sorry

                                1 Reply Last reply
                                0
                                • G Galilio

                                  I want to write a unittest for the QML file.
                                  I could access all objects using ObjectName and check the property.

                                  at delegate I can not use objectname and my question how can I access the property within this delegate?

                                  S Offline
                                  S Offline
                                  Shivaraj
                                  wrote on last edited by
                                  #18

                                  @Galilio how do you access the objects using objectName can you share some code

                                  dheerendraD 1 Reply Last reply
                                  0
                                  • S Shivaraj

                                    @Galilio how do you access the objects using objectName can you share some code

                                    dheerendraD Offline
                                    dheerendraD Offline
                                    dheerendra
                                    Qt Champions 2022
                                    wrote on last edited by
                                    #19

                                    @Shivaraj

                                    Delegate you can't access using the object name. These are dynamically created object.

                                    Dheerendra
                                    @Community Service
                                    Certified Qt Specialist
                                    http://www.pthinks.com

                                    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