Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How can I add element to Qt Quick Controls Combobox?
Forum Updated to NodeBB v4.3 + New Features

How can I add element to Qt Quick Controls Combobox?

Scheduled Pinned Locked Moved QML and Qt Quick
15 Posts 2 Posters 13.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mikecurl91
    wrote on last edited by
    #3

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

    1 Reply Last reply
    0
    • J Offline
      J Offline
      Jens
      wrote on last edited by
      #4

      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

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mikecurl91
        wrote on last edited by
        #5

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

        1 Reply Last reply
        0
        • J Offline
          J Offline
          Jens
          wrote on last edited by
          #6

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

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

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mikecurl91
            wrote on last edited by
            #7

            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"})
            @

            1 Reply Last reply
            0
            • J Offline
              J Offline
              Jens
              wrote on last edited by
              #8

              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.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mikecurl91
                wrote on last edited by
                #9

                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]

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  Jens
                  wrote on last edited by
                  #10

                  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.

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mikecurl91
                    wrote on last edited by
                    #11

                    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]

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mikecurl91
                      wrote on last edited by
                      #12

                      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]

                      1 Reply Last reply
                      0
                      • J Offline
                        J Offline
                        Jens
                        wrote on last edited by
                        #13

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

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          mikecurl91
                          wrote on last edited by
                          #14

                          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]

                          1 Reply Last reply
                          0
                          • J Offline
                            J Offline
                            Jens
                            wrote on last edited by
                            #15

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

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved