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 fill a "ListView QML Type" with a model from a "QList<QString>"'?
Forum Updated to NodeBB v4.3 + New Features

How can I fill a "ListView QML Type" with a model from a "QList<QString>"'?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qtquickqml
15 Posts 6 Posters 7.3k Views 2 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.
  • S Offline
    S Offline
    sm-a
    wrote on last edited by sm-a
    #6

    Hello @Gojir4

    Thank you for the explanation.

    I have a method that returns a "QStringList". This return value must now be transferred to the model and displayed. The model must also be updated after each click on a button that call the method again and refreshes the returned value and therefore the ListView.

    I can't do it with the presented method.
    I hope you have an idea for that.

    Greetings,
    Sven

    Gojir4G 1 Reply Last reply
    0
    • S sm-a

      Hello @Gojir4

      Thank you for the explanation.

      I have a method that returns a "QStringList". This return value must now be transferred to the model and displayed. The model must also be updated after each click on a button that call the method again and refreshes the returned value and therefore the ListView.

      I can't do it with the presented method.
      I hope you have an idea for that.

      Greetings,
      Sven

      Gojir4G Offline
      Gojir4G Offline
      Gojir4
      wrote on last edited by
      #7

      @sm-a,

      This is a simplified example. You will probably not instantiate your model in the main.cpp in your final application. Usually it is a member of a class, like MainWindow, or a dedicated class for anything you want to do.

      Can we see how is actually returned the stringlist in your code ?

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sm-a
        wrote on last edited by
        #8

        Hello @Gojir4

        I do it very simple. I do this as follows:

        QStringList getJobsFromI::getJobsForCity(QString city)
        {
            [...]
            return jobsStringList;
        }
        
        Gojir4G 1 Reply Last reply
        0
        • S sm-a

          Hello @Gojir4

          I do it very simple. I do this as follows:

          QStringList getJobsFromI::getJobsForCity(QString city)
          {
              [...]
              return jobsStringList;
          }
          
          Gojir4G Offline
          Gojir4G Offline
          Gojir4
          wrote on last edited by Gojir4
          #9

          @sm-a Sorry I was not clear. I mean where do you call this function ?
          Is your class getJobsFromI deriving from QObject ?If yes, you can simply add the QStringListModel as a Q_PROPERTY of your class getJobsFromI, and then share the instance of this class with QML by using QQmlContext::setContextProperty, as demonstrated by @LeLev. Then when you update the stringlist, you can update the model at the same time.

          If getJobsFromI does not derive from QObject, you can the the QStringListModel instance as context property, but then you need to setup an access of your model from the place in your code where you update the stringlist, to be able to update the model as well.

          How is structured your app ? Where is located the getJobsFromI object ?

          1 Reply Last reply
          0
          • S Offline
            S Offline
            sm-a
            wrote on last edited by sm-a
            #10

            I have now a test class created with default data. How can I implement this to a ListView QML? Please help!

            getjobsfromi.cpp

            #include "getjobsfromi.h"
            
            getJobsFromI::getJobsFromI(QObject *parent) : QObject(parent)
            {
            
            }
            
            QStringList getJobsFromI::getJobsForTiteAndCity(QString jobTitle, QString jobCity)
            {
                    // TEST WITH STATIC DATA
                    QStringList jobsStringList;
                    jobsStringList << "Job1" << "Job2" << "Job3";
            
                    return jobsStringList;
            }
            

            getjobsfromi.h

            #ifndef GETJOBSFROMI_H
            #define GETJOBSFROMI_H
            
            #include <QObject>
            
            class getJobsFromI : public QObject
            {
                Q_OBJECT
            public:
                explicit getJobsFromI(QObject *parent = nullptr);
                QStringList getJobsForTiteAndCity(QString jobTitle, QString jobCity);
            
            signals:
            
            public slots:
            };
            
            #endif // GETJOBSFROMI_H
            
            T Gojir4G 2 Replies Last reply
            0
            • S sm-a

              I have now a test class created with default data. How can I implement this to a ListView QML? Please help!

              getjobsfromi.cpp

              #include "getjobsfromi.h"
              
              getJobsFromI::getJobsFromI(QObject *parent) : QObject(parent)
              {
              
              }
              
              QStringList getJobsFromI::getJobsForTiteAndCity(QString jobTitle, QString jobCity)
              {
                      // TEST WITH STATIC DATA
                      QStringList jobsStringList;
                      jobsStringList << "Job1" << "Job2" << "Job3";
              
                      return jobsStringList;
              }
              

              getjobsfromi.h

              #ifndef GETJOBSFROMI_H
              #define GETJOBSFROMI_H
              
              #include <QObject>
              
              class getJobsFromI : public QObject
              {
                  Q_OBJECT
              public:
                  explicit getJobsFromI(QObject *parent = nullptr);
                  QStringList getJobsForTiteAndCity(QString jobTitle, QString jobCity);
              
              signals:
              
              public slots:
              };
              
              #endif // GETJOBSFROMI_H
              
              T Offline
              T Offline
              Tom_H
              wrote on last edited by
              #11

              @sm-a By using the first answer provided by @Gojir4

              1 Reply Last reply
              1
              • S sm-a

                I have now a test class created with default data. How can I implement this to a ListView QML? Please help!

                getjobsfromi.cpp

                #include "getjobsfromi.h"
                
                getJobsFromI::getJobsFromI(QObject *parent) : QObject(parent)
                {
                
                }
                
                QStringList getJobsFromI::getJobsForTiteAndCity(QString jobTitle, QString jobCity)
                {
                        // TEST WITH STATIC DATA
                        QStringList jobsStringList;
                        jobsStringList << "Job1" << "Job2" << "Job3";
                
                        return jobsStringList;
                }
                

                getjobsfromi.h

                #ifndef GETJOBSFROMI_H
                #define GETJOBSFROMI_H
                
                #include <QObject>
                
                class getJobsFromI : public QObject
                {
                    Q_OBJECT
                public:
                    explicit getJobsFromI(QObject *parent = nullptr);
                    QStringList getJobsForTiteAndCity(QString jobTitle, QString jobCity);
                
                signals:
                
                public slots:
                };
                
                #endif // GETJOBSFROMI_H
                
                Gojir4G Offline
                Gojir4G Offline
                Gojir4
                wrote on last edited by
                #12

                @sm-a Are you calling getJobsForTiteAndCity() from C++ or QML ?

                Is your goal to call method getJobsForTiteAndCity() from QML to fill the ListView, according to the job title and city ? In this case I think you don't need getJobsForTiteAndCity() but you could use two properties for title and city, and then updating your model when these properties change.

                Anyway, here is how you can modify getJobFromI to provide a model to QML :
                main.cpp

                #include "getjobsfromi.h"
                
                #include <QGuiApplication>
                #include <QQmlApplicationEngine>
                #include <QQmlContext>
                #include <QStringListModel>
                
                int main(int argc, char *argv[])
                {
                    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
                
                    QGuiApplication app(argc, argv);
                
                //    QStringListModel model;
                //    model.setStringList(QStringList() << "This" << "is" << "my" << "stringlist");
                    getJobsFromI gjfi;
                    QQmlApplicationEngine engine;
                
                    engine.rootContext()->setContextProperty("myModel", gjfi.jobListModel());
                
                    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
                    if (engine.rootObjects().isEmpty())
                        return -1;
                
                    return app.exec();
                }
                
                

                getjobsfromi.h

                #ifndef GETJOBSFROMI_H
                #define GETJOBSFROMI_H
                
                #include <QObject>
                #include <QStringListModel>
                
                class getJobsFromI : public QObject
                {
                    Q_OBJECT
                    //declaration in QObject derived class
                    Q_PROPERTY(QStringListModel* jobsListModel READ jobListModel)
                
                    QStringListModel m_jobsListModel;
                
                public:
                    explicit getJobsFromI(QObject *parent = nullptr);
                    QStringList getJobsForTiteAndCity(QString jobTitle, QString jobCity);
                
                    QStringListModel* jobListModel();
                
                signals:
                
                public slots:
                };
                
                #endif // GETJOBSFROMI_H
                
                

                getjobsfromi.cpp

                #include "getjobsfromi.h"
                
                getJobsFromI::getJobsFromI(QObject *parent) :
                    QObject(parent)
                {}
                
                QStringList getJobsFromI::getJobsForTiteAndCity(QString jobTitle, QString jobCity)
                {
                    // TEST WITH STATIC DATA
                    QStringList jobsStringList;
                    jobsStringList << "Job1" << "Job2" << "Job3";
                
                    //Update the model
                    m_jobsListModel.setStringList(jobsStringList);
                
                    return jobsStringList;
                }
                
                QStringListModel *getJobsFromI::jobListModel()
                {
                    return &m_jobsListModel;
                }
                

                This is just an example but as I said, if you only want to access the list from QML, getJobsForTiteAndCity() is useless and I would recommend to use another approach.

                1 Reply Last reply
                0
                • S sm-a

                  Hello to all,

                  I have a QList<QString> (in c++) and now I want to use it as a model for a "ListView QML Type".
                  How can I do that?

                  I've used a lot of Google and haven't found a solution yet. I hope someone has an idea how to solve this.

                  I wish you a nice week.

                  Greetings,
                  Sven

                  S Offline
                  S Offline
                  sharath
                  wrote on last edited by sharath
                  #13

                  @sm-a Hi,

                  1. First create Q_Property of type QStringList as follows
                   Q_PROPERTY(QStringList myModel READ getJobsForTiteAndCity  NOTIFY modelChanged)
                  //OR 
                  QStringList m_model;
                  Q_PROPERTY(QStringList model MEMBER m_model NOTIFY modelChanged)
                  
                  
                  1. emit the signal whenever you update the data
                  QStringList getJobsFromI::getJobsForTiteAndCity(QString jobTitle, QString jobCity)
                  {
                          // TEST WITH STATIC DATA
                          QStringList jobsStringList;
                          jobsStringList << "Job1" << "Job2" << "Job3";
                  
                          return jobsStringList;
                          emit modelChanged(); //emitting signal
                  }
                  
                  1. call the model variable "myModel" in QML listview
                  import QtQuick 2.9
                  import QtQuick.Window 2.2
                  
                  Window {
                      visible: true
                      width: 640
                      height: 480
                      title: qsTr("Hello World")
                  
                      ListView{
                          anchors.fill: parent
                          model: getJobsFromI.myModel //classname.myModel
                          delegate: Text{
                              text: modelData
                          }
                      }
                  }
                  
                  
                  

                  Try this , i'm pretty sure it works for you

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    sm-a
                    wrote on last edited by
                    #14

                    I took another approach and executed the method directly in QML and set the "model". Now it works.

                    I just solved it like this:
                    listview.model = <myclassandmethod>(<heremyparameters>)

                    Many thanks to everyone who answered me.

                    Greetings,
                    Sven

                    1 Reply Last reply
                    0
                    • P Offline
                      P Offline
                      PHYNIXO
                      wrote on last edited by
                      #15

                      i made a script that fetch IP addresses and save them to QList and now i want to show buttons as many as the sizeof the QList in qml and i couldn't use the QAbstractListModel nor the Q_PROPERTY please help me.

                      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