Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved setting arguments in reused QML file

    QML and Qt Quick
    2
    6
    192
    Loading More Posts
    • 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.
    • mzimmers
      mzimmers last edited by

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

      ODБOï 1 Reply Last reply Reply Quote 0
      • ODБOï
        ODБOï @mzimmers last edited by ODБOï

        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

        mzimmers 1 Reply Last reply Reply Quote 0
        • mzimmers
          mzimmers @ODБOï last edited by

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

          ODБOï 1 Reply Last reply Reply Quote 0
          • ODБOï
            ODБOï @mzimmers last edited by ODБOï

            @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

            mzimmers 1 Reply Last reply Reply Quote 4
            • mzimmers
              mzimmers @ODБOï last edited by

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

              ODБOï 1 Reply Last reply Reply Quote 1
              • ODБOï
                ODБOï @mzimmers last edited by ODБOï

                @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

                1 Reply Last reply Reply Quote 1
                • First post
                  Last post