Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Declaring and setting the properties in a qml file.



  • Hi,
    I am creating customised widgets that will be used in my application. So instead of exposing properties I will be setting the values of the
    properties via set Method.
    Ex:

    • MyWidget.qml
      Item{
      anchors.fill : parent
      QtObject{
      id:object
      property int rectWidth: 0
      }
      function setWidth(value)
      {
      object.rectWidth = value
      }
      Rectangle{
      width : object.rectWidth
      .
      }
      .
      .
      }
      -main.qml
      {
      MyWidget
      {
      width:100
      Component.onCompleted:
      {
      setWidth(20)
      }
      }
      }

    My query is: till the component creation is not completed the vital values would not be set. Am i right? and if we use the above approach will it have any disadvantage while setting up the properties of the widgets ?


  • Qt Champions 2017

    Any reason for using the methods to set the values ? Unless objects are created you will not be able to set the values. It is is better to work with Properties rather than function which looks procedural programming.



  • @dheerendra ,

    We were starting with implementation, So having functions were decided and keeping the properties as private. But setting the properties of the file after the component is completed, will it work similar way for all the QML components such as RowLayout, ColumnLayout as well?

    As the RowLayout/ ColumnLayout will be used as a part of Model view and delegate, and all the properties of it (width, height,preferred height etc) will be set after the component is completed, so Will this also work in a similar way as setting up the properties?


  • Qt Champions 2017

    yes. It works as setting properties.


Log in to reply