我遇到了以下代码的问题,我想从包含 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"
}
}
}
}
}
}
}
我只在第一个索引而不是另一个索引上获得价值。而且我也无法为分数模型分配值。 应用截图
我想访问和更改不同玩家的分数。