I'm sending a QJsonArray
from C++ to QML combo box:
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?
}
}
}
The sample QJsonArray
is
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 blah blah blah","id":0,"name":"Profile 2-0"},{"config":"1 blah blah blah","id":1,"name":"Profile 2-1"},{"config":"2 blah blah blah","id":2,"name":"Profile 2-2"}]}])
How can I access key/values of ComboBox model QJsonArray
from within the TextArea
on my QML code?
This way, I could access JsonArray
model key/values of ComboBox
with id printers
from within my TextArea
:
TextArea {
text: "Printer IP: " + printers.model[printers.currentIndex].ip +
"\nPrinter Port: " + printers.model[printers.currentIndex].port
}