Problems with Open-Source Downloads read https://www.qt.io/blog/problem-with-open-source-downloads and https://forum.qt.io/post/638946

QQmlListProperty, ListView and delegate



  • Hello everybody

    I make a QQmlListProperty<MagicSquare> and I would like to display the attribut 'label' from my class MagicSquare but it does not work. I think that my problem come from QML and not of Qt, so I do not show my code source execept if you need it.

    I have in my source code (magicSquareModel.h) :
    Q_PROPERTY ( QQmlListProperty<MagicSquare> magicSquareList READ getMagicSquareList NOTIFY magicSquareListChanged )

    We can acces to QQmlList thanks to : qmlContext->setContextProperty ( "magicSquareModel", m_magicSquareModel );

    So this is my code in Qml :

    ListView{

    width: parent.width
    height: parent.height
    model: magicSquareModel.magicSquareList
    clip: true
    
    delegate: SimulationMagicSquare{
        width: parent.width * 0.75
        height: virtualModeGraphicIdentity.buttonHeight
        anchors {
            horizontalCenter: parent.horizontalCenter
        }
    
        title: model.label
    
    }
    

    }
    my terminal returns : Unable to assign [undefined] to QString where there is title: model.label (title is just a Text{} )
    have you got an idea of the problem ?

    Thanks.


  • Qt Champions 2017

    magicSquareModel.magicSquareList is not a model. It is just a list property.



  • Thanks for you answer.

    But how can I see my attribut from my magicSquareList in the qml ?


  • Moderators

    @cosmoff said in QQmlListProperty, ListView and delegate:

    Thanks for you answer.

    But how can I see my attribut from my magicSquareList in the qml ?

    inside your delegate, you cann access the underlying data via modelData

    delegate: SimulationMagicSquare{
        width: parent.width * 0.75
        height: virtualModeGraphicIdentity.buttonHeight
        anchors {
            horizontalCenter: parent.horizontalCenter
        }
    
        title: modelData.label //here
    
    }
    


  • it does not work,

    the code is :

    ListView{
    
                    id:virtualModeList
                    width: parent.width
                    height: parent.height
                    model: virtualModeModel.virtualModeList
                    clip: true
    
                    delegate: SimulationButton{
                        width: parent.width * 0.75
                        height: virtualModeGraphicIdentity.buttonHeight
                        anchors {
                            horizontalCenter: parent.horizontalCenter
                        }
    
                        title: modelData.label
    
                    }
    
    
                }
    

    the terminal returns : Unable to assign [undefined] to QString


  • Qt Champions 2017

    What is the data you are trying to expose ? Is it that you have to do QQMLPropertyList ?



  • I make a :
    QQmlListProperty<MagicSquare> MagicSquareModel::getMagicSquareList()
    {
    return QQmlListProperty<MagicSquare>(this, &m_MagicSquareList,
    &MagicSquareModel::MagicSquareListCount,
    &MagicSquareModel::MagicSquareListAt);
    }

    in my class MagicSquare I have an attribut which is QString label and i want to display on the qml


  • Moderators

    @cosmoff said in QQmlListProperty, ListView and delegate:

    in my class MagicSquare I have an attribut which is QString label and i want to display on the qml

    that has to be a Q_PROPERTY or a function that you made available to QML via Q_INVOKABLE



  • Its= works! I forget to put a Q_PROPERTY for the attribut label...

    thanks for your help !


Log in to reply