Customizing QTreeView Item Flags

I have the following situation: I need to create a custom tree control whose checkboxes are also configured. I easily made most of the settings for managing the tree using style sheets; I managed to add checkboxes to the QTreeView elements, but I had big problems setting them up - I need to display a custom image for the checked state, and another for the uncontrolled state.

The place where I concluded this can be done in my QStyledItemDelegate subclass, in the paint event (i.e. CheckBoxItemDelegate :: paint). I need to display the text, icon and checkbox for the item. But the problems are:
- I can’t get the style of the element (which I set using the stylesheet) - otherwise my text can be drawn with the wrong color,
- I do not know the errors of each sub-element (flag, icon, text).
- I do not know how to get the icon of an element (given its QModelIndex) to draw it.

PS I have subclassed QTreeView (obviously), and when I work with QFileSystemModel, I also subclassed it to add functionality to it.

Can someone help me please? Is QStyledItemDelegate :: draw a suitable place to change the visual checkbox elements for tree items? If so, can you give me a small example or something else, how can I do this?

+6
source share
1 answer

This is how I visualized checked items inside a QTreeView with two images (eye open / closed eye to represent their visibility state) instead of a check box:

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

Elements should be set as verifiable, of course. Hope this helps.

+7
source

Source: https://habr.com/ru/post/911435/


All Articles