How can I add element to Qt Quick Controls Combobox?
-
wrote on 20 Aug 2013, 08:26 last edited by
I want to refresh the lists of element in a combobox, how can i do this?
The goal is to populate the combo with the list of the serial port available on the pc. -
wrote on 20 Aug 2013, 11:11 last edited by
You should be able to create any QAbstractItemModel and assign it to the model property of ComboBox after it has been populated. It should also respect modifications done to the model dynamically as long as the model notifies the view about changes. Perhaps you are running into a bug?
-
wrote on 20 Aug 2013, 12:27 last edited by
Hi, sorry but I don't understand... can you give me an example?
-
wrote on 20 Aug 2013, 12:59 last edited by
Not sure what you need an example of. This is how you can expose QStandardItemModel to qml from C++:
http://developer.nokia.com/Community/Wiki/Using_QStandardItemModel_in_QML -
wrote on 20 Aug 2013, 13:07 last edited by
@ ComboBox
{
id: comboComs
model: ListModel
{
id: cbItems
ListElement { text: "Banana"; color: "Yellow" }
ListElement { text: "Apple"; color: "Green" }
ListElement { text: "Coconut"; color: "Brown"
}
}
Component.onCompleted:
{
//invoke method on c++ nad get new list of elements
//add element to ListModel
cbItems.append(?????????)
}
@This is the code I'm tring to use... but the problem is how to populate the ListModel whit new items...
[quote author="Jens" date="1377003595"]Not sure what you need an example of. This is how you can expose QStandardItemModel to qml from C++:
http://developer.nokia.com/Community/Wiki/Using_QStandardItemModel_in_QML[/quote] -
wrote on 20 Aug 2013, 13:23 last edited by
You can check the ListView documentation. It goes something like this:
@
cbItems.append({"text": "Banana", "color": "red"})
@ -
wrote on 20 Aug 2013, 13:37 last edited by
It works, but from c++ I've a QList<SerialPortInfo>... so, how can i expose the serialportinfo propreties in the list?
There is compatibility from c++ obj and qml object? So I can do something like this:
@
cbItems.append({"text": "serialport.value(0).name", "color": "red"})
@ -
wrote on 20 Aug 2013, 13:40 last edited by
As I previously mention you cannot use ListView directly from C++. Look at the Using QStandardItemModel link I pasted. That is a regular C++ model. you can populate in C++. The documentation for how to use the model in C++ is here: http://qt-project.org/doc/qt-5.0/qtgui/qstandarditemmodel.html
The link I pasted above explains how to make use of it from qml. -
wrote on 20 Aug 2013, 15:23 last edited by
This is what I've tred
@int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv); //preparo l'applicazione
QQmlApplicationEngine engine(QUrl("cn/qml/main.qml")); //carico l'interfaccia
QObject *radice = engine.rootObjects().value(0); //identifico la radice
QQuickWindow *applicationwindow = qobject_cast<QQuickWindow *>(radice); //acquisisco in C++ l'ogg QML ApplicationWindow
if ( !applicationwindow )
{
qWarning("Error: Your root item has to be a Window.");
return -1;
}Engine motore(radice); QObject::connect(applicationwindow, SIGNAL(sceneGraphInitialized()),&motore,SLOT(fineCaricamento())); QStandardItemModel* m = new QStandardItemModel(); QStandardItem* it = new QStandardItem(); it->setData("ciao",0); it->setData("e.score", 1); it->setData("ciao",2); m->appendRow(it); QQmlContext c(&engine,(radice->findChild<QObject*>("comboCom",Qt::FindChildrenRecursively))); c.setContextProperty("cbItems",m); applicationwindow->show(); return app.exec();
}@
and the specific QML is:
@ComboBox
{
id: comboComs
objectName: "comboCom"
model: ListModel
{
id: cbItems
ListElement { text: "Banana"; }
}
}@
but nothing happen...I've no idea what is the problem...
[quote author="Jens" date="1377006040"]As I previously mention you cannot use ListView directly from C++. Look at the Using QStandardItemModel link I pasted. That is a regular C++ model. you can populate in C++. The documentation for how to use the model in C++ is here: http://qt-project.org/doc/qt-5.0/qtgui/qstandarditemmodel.html
The link I pasted above explains how to make use of it from qml.[/quote] -
wrote on 20 Aug 2013, 15:29 last edited by
You don't have to create a ListModel on the qml side. You simply assign the context property itself as the model. I.e "model: cbItems", provided that cbItems is available in the scope.
-
wrote on 20 Aug 2013, 15:30 last edited by
So I must delete the ListElement { text: "Banana"; }...
[quote author="Jens" date="1377012579"]You don't have to create a ListModel on the qml side. You simply assign the context property itself as the model. I.e "model: cbItems", provided that cbItems is available in the scope.[/quote] -
wrote on 20 Aug 2013, 15:44 last edited by
I've tryed:
@ComboBox
{
id: comboComs
objectName: "comboCom"
model: m
}
@
@ QStandardItemModel* m = new QStandardItemModel();
QStandardItem* it = new QStandardItem();
it->setData("com1");
m->appendRow(it);
QStandardItem* it2 = new QStandardItem();
it2->setData("com2");
m->appendRow(it2);
@and I'm getting this error
@"file:///C:/Users/Michele/Desktop/cn/cn/qml/main.qml:106: ReferenceError: m is not defined"@
[quote author="mikecurl91" date="1377012648"]So I must delete the ListElement { text: "Banana"; }...
[quote author="Jens" date="1377012579"]You don't have to create a ListModel on the qml side. You simply assign the context property itself as the model. I.e "model: cbItems", provided that cbItems is available in the scope.[/quote]
[/quote][quote author="mikecurl91" date="1377012648"]So I must delete the ListElement { text: "Banana"; }...
[quote author="Jens" date="1377012579"]You don't have to create a ListModel on the qml side. You simply assign the context property itself as the model. I.e "model: cbItems", provided that cbItems is available in the scope.[/quote]
[/quote] -
wrote on 20 Aug 2013, 15:50 last edited by
Your context property is called "cbItems", not "m".
-
wrote on 20 Aug 2013, 15:57 last edited by
Ok, no errors...
@ QStandardItemModel* m = new QStandardItemModel();
QStandardItem* it = new QStandardItem();
it->setData(QVariant("com1"));
m->appendRow(it);
QStandardItem* it2 = new QStandardItem();
it2->setData(QVariant("com2"));
m->appendRow(it2);QQmlContext c(&engine,(radice->findChild<QObject*>("comboCom",Qt::FindChildrenRecursively))); c.setContextProperty("cbItems",m);@
@ComboBox
{
id: comboComs
objectName: "comboCom"
model: ListModel{
id: cbItems
}
}@but on combobox nothing appears... :
[quote author="Jens" date="1377013804"]Your context property is called "cbItems", not "m". [/quote] -
wrote on 21 Aug 2013, 08:15 last edited by
As previously mentioned, you are not supposed to create a ListModel when you expose an abstract item model to qml.
@
ComboBox {
id: comboComs
objectName: "comboCom"
model: cbItems
}
@
6/15