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. Understanding Qt view-model architecture: when to create and how to delete indexes in QAbstractItemModel implementation?
Forum Update on Monday, May 27th 2025

Understanding Qt view-model architecture: when to create and how to delete indexes in QAbstractItemModel implementation?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.2k 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.
  • V Offline
    V Offline
    Violet Giraffe
    wrote on 22 Sept 2013, 07:48 last edited by
    #1

    I'm currently migrating my project from QTreeWidget to QtreeView, and have a lot of problems caused by poor understanding of the Qt model-view design. So far I couldn't find answers even in Qt examples.

    I've implemented my QAbstractItemModel. I'm returning strings to be viewed in the QTreeView through data method. Now, the underlying data will change in runtime. To handle this my model is subscribed to a notification that does emit dataChanged(index(0,0), index(rowCount() - 1, LastColumn));. The question is: how to create and delete QModelIndex objects? One of the Qt examples reimplements index method, so I did the same:
    @QModelIndex CFileListModel::index(int row, int column, const QModelIndex &/parent/) const
    {
    QModelIndex index = createIndex(row, column);
    return index;
    }@

    However, data is static in that example and in my case it changes at runtime. Is my index implemetation correct? What if index is called more than once for the same coordinates? Do I need to somehow delete old indexes before emitting dataChanged?

    1 Reply Last reply
    0
    • J Offline
      J Offline
      JKSH
      Moderators
      wrote on 22 Sept 2013, 12:18 last edited by
      #2

      Hi,

      If your model only returns strings, it's much easier to work with QStandardItemModel than QAbstractItemModel. It takes care lots of the details for you, so you don't even have to reimplement anything. Just call setItem() or setData() at runtime and your QTreeView will update itself -- you don't have to create indexes manually.

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

      1 Reply Last reply
      0
      • V Offline
        V Offline
        Violet Giraffe
        wrote on 22 Sept 2013, 13:23 last edited by
        #3

        Hmm. My model returns strings and icons for Qt::DecorationRole. Can QStandardItemModel handle icons?

        1 Reply Last reply
        0
        • J Offline
          J Offline
          JKSH
          Moderators
          wrote on 22 Sept 2013, 14:47 last edited by
          #4

          Yes. QStandardItem has a constructor that accepts an QIcon: http://qt-project.org/doc/qt-5.1/qtgui/qstandarditem.html#QStandardItem-3

          Or, call QStandardItem::setIcon()

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

          1 Reply Last reply
          0

          1/4

          22 Sept 2013, 07:48

          • Login

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