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. [Solved with Workaround] QTableView, QStandardItemModel and setRootIndex

[Solved with Workaround] QTableView, QStandardItemModel and setRootIndex

Scheduled Pinned Locked Moved General and Desktop
3 Posts 1 Posters 1.5k Views 1 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.
  • simowS Offline
    simowS Offline
    simow
    wrote on last edited by
    #1

    Hello,

    I have a hierarchical QStandardItemModel of the form:

    • Item1
      ** SubItem1
      ** SubItem2
      ** SubItem3
    • Item2
      ** SubItem1
      ** SubItem2
      ** SubItem3

    In a widget I have a QListView and a QTableView right next to each other. I supplied the model to both of the views. In the listview I now see the 1st-level items "Item1" and "Item2". I react upon selection change of the list to set the the contents of e.g. "Item1" to the table view by calling setRootIndex on the table view.

    Unfortunately the row count of the table is not updated. It stops at 2 (because the 1st-level item count still is 2) but I can see all of the Subitems.

    Is there any way to tell the QTableView to get the row count of the SubItems instead of the model?

    Here is a screenshot for clarification:
    "Screenshot":https://www.chronowerks.de/pub/qstandarditemmodel.png

    Update: Setting the QModelIndex on the QHeaderView of the QTableView did not work either:
    @ table->verticalHeader()->setRootIndex(index);@

    Let's Try To Negotiate!

    1 Reply Last reply
    0
    • simowS Offline
      simowS Offline
      simow
      wrote on last edited by
      #2

      So, after investigating the Qt source of qstandarditemmodel.cpp it turned out that the method headerData() is only aware of the rowCount() (and columnCount()) of the underlying model and does not make any difference if the root index via setRootIndex() of the view been altered.

      If one -- as in my case -- just wants the row index to be displayed in the vertical HeaderView, it works by subclassing the QStandardItemModel overriding the headerData-Member:

      @class MyItemModel : public QStandardItemModel {
      public:
      QVariant headerData( int section, Qt::Orientation orientation, int role ) const {
      if ( orientation == Qt::Vertical ) {
      QStandardItem item(QVariant(section).toString());
      return item.data(role);
      } else {
      return QStandardItemModel::headerData(section,orientation,role);
      }
      }
      };@

      Let's Try To Negotiate!

      1 Reply Last reply
      0
      • simowS Offline
        simowS Offline
        simow
        wrote on last edited by
        #3

        In case someone is interested, it seems to be a bug: https://bugreports.qt-project.org/browse/QTBUG-40560

        Let's Try To Negotiate!

        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