Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Implementing a QAbstractItemModel that reacts to external changes
Forum Updated to NodeBB v4.3 + New Features

Implementing a QAbstractItemModel that reacts to external changes

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 5 Posters 873 Views 4 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.
  • G Offline
    G Offline
    Glax
    wrote on last edited by
    #1

    I'm writing a QAbstractItemModel for a tree view that represents a hierarchy of QObjects.
    I'm storing the pointer to the QObject as the internal pointer in the model indexes.

    Sometimes the objects are added/removed from the hierarchy (always outside the view / item model) and I want the model to notify the view about the change.

    The problem I'm having is that functions like beginRemoveRows etc say that they need to be called before the removal has happened and functions like endRemoveRows should happen after the data has been removed.

    Since I'm reacting to changes happening outside the model this isn't possible as I can be notified of the change having already happened in the storage.
    I tried just calling the begin/end functions but doing that causes the model to call parent() passing indexes with objects which have already been deleted.

    Is there any way around this issue?

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Unfortunately it's a common problem when integrating with external data source.
      If you have a way to modify that storage the cleanest way is to provide some sort of callback before you modify the storage - that's how Qt model is designed to work.

      If you can't do that there are layers of increasing ugliness that you can try.

      In the past I've dealt with this problem in different ways depending on what I had to work with.
      In one case I had an internal structure in the model that held info about the integrity of the objects and carefully checked it before every access in the model.
      In another case I could modify the objects themselves to send an aboutToBeDestroyed() signal.
      Yet another time I had to wrap all the calls that modified the 3rd party storage with my own functions that emitted appropriate signals.

      I realize it's sort of a hand wave, but it's hard to suggest a specific solution that will work in your case without knowing more details about what you have access to.

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

        Hi and welcome to devnet,

        Can you explain a bit more how these changes happen ? What kind of data structure are you using ?

        One way would be for the data structure to have an API that you can use in your model to keep things in sync.

        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
        • A Offline
          A Offline
          adomanim
          Banned
          wrote on last edited by JKSH
          #4
          This post is deleted!
          Chris KawaC 1 Reply Last reply
          0
          • A adomanim

            This post is deleted!

            Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by Chris Kawa
            #5

            @adomanim
            beginModelReset() is just like beginInsertRows() in the sense that the data needs to still be valid at the point where it is called, so calling it after the change already occurred is the same error as calling beginInsertRows(). Some views might handle it fine, just like some views might handle beginInsertRows() on modified data fine, but you should never rely on that, as it breaks the API contract.
            destroyed() is called from QObject's destructor meaning it's often unsuitable if you need to access anything from the derived class (destructors of the derived already executed). It could be useful though if you use it like you said - as a key in some sort of map used for validation. Unfortunately that's often only part of the puzzle.

            JKSHJ 1 Reply Last reply
            2
            • Chris KawaC Chris Kawa

              @adomanim
              beginModelReset() is just like beginInsertRows() in the sense that the data needs to still be valid at the point where it is called, so calling it after the change already occurred is the same error as calling beginInsertRows(). Some views might handle it fine, just like some views might handle beginInsertRows() on modified data fine, but you should never rely on that, as it breaks the API contract.
              destroyed() is called from QObject's destructor meaning it's often unsuitable if you need to access anything from the derived class (destructors of the derived already executed). It could be useful though if you use it like you said - as a key in some sort of map used for validation. Unfortunately that's often only part of the puzzle.

              JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by
              #6

              @Chris-Kawa The post before you was made by a spammer; it has been deleted.

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

              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