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. setting arguments in reused QML file
Forum Update on Monday, May 27th 2025

setting arguments in reused QML file

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 2 Posters 450 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.
  • M Offline
    M Offline
    mzimmers
    wrote on 4 Feb 2021, 23:52 last edited by
    #1

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

    O 1 Reply Last reply 5 Feb 2021, 00:10
    0
    • M mzimmers
      4 Feb 2021, 23:52

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

      O Offline
      O Offline
      ODБOï
      wrote on 5 Feb 2021, 00:10 last edited by ODБOï 2 May 2021, 00:10
      #2

      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

      M 1 Reply Last reply 5 Feb 2021, 00:16
      0
      • O ODБOï
        5 Feb 2021, 00:10

        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

        M Offline
        M Offline
        mzimmers
        wrote on 5 Feb 2021, 00:16 last edited by
        #3

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

        O 1 Reply Last reply 5 Feb 2021, 00:20
        0
        • M mzimmers
          5 Feb 2021, 00:16

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

          O Offline
          O Offline
          ODБOï
          wrote on 5 Feb 2021, 00:20 last edited by ODБOï 2 May 2021, 00:21
          #4

          @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

          M 1 Reply Last reply 5 Feb 2021, 16:19
          4
          • O ODБOï
            5 Feb 2021, 00:20

            @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

            M Offline
            M Offline
            mzimmers
            wrote on 5 Feb 2021, 16:19 last edited by
            #5

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

            O 1 Reply Last reply 5 Feb 2021, 17:09
            1
            • M mzimmers
              5 Feb 2021, 16:19

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

              O Offline
              O Offline
              ODБOï
              wrote on 5 Feb 2021, 17:09 last edited by ODБOï 2 May 2021, 17:28
              #6

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

              1/6

              4 Feb 2021, 23:52

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved