Using one property from a qml file in another qml file



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



  • @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 }



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



  • 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


  • Qt Champions 2017

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


  • Qt Champions 2017

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


  • Qt Champions 2017

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.