Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Unsolved Implementing a QAbstractItemModel that reacts to external changes

    General and Desktop
    5
    6
    304
    Loading More Posts
    • 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
      Glax last edited by

      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 Reply Quote 0
      • Chris Kawa
        Chris Kawa Moderators last edited by

        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 Reply Quote 2
        • SGaist
          SGaist Lifetime Qt Champion last edited by

          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 Reply Quote 0
          • A
            adomanim Banned last edited by JKSH

            This post is deleted!
            Chris Kawa 1 Reply Last reply Reply Quote 0
            • Chris Kawa
              Chris Kawa Moderators @adomanim last edited by 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.

              JKSH 1 Reply Last reply Reply Quote 2
              • JKSH
                JKSH Moderators @Chris Kawa last edited by

                @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 Reply Quote 0
                • First post
                  Last post