Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import QtQuick 2.9
- import QtQuick.Window 2.2
- import QtQuick.Controls 2.2
- Window {
- id: root
- visible: true
- width: 640
- height: 480
- title: qsTr("Hello World")
- ListModel {
- id: listModel
- ListElement { text: "one"; value: 1 }
- ListElement { text: "two"; value: 2 }
- ListElement { text: "three"; value: 3 }
- ListElement { text: "four"; value: 4 }
- }
- property var arrayModel: [
- { text: "one", value: 1 },
- { text: "two", value: 2 },
- { text: "three", value: 3 },
- { text: "four", value: 4 }
- ]
- property string myText: "three"
- property var myValue: 3
- ComboBox {
- id: comboBox
- model: listModel
- textRole: "text"
- currentIndex: findValue(root.myValue)
- onActivated: myText = currentText
- property string valueRole: "value"
- function findValue(value) {
- print("findValue model count", model.length || model.count)
- if (!valueRole)
- return -1;
- if (Array.isArray(model)) {
- return model.findIndex(data => data && typeof data === "object" && data.hasOwnProperty(valueRole) && data[valueRole] === value);
- } else {
- for (var i = 0; i < model.count; ++i) {
- if (model.get(i)[valueRole] === value)
- return i;
- }
- return -1;
- }
- }
- }
- Text {
- x: 200
- text: comboBox.currentText
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement