QTreeView: Styling contents



  • Hello,

    Is it possible to style the contents of a QtreeView using style sheets?

    The docs show examples of styling the tree widget, but necessarily the items.
    http://doc.qt.nokia.com/4.7/stylesheet-examples.html#customizing-qtreeview

    For instance, certain columns should have bold text or custom checkbox images.

    Failing that, can I supply a widget instance such as a QLabel, or even QPushButton, for particular data items? (Overriding the paint() method using delegates seems a bit heavy handed.)

    Thank you



  • If you have some different groups of items with different properties then you need to use delegate or at least overwrite some roles in model (like Qt::FontRole to make bold for some items). Style sheets are more generic and not connected to logic "which items should get properties"

    Regarding item delegates - No need to overwrite paint() if you would like to insert just QPushButton - just create in desired widget in createEditor() - you can insert even while complicated panel with widgets etc.

    If you need custom checkbox images - you may create QCheckBoxes in delegate and apply stylesheets to them in that place



  • [quote author="yshurik" date="1304978093"]
    Regarding item delegates - No need to overwrite paint() if you would like to insert just QPushButton - just create in desired widget in createEditor() - you can insert even while complicated panel with widgets etc.

    If you need custom checkbox images - you may create QCheckBoxes in delegate and apply stylesheets to them in that place[/quote]

    This all only works if in edit mode. all other cells will be drawn by default.
    There is the possibility of setting widgets to cells by "setIndexWidget":http://doc.qt.nokia.com/4.7/qabstractitemview.html#setIndexWidget . These widgets should be displayed the whole time.



  • If you are able to manipulate the style enough by using the styling roles, then I would go that route. It is the easiest to accomplish. You could make a QSortFilterProxyModel subclass that simply sets the required data roles for the columns you need it for (such as a bold font in a certain column). Depending on the number of items in your view, using widgets can get too expensive.

    See "this topic":http://developer.qt.nokia.com/forums/viewthread/4788/ for an example on how to use a QSFPM to assign colors to cells.


Log in to reply
 

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