Important: Please read the Qt Code of Conduct -

QML & Array of Objects to QML side

  • Qt Champions 2017


    I have array of Objects in C++ side. E.g QList<Fruits>. Assume that number of fruits are 5. I would like to display the name of fruit using five text elements.

    1. I can expose these objects using Model. If Expose the fruit list as model, I have to use them View.
    2. If I expose through QVariantList, any updates to fruit list will not be updated updated internally. Issues with this approach is that need to send the full list again.

    Is there way I can use the model, still update the data with model internally and use it without Views ?

    Please note that I'm experimenting with different options. If you have any inputs please suggest.

  • @dheerendra said in QML & Array of Objects to QML side:

    Is there way I can use the model, still update the data with model internally and use it without Views ?

    I didn't understand, can you rephrase the question? What do you mean by "update the data with model internally"? What is using without Views?

  • Qt Champions 2017

    Thank you responding to my question. I would like to have model in C++. Assume I have 5 elements in model and it has role called 'name'. I would like to display the name with simple 5 text elements instead of using ListView or GridView etc. Also I would like the text elements to updated whenever the model changes.

    Hope it clarifies.

  • @dheerendra I think you still need something which uses a model automatically or you have to catch the signals from C++ and update the visual elements manually. Strictly speaking a Repeater is not a View but it's updated automatically:

    import QtQuick 2.7
    import QtQuick.Controls 2.2
    ApplicationWindow {
        visible: true
        width: 640
        height: 480
        ListModel {
            id: lm
            ListElement {}
            ListElement {}
        Row {
              Repeater {
                  model: lm
                  Button {
                      text: lm.count
                      onClicked: lm.append({})

  • I think Signal & Slot may be helpful for update data

  • @dheerendra

    Do you really need a model in C++ to do this. Here is how I create a row of tabs dynamically in QML:

     TabView {
                var i = controller.getNumOfMarkerTypes();
                for (var j = 0; j< i; j++) {
                    var tab = addTab(controller.getMarkerTypeString(j),waypointIconTabView)
           = true;
                    console.log("\t\t>>>>>>Tab", tab.progress, tab.item)
                    tab.item.markerType = j;
                    //    tab.loaded.connect(
                    //      tab.loaded.connect(function() { tab.item.markerType = j; })
                    tab.item.focus = true
                currentIndex = 1;
    Now controller is a c++ class.  It has the two methods defined as:

    Q_INVOKABLE int getNumOfMarkerTypes();
    Q_INVOKABLE QString getMarkerTypeString(int);

  • @DBoosalis That will not be updated automatically, which dheerendra explicitly wanted.

Log in to reply