Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customizing the checkboxes of the items of a QTreeView

I'm having the following situation: I need to create a custom tree control, whose checkboxes are also customized. I have easily made most of the customizations for the tree control by using style sheets; I have succeeded adding checkboxes to the QTreeView's items, but I'm having big problems with customizing them - I need to display a custom image for the checked state, and another for the unchecked state.

The place I concluded this can be done is in my subclass of QStyledItemDelegate, in the paint event (i.e. CheckBoxItemDelegate::paint). What I need is to display the text, the icon, and the checkbox for the item. But the problems are:
- I can't get the style of the item (which I set using the stylesheet) - otherwise my text can be drawn with the incorrect color;
- I don't know the rects of each subitem (the checkbox, the icon, the text);
- I don't know how to get the icon of an item (given its QModelIndex) to draw it.

P.S. I had subclassed the QTreeView (obviously), and, as I am working with QFileSystemModel, I have subclassed it too in order to add the checkbox functionality to it.

Can anybody help me, please? Is QStyledItemDelegate::paint the proper place for changing the visuals of the checkbox of the tree items? If yes, can you please give me a small example or something, how I can do that?

like image 983
Feoggou Avatar asked Mar 22 '12 17:03

Feoggou


1 Answers

This is how I rendered checkable items inside a QTreeView with two images (eye opened/eye closed, to represent their visibility state) instead of a checkbox:

ui.myTreeView->setStyleSheet(
    "QTreeView::indicator:unchecked {image: url(:/icons/eye_grey.png);}"
    "QTreeView::indicator:checked {image: url(:/icons/eye.png);}"
  );

Items should be set as checkable, of course. Hope this helps.

like image 131
Masci Avatar answered Sep 22 '22 03:09

Masci