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

using alignment enum in an array



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


  • Moderators

    @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



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


  • Moderators

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



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


Log in to reply