Overriding the use of Qt.DisplayRole in a QTreeView (or any other item view)



  • Hi

    I have been trying to figure how I can make the treeview use some other role than the default DisplayRole when it's fetching data from a model for displaying.

    The reason for this is that I use my QAbstractItemModel for both a main QTreeView and a few QTableViews.
    In the treeview I have only 1 column and want to return a concatenation of 2-3 columns for it's display role.
    The table views have multiple columns and I want to return them respectively from the data function.

    It's the problem with using the same DisplayRole for both types of views since I have no way of knowing what kind of view is requesting the data from the model's data function, so I thought the simplest solution might be to use custom roles for them (e.g. 'TreeDisplayRole' and 'TableDisplayRole').

    I haven't been able to find anything about this. Neither how the view decides to use DisplayRole or how to override that functionality.

    I guess I could use a proxy model for each type of view for simply changing 'DisplayRole' to my custom role before sending the call to the underlying model's data function, but I'm thinking that might be overkill. I'm hoping I can do this by subclassing the item views at most.

    Thanks for reading and for all future replies.



  • It's not an overkill! Use a proxy model right between the tree view and the original model.



  • [quote author="peppe" date="1326633043"]It's not an overkill! Use a proxy model right between the tree view and the original model.[/quote]

    I actually started with subclassing QItemDelegate, overriding the paint function and I'm realizing that might be a more complex solution. I have successfully made it display the text the way I want it, but I haven't been able to make it show the graphical theme you get by default (mouse over effects and such).
    Looks like I don't have access to some of the functions like 'textRectangle' and 'decorationRectangle' in PyQt (I just noticed the language bindings board, a mod may move this) so it might be a little tricky.

    Would the "QIdentityProxyModel":http://developer.qt.nokia.com/doc/qt-4.8/qidentityproxymodel.html be a good choice for this?


Log in to reply
 

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