gpt4 book ai didi

c++ - QML - 实现 C++ ListModel 的最佳方式

转载 作者:行者123 更新时间:2023-11-30 05:39:39 24 4
gpt4 key购买 nike

目前我有一个在 QML 中定义的 ListModel,每个 ListElement 中有许多不同的字段。委托(delegate)根据“myType”值以不同方式使用react。

ListView {
id: createLocalGameViewList
anchors.fill: parent
model: ListModel {
id: createLocalGameViewModel
ListElement {
myId: "comboBox_NumberOfPlayers"
myTitle: qsTr("Number of players")
myType: "ComboBox"
myValuesList: [
ListElement { value: "10" },
ListElement { value: "9" },
ListElement { value: "8" },
ListElement { value: "7" },
ListElement { value: "6" },
ListElement { value: "5" },
ListElement { value: "4" },
ListElement { value: "3" },
ListElement { value: "2" }
]
myValueIsIndex: false
myValue: ""
}
ListElement {
myId: "spinBox_StartCash"
myTitle: qsTr("Start cash")
myType: "SpinBox"
myMaxValue: "1000000"
myMinValue: "1000"
myPrefix: "$"
myValue: ""
}
ListElement {
myId: "spinBox_StartBlind"
myTitle: qsTr("Start blind")
myType: "SpinBox"
myMaxValue: "20000"
myMinValue: "5"
myPrefix: "$"
myValue: ""
}
ListElement {
myId: "selector_BlindsRaiseInterval"
myTitle: qsTr("Blinds raise interval")
myType: "BlindsRaiseInterval"
myRaiseOnHandsType: "" //if false it is raise on minutes type
myRaiseOnHandsInterval: ""
myRaiseOnMinutesInterval: ""
}
ListElement {
myId: "selector_BlindsRaiseMode"
myTitle: qsTr("Blinds raise mode")
myType: "BlindsRaiseMode"
myAlwaysDoubleBlinds: "" //if false it is "Manual Blinds Order"
myManualBlindsList: []
myAfterMBAlwaysDoubleBlinds: ""
myAfterMBAlwaysRaiseAbout: ""
myAfterMBAlwaysRaiseValue: ""
myAfterMBStayAtLastBlind: ""
}
ListElement {
myId: "comboBox_GameSpeed"
myTitle: qsTr("Game speed")
myType: "ComboBox"
myValuesList: [
ListElement { value: "11" },
ListElement { value: "10" },
ListElement { value: "9" },
ListElement { value: "8" },
ListElement { value: "7" },
ListElement { value: "6" },
ListElement { value: "5" },
ListElement { value: "4" },
ListElement { value: "3" },
ListElement { value: "2" },
ListElement { value: "1" }
]
myValue: ""
myValueIsIndex: false
}

当 ListView 组件完成后,我从名为“Config”的 C++ 类中读取了一些配置值。

            Component.onCompleted: {
//set Config Values from config file
createLocalGameViewModel.setProperty(0, "myValue", Config.readConfigIntString("NumberOfPlayers"));
createLocalGameViewModel.setProperty(1, "myValue", Config.readConfigIntString("StartCash"));
createLocalGameViewModel.setProperty(2, "myValue", Config.readConfigIntString("FirstSmallBlind"));
createLocalGameViewModel.setProperty(3, "myRaiseOnHandsType", Config.readConfigIntString("RaiseBlindsAtHands"));
createLocalGameViewModel.setProperty(3, "myRaiseOnHandsInterval", Config.readConfigIntString("RaiseSmallBlindEveryHands"));
createLocalGameViewModel.setProperty(3, "myRaiseOnMinutesInterval", Config.readConfigIntString("RaiseSmallBlindEveryMinutes"));
createLocalGameViewModel.setProperty(4, "myAlwaysDoubleBlinds", Config.readConfigIntString("AlwaysDoubleBlinds"));

var tempList = Config.readConfigIntStringList("ManualBlindsList");
for(var i=0; i < tempList.length; i++) {
createLocalGameViewModel.get(4).myManualBlindsList.append({"blindValue": tempList[i].toString()})
}

createLocalGameViewModel.setProperty(4, "myAfterMBAlwaysDoubleBlinds", Config.readConfigIntString("AfterMBAlwaysDoubleBlinds"));
createLocalGameViewModel.setProperty(4, "myAfterMBAlwaysRaiseAbout", Config.readConfigIntString("AfterMBAlwaysRaiseAbout"));
createLocalGameViewModel.setProperty(4, "myAfterMBAlwaysRaiseValue", Config.readConfigIntString("AfterMBAlwaysRaiseValue"));
createLocalGameViewModel.setProperty(4, "myAfterMBStayAtLastBlind", Config.readConfigIntString("AfterMBStayAtLastBlind"));
createLocalGameViewModel.setProperty(5, "myValue", Config.readConfigIntString("GameSpeed"));
}

当 listView 完成后,我需要将编辑后的数据传递给 C++。所以我意识到这不是在 QML 中构建模型的最佳方式。所以我打算用 C++ 创建模型,但我以前从未这样做过。所以我的问题是:哪种模型结构最适合这个用例,但设置起来不会太复杂?

或者是否有另一种不太难看的方法将数据从 QML 模型放入 C++ 上下文?

最佳答案

似乎是在使用 QQmlListProperty,如 http://doc.qt.io/qt-5/qtqml-cppintegration-exposecppattributes.html#properties-with-object-list-types 中所述很适合您的模型。

如果您碰巧还在使用 Qt 4.8,您自己的 QObject 派生类的 QList 可能也可以工作。有关这方面的更多信息,请参见:http://doc.qt.io/qt-4.8/qdeclarativemodels.html#c-data-models .

关于c++ - QML - 实现 C++ ListModel 的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32237037/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com