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. QAbstractListModel: How to use insertRows/removeRows if data is managed outside

QAbstractListModel: How to use insertRows/removeRows if data is managed outside

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 3 Posters 695 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.
  • M Offline
    M Offline
    maxwell31
    wrote on last edited by
    #1

    Hi,

    I want to make a QAbstractListModel which is a member of a class. Inside the model I would like to store a pointer to the actual data:

    	std::vector<double> * _pos;
    

    I understand how to implement rowCount and data but I wonder what I should do withinsertRows and removeRows if the contents of _pos are managed outside the model.

    The implementation of my data function looks like this:

    	case Position:
    		return  QVariant::fromValue(_pos->at(index.row()));
    
    JKSHJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      How exactly is that data managed ?
      Where does it come from ?

      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
      • M maxwell31

        Hi,

        I want to make a QAbstractListModel which is a member of a class. Inside the model I would like to store a pointer to the actual data:

        	std::vector<double> * _pos;
        

        I understand how to implement rowCount and data but I wonder what I should do withinsertRows and removeRows if the contents of _pos are managed outside the model.

        The implementation of my data function looks like this:

        	case Position:
        		return  QVariant::fromValue(_pos->at(index.row()));
        
        JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #3

        @maxwell31 said in QAbstractListModel: How to use insertRows/removeRows if data is managed outside:

        Inside the model I would like to store a pointer to the actual data:

        How does your model know when the data has changed externally?

        @maxwell31 said in QAbstractListModel: How to use insertRows/removeRows if data is managed outside:

        I wonder what I should do withinsertRows and removeRows if the contents of _pos are managed outside the model.

        It should either

        • Modify the data and return true, OR
        • Do nothing and return false

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        1
        • M Offline
          M Offline
          maxwell31
          wrote on last edited by
          #4

          Well, maybe it would be bad design anyway, but this is how I thought it could be:

          I have a c++ class, which has e.g. a std::vector<double> which I want to expose to qml via a QAbstractListModel. In this backend class, items can be added, modified, and deleted. I thought that instead of mirroring the vector inside my QAbstractListModel I could simply use the pointer to this vector in the QAbstractListModel, and call the necessary signals of QAbstractListModel in the backend class

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

            That's correct. Now the question are:

            1. Where is that backend class modified in your code ?
            2. What will modify the content of that vector ?

            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

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved