[Solved with Workaround] QTableView, QStandardItemModel and setRootIndex



  • 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);@



  • 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);
    }
    }
    };@



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.