访问组合框数据模型
Accessing combo box data model
我正在将 QJsonArray
从 C++ 发送到 QML 组合框:
netlib.h
void discoveryResultChanged(const QJsonArray jsonArray);
netlib.cpp
QByteArray answer_ba = reply->readAll();
QJsonDocument answer_json_doc = QJsonDocument::fromJson(answer_ba);
QJsonObject answer_json_obj = answer_json_doc.object();
QJsonArray answer_json_array = answer_json_obj["printers_array"].toArray();
qDebug() << __func__ << "JSON array: " << answer_json_array;
emit discoveryResultChanged(answer_json_array);
*.qml
ColumnLayout {
id: printersColumn
width: parent.width
spacing: 8
Layout.bottomMargin: 8
Layout.topMargin: 8
visible: !netlib.discoveryInProgress
RowLayout {
width: parent.width
ComboBox {
id: printers
width: parent.width
anchors.margins: 4
textRole: "name"
}
Connections {
target: netlib
onDiscoveryResultChanged: {
printers.model = jsonArray // ComboBox model is set to QJsonArray
}
}
}
RowLayout {
TextArea {
text: printers.currentText
// How to access `printers` ComboBox data model here?
// I need to access key/values of QJsonArray ... how?
}
}
}
样本QJsonArray
是
QJsonArray([{"ip":"10.10.2.22","name":"N 0","port":4000,"profiles_array":[{"config":"0 blah blah blah","id":0,"name":"Profile 0-0"},{"config":"1 blah blah blah","id":1,"name":"Profile 0-1"},{"config":"2 blah blah blah","id":2,"name":"Profile 0-2"}]},{"ip":"192.168.1.1","name":"N 1","port":4001,"profiles_array":[{"config":"0 blah blah blah","id":0,"name":"Profile 1-0"},{"config":"1 blah blah blah","id": 1,"name":"Profile 1-1"},{"config":"2 blah blah blah","id":2,"name":"Profile 1-2"}] },{"ip":"172.16.1.1","name":"N 2","port":4003,"profiles_array":[{"config":" 0 等等等等等等","id":0,"name":"Profile 2-0"},{"config":"1 等等等等等等","id":1,"name":"Profile 2-1"},{"config":"2 blah blah blah","id":2,"name":"Profile 2-2"}]}])
如何从我的 QML 代码的 TextArea
中访问 ComboBox 模型 QJsonArray
的 key/values?
这样,我可以从我的 TextArea
:
中访问 ID 为 printers
的 ComboBox
的 JsonArray
模型 key/values
TextArea {
text: "Printer IP: " + printers.model[printers.currentIndex].ip +
"\nPrinter Port: " + printers.model[printers.currentIndex].port
}
我正在将 QJsonArray
从 C++ 发送到 QML 组合框:
netlib.h
void discoveryResultChanged(const QJsonArray jsonArray);
netlib.cpp
QByteArray answer_ba = reply->readAll();
QJsonDocument answer_json_doc = QJsonDocument::fromJson(answer_ba);
QJsonObject answer_json_obj = answer_json_doc.object();
QJsonArray answer_json_array = answer_json_obj["printers_array"].toArray();
qDebug() << __func__ << "JSON array: " << answer_json_array;
emit discoveryResultChanged(answer_json_array);
*.qml
ColumnLayout {
id: printersColumn
width: parent.width
spacing: 8
Layout.bottomMargin: 8
Layout.topMargin: 8
visible: !netlib.discoveryInProgress
RowLayout {
width: parent.width
ComboBox {
id: printers
width: parent.width
anchors.margins: 4
textRole: "name"
}
Connections {
target: netlib
onDiscoveryResultChanged: {
printers.model = jsonArray // ComboBox model is set to QJsonArray
}
}
}
RowLayout {
TextArea {
text: printers.currentText
// How to access `printers` ComboBox data model here?
// I need to access key/values of QJsonArray ... how?
}
}
}
样本QJsonArray
是
QJsonArray([{"ip":"10.10.2.22","name":"N 0","port":4000,"profiles_array":[{"config":"0 blah blah blah","id":0,"name":"Profile 0-0"},{"config":"1 blah blah blah","id":1,"name":"Profile 0-1"},{"config":"2 blah blah blah","id":2,"name":"Profile 0-2"}]},{"ip":"192.168.1.1","name":"N 1","port":4001,"profiles_array":[{"config":"0 blah blah blah","id":0,"name":"Profile 1-0"},{"config":"1 blah blah blah","id": 1,"name":"Profile 1-1"},{"config":"2 blah blah blah","id":2,"name":"Profile 1-2"}] },{"ip":"172.16.1.1","name":"N 2","port":4003,"profiles_array":[{"config":" 0 等等等等等等","id":0,"name":"Profile 2-0"},{"config":"1 等等等等等等","id":1,"name":"Profile 2-1"},{"config":"2 blah blah blah","id":2,"name":"Profile 2-2"}]}])
如何从我的 QML 代码的 TextArea
中访问 ComboBox 模型 QJsonArray
的 key/values?
这样,我可以从我的 TextArea
:
printers
的 ComboBox
的 JsonArray
模型 key/values
TextArea {
text: "Printer IP: " + printers.model[printers.currentIndex].ip +
"\nPrinter Port: " + printers.model[printers.currentIndex].port
}