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. using alignment enum in an array
Forum Updated to NodeBB v4.3 + New Features

using alignment enum in an array

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 2 Posters 569 Views 2 Watching
  • 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 9 Jun 2021, 21:35 last edited by mzimmers 6 Sept 2021, 21:38
    #1

    Hi all -

    I have a multicolumn table used in several places. The caller can specify column widths by overriding some properties, which are defined as follows:

    property int cellWidth: 200 // or whatever
    
    property int widthCell0: cellWidth
    property int widthCell1: cellWidth
    property int widthCell2: cellWidth
    property variant columnWidths: [widthCell0, widthCell1, widthCell2]
    
    Repeater {
       Column {
          Rectangle {
             id: columnRect
             width: if (index < currentData.length) {
                      columnWidths[index]
                   } else {
                      cellWidth
                   }
    ...
    

    This works fine. But when I attempt to apply a similar solution for alignment, I get an error.

        property int aligncell0: Text.AlignLeft
        property int aligncell1: Text.AlignRight
        property int aligncell2: Text.AlignLeft
        property int columnAligns: [alignCell0, alignCell1, alignCell2]
    ...
    Text { // Text is a child of the Rectangle above.
       anchors.verticalCenter: parent.verticalCenter
       horizontalAlignment:
          columnAligns[index]
    ...
    
    

    If I make my array type variant, I get this error:

    file:///home/mzimmers/git/KOL-UI/src/lib/qml/SortedTable.qml:234: TypeError: Cannot read property '3' of undefined
    

    If I make my array type int, I don't get an error, but it doesn't work. Can someone explain what I'm doing wrong?

    Thanks...

    K 1 Reply Last reply 9 Jun 2021, 21:52
    0
    • M mzimmers
      9 Jun 2021, 21:35

      Hi all -

      I have a multicolumn table used in several places. The caller can specify column widths by overriding some properties, which are defined as follows:

      property int cellWidth: 200 // or whatever
      
      property int widthCell0: cellWidth
      property int widthCell1: cellWidth
      property int widthCell2: cellWidth
      property variant columnWidths: [widthCell0, widthCell1, widthCell2]
      
      Repeater {
         Column {
            Rectangle {
               id: columnRect
               width: if (index < currentData.length) {
                        columnWidths[index]
                     } else {
                        cellWidth
                     }
      ...
      

      This works fine. But when I attempt to apply a similar solution for alignment, I get an error.

          property int aligncell0: Text.AlignLeft
          property int aligncell1: Text.AlignRight
          property int aligncell2: Text.AlignLeft
          property int columnAligns: [alignCell0, alignCell1, alignCell2]
      ...
      Text { // Text is a child of the Rectangle above.
         anchors.verticalCenter: parent.verticalCenter
         horizontalAlignment:
            columnAligns[index]
      ...
      
      

      If I make my array type variant, I get this error:

      file:///home/mzimmers/git/KOL-UI/src/lib/qml/SortedTable.qml:234: TypeError: Cannot read property '3' of undefined
      

      If I make my array type int, I don't get an error, but it doesn't work. Can someone explain what I'm doing wrong?

      Thanks...

      K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 9 Jun 2021, 21:52 last edited by kshegunov 6 Sept 2021, 21:58
      #2

      @mzimmers said in using alignment enum in an array:

      property int columnAligns: [alignCell0, alignCell1, alignCell2]
      

      This stores the size of the array. So you're trying to use an int as an array so the QML engine chokes on it.

      property array columnAligns ...
      

      should fix it.

      Here's my favorite talk on that subject: https://www.destroyallsoftware.com/talks/wat

      PS. Also JS is case sensitive. aligncell0 != alignCell0

      Read and abide by the Qt Code of Conduct

      M 1 Reply Last reply 9 Jun 2021, 22:03
      1
      • K kshegunov
        9 Jun 2021, 21:52

        @mzimmers said in using alignment enum in an array:

        property int columnAligns: [alignCell0, alignCell1, alignCell2]
        

        This stores the size of the array. So you're trying to use an int as an array so the QML engine chokes on it.

        property array columnAligns ...
        

        should fix it.

        Here's my favorite talk on that subject: https://www.destroyallsoftware.com/talks/wat

        PS. Also JS is case sensitive. aligncell0 != alignCell0

        M Offline
        M Offline
        mzimmers
        wrote on 9 Jun 2021, 22:03 last edited by
        #3

        @kshegunov property array seems broken:

            property int alignCell0: Text.AlignLeft
            property int alignCell1: Text.AlignRight
            property int alignCell2: Text.AlignLeft
            property int alignCell3: Text.AlignLeft
            property int alignCell4: Text.AlignLeft
            property int alignCell5: Text.AlignLeft
            property array columnAligns: [alignCell0, alignCell1, alignCell2, alignCell3, alignCell4, alignCell5]
        

        Gives me:

        file:///home/mzimmers/git/KOL-UI/src/lib/qml/SortedTable.qml:42 Expected property type
        

        line 42 is the property array.

        Thanks for catching my Case typos...

        K 1 Reply Last reply 9 Jun 2021, 23:26
        0
        • M mzimmers
          9 Jun 2021, 22:03

          @kshegunov property array seems broken:

              property int alignCell0: Text.AlignLeft
              property int alignCell1: Text.AlignRight
              property int alignCell2: Text.AlignLeft
              property int alignCell3: Text.AlignLeft
              property int alignCell4: Text.AlignLeft
              property int alignCell5: Text.AlignLeft
              property array columnAligns: [alignCell0, alignCell1, alignCell2, alignCell3, alignCell4, alignCell5]
          

          Gives me:

          file:///home/mzimmers/git/KOL-UI/src/lib/qml/SortedTable.qml:42 Expected property type
          

          line 42 is the property array.

          Thanks for catching my Case typos...

          K Offline
          K Offline
          kshegunov
          Moderators
          wrote on 9 Jun 2021, 23:26 last edited by
          #4

          @mzimmers said in using alignment enum in an array:

          line 42 is the property array.

          Yes, I'm a dum-dum. Just use var is what I meant to write, but failed miserably.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          4
          • M Offline
            M Offline
            mzimmers
            wrote on 10 Jun 2021, 16:36 last edited by mzimmers 6 Oct 2021, 16:37
            #5

            Right you are (about var, not the dum-dum part). It works fine now. Thanks!

            I also changed my variant (in the first example) to var, as I discovered today that variant is obsolete. FWIW...

            1 Reply Last reply
            2

            1/5

            9 Jun 2021, 21:35

            • Login

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