How can I add element to Qt Quick Controls Combobox?
-
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"})
@ -
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. -
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] -
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] -
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] -
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]