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

setting arguments in reused QML file



  • Hi all -

    I've looked through the docs, but didn't find quite what I wanted. I have a QML file (SortedTable.qml) that is used by several displays. I need to modify it so the parent can specify the widths of the columns.

    Currently, the column widths are formatted like this:

     property int cellWidth: width / (viewModel.header().length + extraCols_)
     property real cellStretch0: cellWidth * 1.7
     property real cellStretch1: cellWidth * 0.4
     property real cellStretch2: cellWidth * 0.9
     property real cellStretch3: cellWidth
     property variant columnWidths: [cellStretch0, cellStretch1, cellStretch2, cellStretch3]
    

    And the property columnWidths is used in various places in SortedTable.

    I'd like to move the cellStretchN definitions out of SortedTable, into the parent files, but have a default value for them in SortedTable. I'm sure this can be done, but I need a pointer on how.

    Thanks...



  • hi @mzimmers
    If i understand the question correctly, there is nothig special to do here, you can let whatever default value in Sorted Table.qml and then reassign that value when you create SortedTable components



  • @LeLev here's what I tried:

    SortedTable.qml:

    Item {
      id: root
      property int cellWidth: width / (viewModel.header().length + extraCols_)
      property real cellStretch0: cellWidth
      property real cellStretch1: cellWidth
      property real cellStretch2: cellWidth
      property real cellStretch3: cellWidth
    

    and in the parent:

      SortedTable {
          property real cellStretch0: cellWidth * 1.7
          property real cellStretch1: cellWidth * 0.4
          property real cellStretch2: cellWidth * 0.9
          property real cellStretch3: cellWidth
    

    but it doesn't "take" (the values in the parent aren't used).



  • @mzimmers said in setting arguments in reused QML file:

    SortedTable {
    property real cellStretch0: cellWidth * 1.7

    you are re-declaring the properties, you just need to assing the new values like this

    SortedTable {
          cellStretch0: cellWidth * 1.7
         cellStretch1: cellWidth * 0.4
    

    but i'm not sure columnWidths will be updated in the component, you might need to reassign it



  • @LeLev thank you! This works fine. And the array does indeed update -- I guess it's created on-the-fly when the screen is invoked.

    Thanks again for the help.



  • @mzimmers You're welcome :)
    BTW "variant" qml type is obsolete. If possible, you should use "var" instead
    see here https://doc.qt.io/qt-5/qml-variant.html


Log in to reply