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. QStringListModel doesn't update ListView
Qt 6.11 is out! See what's new in the release blog

QStringListModel doesn't update ListView

Scheduled Pinned Locked Moved QML and Qt Quick
9 Posts 3 Posters 7.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.
  • X Offline
    X Offline
    xardas008
    wrote on last edited by
    #1

    Hi,

    I have a QStringListModel where I set the StringList everytime I've added a new item to my Stringlist with the following code:

    @
    model->setStringList(stringList);
    @

    As the documentation tells me this method will notify each view about changes.
    rowCount tells me everytime the correct value of items containing, but my list view doesn't get updated (max. displayed items are 1 or 0).
    I let qml know about the changes with the following code in my main:

    @
    view.rootContext()->setContextProperty("myVar", QVariant::fromValue(list));
    @
    Did I miss something or what could be the problem I'm facing?

    So long.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dmcr
      wrote on last edited by
      #2

      Hello,

      you had to execute the last line of code each time you chang the model.

      dmcr

      1 Reply Last reply
      0
      • X Offline
        X Offline
        xardas008
        wrote on last edited by
        #3

        Ok I now get the view updated. I had somewhere a

        @QStringList list = myList.stringList()@

        and this list got added to the ListView in QML, so no updates possible. Now I changed the context property to the correct list, but I don't know how to get the QString of the QStringListModel inside of qml? Someone an idea?

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dmcr
          wrote on last edited by
          #4

          You can check the precedent post : the solution is in it : myVar.modelData in the delagate.
          Or with myVar[index] in javascript

          dmcr

          1 Reply Last reply
          0
          • X Offline
            X Offline
            xardas008
            wrote on last edited by
            #5

            Both myVar.modelData as well as myVar[index] tell me that it's undefined.

            I changed the context property in main to the following:
            @view.rootContext()->setContextProperty("myVar", myList)@

            Now he shows me each rectangle of the list i should have, but I don't know how to get the strings out of the model.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dmcr
              wrote on last edited by
              #6

              Could you bring a bench of code, it help us to have an idea of what's going wrong

              dmcr

              1 Reply Last reply
              0
              • Z Offline
                Z Offline
                Zamx
                wrote on last edited by
                #7

                I inherited for QStringListModel so that I could access it from QML. With this you can modelData inside QML.

                @class NIQStringListModel : public QStringListModel
                {
                Q_OBJECT
                public:
                explicit NIQStringListModel (QObject *parent = 0);

                void         append             (const QString& string);
                

                public slots:
                int length () const { return rowCount(); }
                QStringList getStringList () const { return stringList(); }
                QString dataAt (int i) const;
                };@

                @NIQStringListModel::NIQStringListModel(QObject *parent) :
                QStringListModel(parent)
                {
                QHash<int, QByteArray> roles;
                roles[Qt::DisplayRole] = "modelData";
                setRoleNames(roles);
                }

                void NIQStringListModel::append(const QString &string)
                {
                insertRow( rowCount() );
                setData( index( rowCount() - 1 ), string );
                }

                QString NIQStringListModel::dataAt(int i) const
                {
                if ( i >= 0 && i < rowCount() )
                return stringList().at( i );

                return QString();
                

                }@

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  dmcr
                  wrote on last edited by
                  #8

                  For a simple stringList you don't need to inherit the model, you can just use QVariant::fromValue(yourList).

                  Recently i had a same problem, and i didn't use a model but a simple QList<QObject*>, and it works (however you had to call
                  @view.rootContext()->setContextProperty("myVar", QVariant::fromValue(list));@ at every change)

                  dmcr

                  1 Reply Last reply
                  0
                  • X Offline
                    X Offline
                    xardas008
                    wrote on last edited by
                    #9

                    Ok the version of Zamx is working. But only if I add the items in the same thread the GUI lives in.
                    When the model will be filled with items in another thread, then my program will crash because the parent of QModelIndex will be null which seems to be quite strange.
                    Any idea how I can get the model work in another thread?

                    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