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. [SOLVED] receiving list from c++ to qml listview
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved QML and Qt Quick
22 Posts 5 Posters 25.5k 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.
  • R Offline
    R Offline
    raja26
    wrote on last edited by
    #13

    @spode: My pleasure.. :-)

    1 Reply Last reply
    0
    • J Offline
      J Offline
      joonne
      wrote on last edited by
      #14

      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
      0
      • R Offline
        R Offline
        raja26
        wrote on last edited by
        #15

        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
        0
        • J Offline
          J Offline
          joonne
          wrote on last edited by
          #16

          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
          0
          • R Offline
            R Offline
            raja26
            wrote on last edited by
            #17

            @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
            0
            • R Offline
              R Offline
              raja26
              wrote on last edited by
              #18

              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
              0
              • J Offline
                J Offline
                joonne
                wrote on last edited by
                #19

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

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  joonne
                  wrote on last edited by
                  #20

                  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
                  0
                  • J Offline
                    J Offline
                    joonne
                    wrote on last edited by
                    #21

                    Ok I realised something, I had to use setContextProperty function again to update the excerciseNamesModel and I passed the variable context to my actual model class and now everything works. Thank you for the help! :)

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      raja26
                      wrote on last edited by
                      #22

                      Hi joonne, I am afraid that the moderators will not allow us to discuss about 'qDebug()' problem in this thread, so you have to search in the forums, to see if this question ahs been answered already, else you have to create a new thread for this. :-)

                      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