How can I add element to Qt Quick Controls Combobox?



  • 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.



  • 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?



  • Hi, sorry but I don't understand... can you give me an example?



  • 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



  • @ 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]



  • You can check the ListView documentation. It goes something like this:

    @
    cbItems.append({"text": "Banana", "color": "red"})
    @



  • 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]



  • 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.



  • 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]



  • Your context property is called "cbItems", not "m".



  • 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]



  • 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
    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.