Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Span cells in QTableView from model



  • I want to achieve something like this (image from the web):

    alt text

    where each "section" spans over all the columns. This should be accomplished by using the void QTableView::setSpan(int row, int column, int rowSpanCount, int columnSpanCount) method. But the UI has no way to know which rows are section. The model knows, i.e. I have an hidden column that allows me to distinguish if the current row is either a section row or data one.

    So, what is the best way to bring up this information to the UI in order to set the span?

    My (ugly*) thought: whenever the model changes, first clear all the spans, then cycle the rows one by one, get the model data, check the hidden field and set the span according.

    *ugly, but it actually works!


  • Lifetime Qt Champion

    HI
    It's not possible to use UserRole for this? (custom role)
    So so it's like EditROle and Decoration role and a view can choose to honor it.



  • @mrjj said in Span cells in QTableView from model:

    HI
    It's not possible to use UserRole for this? (custom role)
    So so it's like EditROle and Decoration role and a view can choose to honor it.

    As far as I know with DecorationRole and the other display ones (i.e. BackgroundRole) you can easily change the appearance in terms of colors and icons. I don't know if you can also suppress the cell borders as well.


  • Lifetime Qt Champion

    Hi
    I mean to flag a row if should be a section type.
    Would be less convoluted than having a hidden col. (IMHO)



  • The KDE API has a very similar functionality using KCategorizedSortFilterProxyModel/KCategorizedView/KCategoryDrawer: https://api.kde.org/frameworks/kitemviews/html/annotated.html but it implements only a list view, not a table.
    The alternative is to use a tree model/view, set the categories as top items and the rest as leaves of the tree


Log in to reply