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. Using one property from a qml file in another qml file
Forum Updated to NodeBB v4.3 + New Features

Using one property from a qml file in another qml file

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
9 Posts 4 Posters 2.9k 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
    JennyAug13
    wrote on last edited by
    #1

    @dheerendra

    I have a property called width in button.qml. I have to use this property width inside my component called control inside my multiControl.qml file. How can i use this property width in another qml file? Should i use property alias?

    1 Reply Last reply
    0
    • ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by ODБOï
      #2

      @JennyAug13 Hello,
      first of all you should not use reserved keywords if you create a property, and is't also better to don't call your button 'Button.qml' because QML has already Type named Button.

      //MyButton.qml

      Item{
        property real m_width : 0 
        
        Rectangle{
        width : m_width 
        }
      }
      

      use : MyButton{ m_width : 50 }

      or

      Item{
        property alias m_rec : rec
        
        Rectangle{
      id:rec
        width : 10
        }
      }
      

      MyButton{ m_rec.width : 50 }

      1 Reply Last reply
      1
      • J Offline
        J Offline
        JennyAug13
        wrote on last edited by JennyAug13
        #3

        Oh, it is not working, bit more complicated. I have the following lines of code in button.qml

        sliderHandle{
        id:button
        ...
        
        ...
        }
        

        I have my control.qml file as follows:

        Item{
        
        id: Control
        
        
        Item{
        
        }
        
        button{
        
        }
        
        }
        

        I have multiControl.qml as follows:

        Item{
        id:multiControl
        
        
        Control{
        
        
        }
        
        }
        

        Now, how can i use width property from button.qml file inside multiControl.qml.

        J.HilkJ 1 Reply Last reply
        0
        • J JennyAug13

          Oh, it is not working, bit more complicated. I have the following lines of code in button.qml

          sliderHandle{
          id:button
          ...
          
          ...
          }
          

          I have my control.qml file as follows:

          Item{
          
          id: Control
          
          
          Item{
          
          }
          
          button{
          
          }
          
          }
          

          I have multiControl.qml as follows:

          Item{
          id:multiControl
          
          
          Control{
          
          
          }
          
          }
          

          Now, how can i use width property from button.qml file inside multiControl.qml.

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

          Hi @JennyAug13
          to use a property outside of your QML file, it has to be defined in the root element

          The usual way to foward a property is indeed a property alias in the root item


          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
          • dheerendraD Offline
            dheerendraD Offline
            dheerendra
            Qt Champions 2022
            wrote on last edited by dheerendra
            #5

            You can't use width property of button in MultiControl directly & you should not. If you want to access, you define another property inside the Control.qml which is alias to width property of Button.

            Item{
            id: Control
                property alias jwidth : b1.width. 
                Item{
                }
                Button{
                 id : b1
                }
            }
            

            You can access jwidth inside the multi control.qml

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

            J 1 Reply Last reply
            1
            • dheerendraD dheerendra

              You can't use width property of button in MultiControl directly & you should not. If you want to access, you define another property inside the Control.qml which is alias to width property of Button.

              Item{
              id: Control
                  property alias jwidth : b1.width. 
                  Item{
                  }
                  Button{
                   id : b1
                  }
              }
              

              You can access jwidth inside the multi control.qml

              J Offline
              J Offline
              JennyAug13
              wrote on last edited by JennyAug13
              #6

              @dheerendra Yes, solved it, but how can i access mouseX from my button.qml in multiControl.qml...
              Can i use something like button.MouseArea.mouseX??
              Ah.. many interlinks and complications. I will try to solve it.

              1 Reply Last reply
              0
              • dheerendraD Offline
                dheerendraD Offline
                dheerendra
                Qt Champions 2022
                wrote on last edited by
                #7

                many interlinks and complications.

                Its is not many interlinks & complications. Framework is designed so beautifully such that you don't end in mess. In general as object oriented principles details of inner modules are not directly exposed outside. Now you should consider button as inner module. So from outside no body should directly access button. If you want to expose, expose what is required explicitly. Hope it clarifies.

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

                J 1 Reply Last reply
                0
                • dheerendraD dheerendra

                  many interlinks and complications.

                  Its is not many interlinks & complications. Framework is designed so beautifully such that you don't end in mess. In general as object oriented principles details of inner modules are not directly exposed outside. Now you should consider button as inner module. So from outside no body should directly access button. If you want to expose, expose what is required explicitly. Hope it clarifies.

                  J Offline
                  J Offline
                  JennyAug13
                  wrote on last edited by JennyAug13
                  #8

                  @dheerendra Oh ya..woww. if i want to expose from inner modules, is only way through properties? How can i expose the mouseX from the innner most button.qml to upper qmls. The mouseX is inside my onPressed signal handler of MouseArea of the button and has to be drawn to multiControls.qml level. Can i just take another property with the name startX or something like that and use it in multiControls.qml?

                  1 Reply Last reply
                  0
                  • dheerendraD Offline
                    dheerendraD Offline
                    dheerendra
                    Qt Champions 2022
                    wrote on last edited by
                    #9

                    You can define your own signals or expose it like property.

                    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