0

我遇到了以下代码的问题,我想从包含 qstringlist 的类指针的 q_property 访问 qstringlist。我已经添加了与问题相关的所有代码。

播放器.hpp

此类适用于将要创建的玩家


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"};
}

分数输入.hpp

此类创建新玩家并附加到 m_playerList。


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

    QList<Player *> m_playerList;
}

主文件

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"
                                    }
                                }
                            }
                        }
                    }
                }
            }

我只在第一个索引而不是另一个索引上获得价值。而且我也无法为分数模型分配值。 应用截图

我想访问和更改不同玩家的分数。

4

1 回答 1

0

我调用 c++ 方法setScore2来为分数模型分配值。

我在每个单元格索引处都获得了价值,您的代码工作正常。

这是我修改后的代码:

#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"
                            }
                        }
                    }
                }
            }
        }
    }
}

于 2021-11-09T07:42:10.137 回答