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. ListModel in C++ and append continuously from a thread
Qt 6.11 is out! See what's new in the release blog

ListModel in C++ and append continuously from a thread

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
12 Posts 4 Posters 7.8k Views
  • 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.
  • Q Offline
    Q Offline
    qtMJ
    wrote on last edited by
    #1

    Hi,

    From the Object ListModel Example http://doc.qt.io/qt-5/qtquick-models-objectlistmodel-example.html
    I Want to continuously append rows to the list from a thread.

    After
    ctxt->setContextProperty("myModel", QVariant::fromValue(dataList));
    I can not append more.

    some help had been appreciated!

    /MJ

    raven-worxR E 2 Replies Last reply
    0
    • Q qtMJ

      Hi,

      From the Object ListModel Example http://doc.qt.io/qt-5/qtquick-models-objectlistmodel-example.html
      I Want to continuously append rows to the list from a thread.

      After
      ctxt->setContextProperty("myModel", QVariant::fromValue(dataList));
      I can not append more.

      some help had been appreciated!

      /MJ

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @qtMJ
      thats not possible. Using a list as a model is rather more static.
      You will need to implement a custom QAbstractListModel for example.
      Or assign a whole new list as a model.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        qtMJ
        wrote on last edited by
        #3

        Thanks for you reply!

        I found this AbstractItemModel Example,
        http://doc.qt.io/qt-5/qtquick-models-abstractitemmodel-example.html

        I passed
        AnimalModel model;
        to my thread, and I managed to add to the list, but the view only update my ListView if I resize the window.
        Is this right way to go, and how can I update my view?

        /MJ

        raven-worxR E 2 Replies Last reply
        0
        • Q qtMJ

          Thanks for you reply!

          I found this AbstractItemModel Example,
          http://doc.qt.io/qt-5/qtquick-models-abstractitemmodel-example.html

          I passed
          AnimalModel model;
          to my thread, and I managed to add to the list, but the view only update my ListView if I resize the window.
          Is this right way to go, and how can I update my view?

          /MJ

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @qtMJ
          seems like you are missing some necessary signals in your model implementation when adding new rows

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • Q qtMJ

            Thanks for you reply!

            I found this AbstractItemModel Example,
            http://doc.qt.io/qt-5/qtquick-models-abstractitemmodel-example.html

            I passed
            AnimalModel model;
            to my thread, and I managed to add to the list, but the view only update my ListView if I resize the window.
            Is this right way to go, and how can I update my view?

            /MJ

            E Offline
            E Offline
            Eeli K
            wrote on last edited by
            #5

            @qtMJ Do you mean that when you scroll your list view you can't see your the newly added items but when you resize the window you can see them? Sounds like a problem in QML side. The AnimalModel example has this code:

            void AnimalModel::addAnimal(const Animal &animal)
            {
                beginInsertRows(QModelIndex(), rowCount(), rowCount());
                m_animals << animal;
                endInsertRows();
            }
            

            and if you call it to add animals to list it should send signals automatically and the view should be updated. You can try to catch the signal in QML:

            Connections{
            target: myModelObjectWhichWasSetAsContextProperty
            onRowsInserted: console.log("rows were inserted")
            }
            

            If it works there's something wrong with the view.

            1 Reply Last reply
            0
            • Q qtMJ

              Hi,

              From the Object ListModel Example http://doc.qt.io/qt-5/qtquick-models-objectlistmodel-example.html
              I Want to continuously append rows to the list from a thread.

              After
              ctxt->setContextProperty("myModel", QVariant::fromValue(dataList));
              I can not append more.

              some help had been appreciated!

              /MJ

              E Offline
              E Offline
              Eeli K
              wrote on last edited by
              #6

              @qtMJ Another different solution could be to send signal from C++ when new item is added. You can catch it in QML, get the item from C++ in some compatible data form and add to the list using append(). But a C++ model is better IMO.

              1 Reply Last reply
              0
              • Q Offline
                Q Offline
                qtMJ
                wrote on last edited by
                #7

                When I scroll the list by grab it with the mouse or resize, the list will update the new items.

                The only code I added to the example is my thread that I start in main.cpp before
                QQuickView view;

                And run like this (Here model is moved as a global, even tryed to pass it as pointer)

                void myThread::run()
                {
                int i=0;
                while (1) {
                model.addAnimal(Animal("Animal" + QString::number(++i), "Medium"));
                msleep(1000);
                }
                }

                And in my QML I just added

                Connections{
                target: myModel
                onRowsInserted: console.log("rows were inserted")
                }

                and the text shows up in my consol every second.

                E 1 Reply Last reply
                0
                • Q qtMJ

                  When I scroll the list by grab it with the mouse or resize, the list will update the new items.

                  The only code I added to the example is my thread that I start in main.cpp before
                  QQuickView view;

                  And run like this (Here model is moved as a global, even tryed to pass it as pointer)

                  void myThread::run()
                  {
                  int i=0;
                  while (1) {
                  model.addAnimal(Animal("Animal" + QString::number(++i), "Medium"));
                  msleep(1000);
                  }
                  }

                  And in my QML I just added

                  Connections{
                  target: myModel
                  onRowsInserted: console.log("rows were inserted")
                  }

                  and the text shows up in my consol every second.

                  E Offline
                  E Offline
                  Eeli K
                  wrote on last edited by
                  #8

                  @qtMJ Then there's nothing wrong, that's expected behavior. You have to scroll the view, it's not scrolled automatically. You can catch ListView's add() signal and use positionViewAtIndex() or positionViewAtEnd().

                  Q 1 Reply Last reply
                  0
                  • E Eeli K

                    @qtMJ Then there's nothing wrong, that's expected behavior. You have to scroll the view, it's not scrolled automatically. You can catch ListView's add() signal and use positionViewAtIndex() or positionViewAtEnd().

                    Q Offline
                    Q Offline
                    qtMJ
                    wrote on last edited by
                    #9

                    @Eeli-K Ok thanks,
                    I will try positionViewAtIndex()
                    but just so you did understand me right...
                    It is not that I can't see the items because they comes outside the window and don't scroll down
                    automaticly.
                    There are no item att all in the windows until I grab it or resize it.

                    1 Reply Last reply
                    0
                    • Q Offline
                      Q Offline
                      qtMJ
                      wrote on last edited by
                      #10

                      I found another example at

                      https://arunpkqt.wordpress.com/2016/12/08/qml-list-view-sort-and-filter/

                      To my model I added from the example

                      class FilterProxyModel : public QSortFilterProxyModel

                      and in my main.cpp I changed to following code

                      FilterProxyModel filterModel;
                      filterModel.setSourceModel(&model);
                      
                      QQmlApplicationEngine engine;
                      QQmlContext* ctxt = engine.rootContext();
                      ctxt->setContextProperty("myModel", &filterModel);
                      engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
                      

                      And in my QML I changed to ApplicationWindow and put my ListView in a ColumnLayout.

                      And now when it works as I wanted, I can continue and try to understand why...

                      Thanks for all help and if you think this solution is good, I can mark this post as solved
                      and continue with my project?

                      /MJ

                      1 Reply Last reply
                      0
                      • GrecKoG Offline
                        GrecKoG Offline
                        GrecKo
                        Qt Champions 2018
                        wrote on last edited by
                        #11

                        The correct solution is to not modify a model from an other thread.
                        You could either use invokeMethod or emit a signal and connect it to a function in the main thread to add data to your model.

                        E 1 Reply Last reply
                        0
                        • GrecKoG GrecKo

                          The correct solution is to not modify a model from an other thread.
                          You could either use invokeMethod or emit a signal and connect it to a function in the main thread to add data to your model.

                          E Offline
                          E Offline
                          Eeli K
                          wrote on last edited by
                          #12

                          @GrecKo said in ListModel in C++ and append continuously from a thread:

                          The correct solution is to not modify a model from an other thread.
                          You could either use invokeMethod or emit a signal and connect it to a function in the main thread to add data to your model.

                          @qtMJ Consider also whether you need a thread at all. If you need timed actions use QTimer. It's not a good programming practice to use while loop and sleep in a Qt application.

                          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