目前我有一个在QML中定义的ListModel,每个ListElement中有许多不同的Fields。代表根据“myType”值以不同方式做出反应。
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 ++上下文中吗?
答案 0 :(得分:1)
在Qt中创建强大数据模型的好方法是使用QAbstractItemModel
(http://doc.qt.io/qt-5/qabstractitemmodel.html)
我创建了一个Qt / QML插件库,你可以基于:https://github.com/ThomArmax/QtArmax
有关更多实施信息,请参阅https://github.com/ThomArmax/QtArmax/tree/develop/src/core/datamodels