Я отправляю 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"": 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," examples_array ": [{" config ":" 0 бла-бла-бла "," id ": 0," имя ": "Профиль 1-0"}, {"config": "1 бла-бла-бла", "id": 1, "name": "Профиль 1-1"}, {"config": "2 бла-бла-бла", "id": 2, "name": "Profile 1-2"}]}, {"ip": "172.16.1.1", "name": "N 2", "port": 4003, "profile_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"}]}])
Как получить доступ к ключу / значениям модели ComboBox QJsonArray
из TextArea
в моем коде QML?