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. Leak memory when call setContextProperty to update list object has changed

Leak memory when call setContextProperty to update list object has changed

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
7 Posts 2 Posters 1.9k 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.
  • kidNgK Offline
    kidNgK Offline
    kidNg
    wrote on last edited by kidNg
    #1

    I use setContextProperty to send data from C++ to QML. Every times the data changes, I call setContextProperty again to update the data to QML for displaying.
    But the memory increase each time I call setContextProperty without descrease. I found that there is only one way to update data with setContextProperty.Please refer..

    Note: There is no way for the view to know that the contents of a QStringList have changed. If the QStringList changes, it will be necessary to reset the model by calling QQmlContext::setContextProperty() again.
    

    I think the problem related to the old data of las setContextProperty not free before update new data. How can I resolve this problem. Please help.
    I found my problem is similar with this post:https://forum.qt.io/topic/7729/memory-leak-or-misunderstanding-with-qlist-qobject-as-model

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Not a direct answer but since your QStringList changes over time, why not use a QStringListModel. That way when there's a change it will be communicated automatically.

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • kidNgK Offline
        kidNgK Offline
        kidNg
        wrote on last edited by
        #3

        currently, I use QList<QObject*> as model of listview. This listview contain some repeater of object model (composite design pattern). Could you please show me the way change to QListModel or something like that?
        Also, the direct solution for this problem is prefer (because my source become big now)

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Did you already saw the Using C++ Models with Qt Quick Views chapter of Qt's documentation ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • kidNgK Offline
            kidNgK Offline
            kidNg
            wrote on last edited by kidNg
            #5

            Currently, I use QQmlListProperty to export data from C++ to QML. My data has structure like below:
            grandgrandparent has list of grandparent: QList<grandparent*> in grandgrandparent class
            grandparent has list of parent: QList<parent*> in grandparent class
            parent has list of child: QList<child*> in parent class

            Currently, when I update data in grandgrandparent (QList<grandparent*>) by call setContextProperty, all data of children update automatically. The problem here is I must update data by calling setContextProperty many times and cause leak memory. Is there any way to keep this design and update changed data without leak memory?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Shouldn't you rather have a tree model to represent that ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              kidNgK 1 Reply Last reply
              0
              • SGaistS SGaist

                Shouldn't you rather have a tree model to represent that ?

                kidNgK Offline
                kidNgK Offline
                kidNg
                wrote on last edited by kidNg
                #7

                @SGaist But the grandparent, parent and child are different object.
                0_1472720446562_Untitled.png
                My model have structure and display like above picture(I have three class represents 3 object views) My model contains list grandparent object. grand parent contains lst parent object and parent object contain list child object.
                Currently, I use Listview to display list grandprarent, repeater of column to display parent view, and repeater of row to display child view.
                Current I use QQmlListProperty to store list child, list parent. QList<grandparent> is QList<QObject*> and export to QML by setContextProperty().
                With this structure data, I can modify data of child object easy and can get it from QML code.
                Could you please recommend me the proper model for this structure?

                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