Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    [SOLVED] receiving list from c++ to qml listview

    QML and Qt Quick
    5
    22
    23222
    Loading More Posts
    • 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.
    • S
      spode last edited by

      how to take a list from c++ to qml? in this case for example, i need to obtain the list from file.cpp:
      @
      Component { id: delegate_grafica; Rectangle { width: 300; height: 30; color: "orange"; border.width: 1; border.color: "yellow"; Text { color: "black"; text: deutschesWort; font.pixelSize: 15; anchors.centerIn: parent } } }
      ListView { id: list_parolaSalvate; x: 0; y: 0; width: 300; height: 300; delegate: delegate_grafica; }
      @

      e il "model"???

      1 Reply Last reply Reply Quote 0
      • K
        kkrzewniak last edited by

        Please add some new lines to your code.

        Expose a QAbstractListModel subclass as a context property or by using a Q_INVOAKABLE method.
        Other options are using a QStringList or a QList<QObject> as the model.

        Remember to register your QAbstractListModel subclass if you'll not be exposing it via context property.
        qmlRegisterUncreatableType/qmlRegisterType

        Me, Grimlock, not "nice dino". ME BASH BRAINS!

        1 Reply Last reply Reply Quote 0
        • S
          spode last edited by

          i will use "Other options are using a QStringList or a QList<QObject> as the model.", but the problem is: how to pass this qlist to this QML file?
          main.qml
          @
          Rectangle {
          width: 500; height: 500; signal emitsignal();
          Component { id: delegate_grafica; Rectangle { width: 300; height: 30; color: "orange"; border.width: 1; border.color: "yellow"; Text { color: "black"; text: deutschesWort; font.pixelSize: 15; anchors.centerIn: parent } } }
          ListView { id: list_parolaSalvate; x: 0; y: 0; width: 300; height: 300; delegate: delegate_grafica; model: emitsignal() }
          }
          @
          here i need a model, that i want to obtain from emitsignal, by which i would to obtain a qlist that is then the container of the model of my listview {}. my qlist will contain qlist<biglietto>
          where biglietto is:
          @
          biglietto::biglietto(QString newcolor, int newnumberm, bool important){
          QString color = newcolor;
          int number = newnumber;
          bool isImportant = important;
          etc.
          }
          @
          however, thank yoU!

          1 Reply Last reply Reply Quote 0
          • S
            srikanth_trulyit last edited by

            @ QDeclarativeContext* context = declarativeView.rootContext();
            context->setContextProperty("mymodel",(QObject*)&myModel); // here myModel is the QObject that exposes your model
            @

            @
            MyModel: public QObject {
            ...
            Q_INVOKABLE QStringList dataModel();
            }
            @

            In Qml
            @
            ListView {
            ...
            model: myModel.dataModel();
            }
            @

            1 Reply Last reply Reply Quote 0
            • R
              raja26 last edited by

              But still how can I access the model data in delegate?

              1 Reply Last reply Reply Quote 0
              • S
                spode last edited by

                You need to write in the delegate the name of a public variable contained in the model, which you associate with QDeclarativeView.contextProperty.setContextProperty(nameOfTheModel, listThatyouhavemade). Note the you have to use first that I have just written and then set the source of the qml file where the model is needed.

                1 Reply Last reply Reply Quote 0
                • R
                  raja26 last edited by

                  I found that a simple @modelData@ in the delegate does it for QStringList.
                  [quote author="spode" date="1345319889"]You need to write in the delegate the name of a public variable contained in the model, which you associate with QDeclarativeView.contextProperty.setContextProperty(nameOfTheModel, listThatyouhavemade). Note the you have to use first that I have just written and then set the source of the qml file where the model is needed.[/quote]

                  1 Reply Last reply Reply Quote 0
                  • S
                    spode last edited by

                    I have not understood. could you please explain youself better?

                    1 Reply Last reply Reply Quote 0
                    • R
                      raja26 last edited by

                      To access the list of @QString@ in that QStringList I have used @modelData.

                      Like this:

                      ListView {
                      model: stringListPassedFromCpp
                      delegate: Text {
                      text: modelData
                      }
                      }@

                      1 Reply Last reply Reply Quote 0
                      • S
                        spode last edited by

                        I do not understand..

                        1 Reply Last reply Reply Quote 0
                        • R
                          raja26 last edited by

                          Look at my above code. The 'modelData' gives the QString stored in QStringList. So that I can use 'modelData' to access the QString stored inside the QStrlingList.

                          I have used QStringList as the model for QML ListView. Have you understood now?

                          1 Reply Last reply Reply Quote 0
                          • S
                            spode last edited by

                            Ok. Now i have understood! :)

                            1 Reply Last reply Reply Quote 0
                            • R
                              raja26 last edited by

                              @spode: My pleasure.. :-)

                              1 Reply Last reply Reply Quote 0
                              • J
                                joonne last edited by

                                Hi, I am trying to do almost same thing as mentioned here but I don't understand what modelData means here:

                                @ListView {
                                model: stringListPassedFromCpp
                                delegate: Text {
                                text: modelData
                                }
                                }@

                                How and where should I declare this modelData ? Thanks in advance. :)

                                I could ask something more and accurate about this too:

                                I have now QStringList for testing purposes which has 3 strings and I would like to get this QStringList to qml. I would like to use TextSwitch as delegate. This is what I have at the moment:

                                @SilicaListView {

                                        anchors.fill: parent
                                        model: model_.excerciseNames()
                                
                                        delegate: TextSwitch {
                                
                                            text: 
                                            onClicked: {}
                                        }
                                    }
                                

                                }@

                                The function excerciseNames() is defined as

                                @Q_INVOKABLE QStringList excerciseNames();@

                                I think I am yet very far from the solution so any help would be great :D

                                1 Reply Last reply Reply Quote 0
                                • R
                                  raja26 last edited by

                                  joone
                                  Read about modelData in this link "QStringList to QML":https://qt-project.org/doc/qt-5.0/qtquick/qtquick-modelviewsdata-cppmodels.html#qstringlist-based-model

                                  So all the QString within the QStringList will can be accessed as modelData

                                  So you can access the QString data in the TextSwitch delegate as

                                  @SilicaListView {
                                  anchors.fill: parent
                                  model: model_.excerciseNames()

                                  delegate: TextSwitch {
                                  text: modelData
                                  onClicked: {}
                                  }
                                  }@

                                  1 Reply Last reply Reply Quote 0
                                  • J
                                    joonne last edited by

                                    The examples in that page you pointed me are invisible for me, can someone see whats in there? I tried to solve this from the Qt-Creator examples also but didn't get it to work yet. When I use the Q_INVOKABLE method with QStringList I get one of the Strings from the list but when I try to do as the example refered and use this :

                                    @context>setContextProperty("excerciseNamesModel",QVariant::fromValue(model->excerciseNames()));

                                    SilicaListView {
                                    anchors.fill: parent
                                    model: excerciseNamesModel

                                    delegate: TextSwitch {
                                    text: modelData
                                    onClicked: {}
                                    }
                                    }
                                    @

                                    then I don't get anything. The registered excerciseNamesModel is coloured in the editor so it apparently recognizes the model but it is not working for some reason.

                                    1 Reply Last reply Reply Quote 0
                                    • R
                                      raja26 last edited by

                                      @Q_INVOKABLE@

                                      means the the method can be called from the QML side. So you have to set your cpp model object as
                                      @
                                      ExcerciseNamesModel namesModel;
                                      auto context = viewer.rootContext();
                                      context->setContextProperty("excerciseNamesModel", namesModel)@

                                      and then set the model in QML file as
                                      @
                                      SilicaListView {
                                      anchors.fill: parent
                                      model: excerciseNamesModel.excerciseNames()

                                      delegate: TextSwitch {
                                      text: modelData
                                      onClicked: {}
                                      }
                                      }@

                                      Note: This code is just like pseudocode and I didn't compile it or test it.

                                      1 Reply Last reply Reply Quote 0
                                      • R
                                        raja26 last edited by

                                        You can read more about Exposing CPP models to QML in this link "Link":http://qt-project.org/doc/qt-4.8/qdeclarativemodels.html#qstringlist-based-model

                                        1 Reply Last reply Reply Quote 0
                                        • J
                                          joonne last edited by

                                          Okay thanks, I will try to figure this out. :)

                                          1 Reply Last reply Reply Quote 0
                                          • J
                                            joonne last edited by

                                            Would you happen to know a solution for my other problem also ? After updating this Sailfish SDK to a newer version, I lost qDebug() prints and I haven't found where they would come back. I can't see them at the application output view anymore and this makes it a lot harder to track down the behaviour of my program. :D

                                            1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post