c++qtqmlqlistqproperty

How to access Q_PROPERTY of QStringlist from Q_PROPERTY of MyClass * in qml?


I'm facing issue with the following code where I want to access a qstringlist from q_property of the class pointer containing the qstringlist. I have added all the code relevant to the problem.

player.hpp

This class is for players which will be created


class Player : public QObject {
    Q_OBJECT
    Q_PROPERTY(QStringList score READ score WRITE setScore NOTIFY scoreChanged)

    QStringList m_score = {"9", "2", "3", "4", "5", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4"};
}

scoreinput.hpp

This class creates new players and append to the m_playerList.


class ScoreInput : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QList<Player *> playerList READ playerList WRITE setPlayerList NOTIFY playerListChanged)

    QList<Player *> m_playerList;
}

main.cpp

qmlRegisterType<ScoreInput>("Score", 1, 0, "Score");
qmlRegisterType<Player>("Player", 1, 0, "Player");

scoreinput.qml

Row{
                width: 4 * (0.15625) * item_ScorecardScreen2.width //200
                height: (0.618) * item_ScorecardScreen2.height //432
                Repeater{
                    id:repeater_Player
                    model: scoreInput.playerList

//                    model: 1
                    Component.onCompleted: {
                        console.log(repeater_Player.model)
                    }

                    Column {
                        id: column3
                        width: (0.15625) * item_ScorecardScreen2.width //200
                        height: (0.618) * item_ScorecardScreen2.height //432

                        Repeater{
                            id:repeater_3
                            model: modelData.score

                            Rectangle{
                                id:rect_Col3
                                width: (0.15625) * item_ScorecardScreen2.width //200
                                height: (0.103) * item_ScorecardScreen2.height //72
                                color: "#F2F2F2"
                                border.color: "#C5C5C5"
                                TextField {
                                    id: text3
                                    color: "#2A2A2A"
                                    //                            text: scorecard.player1Score[index]
                                    text: modelData
                                    placeholderText: "-"
                                    anchors.fill: parent
                                    font.pixelSize: (0.0121) * (item_ScorecardScreen2.width + item_ScorecardScreen2.height )
                                    horizontalAlignment: Text.AlignHCenter
                                    verticalAlignment: Text.AlignVCenter //24
                                    font.family: "Roboto"
                                    font.weight: Font.Medium
                                    maximumLength: 1
                                    readOnly: index == 18 ? true : false
                                    selectByMouse : readOnly
                                    validator: IntValidator {
                                        bottom:0
                                        top: 9
                                    }
                                    Component.onCompleted: {
                                        console.log(text3.text)
                                    }

                                    onEditingFinished: {
                                        //                                scorecard.player1Score[index] = text3.text
                                        modelData = text3.text
                                        console.log("Score Added")
                                    }
                                    inputMethodHints: Qt.ImhFormattedNumbersOnly
                                    background: Rectangle {
                                        anchors.fill: parent
                                        color: "#F2F2F2"
                                        border.color: "#C5C5C5"
                                    }
                                }
                            }
                        }
                    }
                }
            }

I'm getting only value at the first index only and not the other index. And I'm also not able to assign values for the score model. Screenshot of the app

I want to access and change the scores for different players.


Solution

  • I call c++ method setScore2 to assign values for the score model.

    I get value at each cell index,your code works fine.

    Here is my modified code:

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include<QDebug>
    class Player : public QObject {
        Q_OBJECT
        Q_PROPERTY(QStringList score READ score WRITE setScore NOTIFY scoreChanged)
    public:
        QStringList score(){return m_score;}
        void setScore(QStringList s){m_score=s;emit scoreChanged();qDebug()<<"test1:"<<m_score;}
        Q_INVOKABLE void setScore2(QString s,int i){m_score[i]=s;}
    Q_SIGNALS:
        void scoreChanged();
    private:
        QStringList m_score = {"9", "2", "3", "4", "5", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4"};
    };
    class ScoreInput : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(QList<Player *> playerList READ playerList WRITE setPlayerList NOTIFY playerListChanged)
    public:
        ScoreInput()
        {
            m_playerList.push_back(new Player());
            m_playerList.push_back(new Player());
        }
        QList<Player *> playerList(){return  m_playerList;}
        void setPlayerList( QList<Player *> s){m_playerList=s;emit playerListChanged();}
    Q_SIGNALS:
        void playerListChanged();
    private:
        QList<Player *> m_playerList;
    };
    #include "main.moc"
    int main(int argc, char *argv[])
    {
    #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    #endif
    
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        qmlRegisterType<ScoreInput>("Score", 1, 0, "Score");
        qmlRegisterType<Player>("Player", 1, 0, "Player");
        const QUrl url(QStringLiteral("qrc:/main.qml"));
        QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                         &app, [url](QObject *obj, const QUrl &objUrl) {
            if (!obj && url == objUrl)
                QCoreApplication::exit(-1);
        }, Qt::QueuedConnection);
        engine.load(url);
    
        return app.exec();
    }
    
    
    
    import QtQuick 2.14
    import QtQuick.Window 2.14
    import QtQuick3D 1.15
    import QtQuick.Controls 2.5
    import QtQuick.Layouts 1.12
    import QtQuick3D.Helpers 1.15
    import Qt.labs.qmlmodels 1.0
    import Score 1.0
    Window {
        Score{id:scoreInput}
        id: window
        width: 640
        height: 480
        visible: true
    
        Row {
            anchors.fill: parent
    
            Repeater{
                id:repeater_Player
                model: scoreInput.playerList
    
                // model: 1
                Component.onCompleted: {
                    console.log(repeater_Player.model)
                }
    
                Column {
                    property int playerindex: index
                    id: column3
                    width: 200
                    height:432
    
                    Repeater{
                        id:repeater_3
                        model: modelData.score
    
                        Rectangle{
                            id:rect_Col3
                            width: 200
                            height: 72
                            color: "#F2F2F2"
                            border.color: "#C5C5C5"
                            TextField {
                                id: text3
                                color: "#2A2A2A"
                                // text: scorecard.player1Score[index]
                                text: modelData
                                placeholderText: "-"
                                anchors.fill: parent
                                font.pixelSize: 15
                                horizontalAlignment: Text.AlignHCenter
                                verticalAlignment: Text.AlignVCenter //24
                                font.family: "Roboto"
                                font.weight: Font.Medium
                                maximumLength: 1
                                readOnly: index == 3 ? true : false
                                selectByMouse : readOnly
                                validator: IntValidator {
                                    bottom:0
                                    top: 9
                                }
                                Component.onCompleted: {
                                    console.log(text3.text)
                                }
    
                                onEditingFinished: {
                                    modelData = text3.text
                                    scoreInput.playerList[column3.playerindex].setScore2(text3.text,index)
                                    console.log("Score Added")
                                    console.log(scoreInput.playerList[column3.playerindex].score)
                                }
                                inputMethodHints: Qt.ImhFormattedNumbersOnly
                                background: Rectangle {
                                    anchors.fill: parent
                                    color: "#F2F2F2"
                                    border.color: "#C5C5C5"
                                }
                            }
                        }
                    }
                }
            }
        }
    }