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 563 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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by mzimmers
    #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...

    kshegunovK 1 Reply Last reply
    0
    • mzimmersM mzimmers

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

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by kshegunov
      #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

      mzimmersM 1 Reply Last reply
      1
      • kshegunovK kshegunov

        @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

        mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on 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...

        kshegunovK 1 Reply Last reply
        0
        • mzimmersM mzimmers

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

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on 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
          • mzimmersM Offline
            mzimmersM Offline
            mzimmers
            wrote on last edited by mzimmers
            #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

            • Login

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