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. change property of Component inside Loader
Forum Updated to NodeBB v4.3 + New Features

change property of Component inside Loader

Scheduled Pinned Locked Moved Solved QML and Qt Quick
componentloader
5 Posts 2 Posters 1.4k 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.
  • J Offline
    J Offline
    Jmueller
    wrote on 7 Sept 2021, 09:34 last edited by
    #1

    i want to place a list of buttons on the screen.
    to avoid double code i read that i can use Loader contaminated with Components.
    now i had the problem to modify property of the Component.

    item {
       /*first button*/
        Loader {
                id: secondButton
                sourceComponent: slotView
                ......
        }
        /*second button*/
        Loader {
                id: secondButton
                anchors.bottomMargin: 5
                anchors.bottom: firstButton.top
                sourceComponent: slotView
                /*try something like textSlotView.text="second Button Text"*/
         }
    
        /*general Slot*/
        Component{
            id: slotView
            Rectangle {
                  id: rectInside
                  height: 50
                  width: 220
                  color: "#49585f"
                  Text {
                       id: textSlotView
                       anchors {
                                  left: rectInside.left
                                  leftMargin: 20
                                  verticalCenter: rectInside.verticalCenter
                        }
                        color: "white"
                        font.pixelSize: 20
                        text: ""
                   }
              }
        }
    }
    

    so the question is, how can i modify property like text our height from the Loader component?

    J 1 Reply Last reply 7 Sept 2021, 09:36
    0
    • J Jmueller
      7 Sept 2021, 09:34

      i want to place a list of buttons on the screen.
      to avoid double code i read that i can use Loader contaminated with Components.
      now i had the problem to modify property of the Component.

      item {
         /*first button*/
          Loader {
                  id: secondButton
                  sourceComponent: slotView
                  ......
          }
          /*second button*/
          Loader {
                  id: secondButton
                  anchors.bottomMargin: 5
                  anchors.bottom: firstButton.top
                  sourceComponent: slotView
                  /*try something like textSlotView.text="second Button Text"*/
           }
      
          /*general Slot*/
          Component{
              id: slotView
              Rectangle {
                    id: rectInside
                    height: 50
                    width: 220
                    color: "#49585f"
                    Text {
                         id: textSlotView
                         anchors {
                                    left: rectInside.left
                                    leftMargin: 20
                                    verticalCenter: rectInside.verticalCenter
                          }
                          color: "white"
                          font.pixelSize: 20
                          text: ""
                     }
                }
          }
      }
      

      so the question is, how can i modify property like text our height from the Loader component?

      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 7 Sept 2021, 09:36 last edited by
      #2

      @Jmueller said in change property of Component inside Loader:

      so the question is, how can i modify property like text our height from the Loader component?

      via the item property of the loader
      https://doc.qt.io/qt-5/qml-qtquick-loader.html#item-prop


      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
      0
      • J Offline
        J Offline
        Jmueller
        wrote on 8 Sept 2021, 06:48 last edited by
        #3

        I am not sure how to use the item:QtObject

        i try:

            Loader {
                    id: secondButton
                    anchors.bottomMargin: 5
                    anchors.bottom: firstButton.top
                    sourceComponent: slotView
                   item.label: "second Button Text"            
             }
        
        /*general Slot*/
            Component{
                id: slotView
               property string label;
                Rectangle {
                      id: rectInside
                      height: 50
                      width: 220
                      color: "#49585f"
                      Text {
                           id: textSlotView
                           anchors {
                                      left: rectInside.left
                                      leftMargin: 20
                                      verticalCenter: rectInside.verticalCenter
                            }
                            color: "white"
                            font.pixelSize: 20
                            text: slotView.label
                       }
                  }
            }
        

        but Qt told me that "label" is not part of the QtObject

        J 1 Reply Last reply 8 Sept 2021, 06:57
        0
        • J Jmueller
          8 Sept 2021, 06:48

          I am not sure how to use the item:QtObject

          i try:

              Loader {
                      id: secondButton
                      anchors.bottomMargin: 5
                      anchors.bottom: firstButton.top
                      sourceComponent: slotView
                     item.label: "second Button Text"            
               }
          
          /*general Slot*/
              Component{
                  id: slotView
                 property string label;
                  Rectangle {
                        id: rectInside
                        height: 50
                        width: 220
                        color: "#49585f"
                        Text {
                             id: textSlotView
                             anchors {
                                        left: rectInside.left
                                        leftMargin: 20
                                        verticalCenter: rectInside.verticalCenter
                              }
                              color: "white"
                              font.pixelSize: 20
                              text: slotView.label
                         }
                    }
              }
          

          but Qt told me that "label" is not part of the QtObject

          J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 8 Sept 2021, 06:57 last edited by
          #4

          ok @Jmueller

          to ways to modify the property like you want it to:

          1. Passing on/accessing a property part of the Loader:
          Loader {
                  id: secondButton
                  anchors.fill: parent
                  sourceComponent: slotView
                  property string label:" second Button Text"
              }
          
              /*general Slot*/
              Component{
                  id: slotView
                  Rectangle {
                      id: rectInside
                      height: 50
                      width: 220
                      color: "#49585f"
                      Text {
                          id: textSlotView
                          anchors {
                              left: rectInside.left
                              leftMargin: 20
                              verticalCenter: rectInside.verticalCenter
                          }
                          color: "white"
                          font.pixelSize: 20
                          text: label
                      }
                  }
              }
          

          2.Using manual binding

          Loader {
                  id: secondButton
                  anchors.fill: parent
                  sourceComponent: slotView
                  Binding{
                      target: secondButton.item
                      when:secondButton.status == Loader.Ready
                      property: "label"
                      value: "second Button Text"
                  }
              }
          
              /*general Slot*/
              Component{
                  id: slotView
                  Rectangle {
                      id: rectInside
                      height: 50
                      width: 220
                      color: "#49585f"
                      property string label;
                      Text {
                          id: textSlotView
                          anchors {
                              left: rectInside.left
                              leftMargin: 20
                              verticalCenter: rectInside.verticalCenter
                          }
                          color: "white"
                          font.pixelSize: 20
                          text: parent.label
                      }
                  }
              }
          

          there are more ways, but this should get you started, I think


          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
          0
          • J Offline
            J Offline
            Jmueller
            wrote on 8 Sept 2021, 07:35 last edited by
            #5

            Thank you.

            That seems to be suitable for me :)

            1 Reply Last reply
            1

            2/5

            7 Sept 2021, 09:36

            • Login

            • Login or register to search.
            2 out of 5
            • First post
              2/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved