自定义QTreeView项目的复选框 [英] Customizing the checkboxes of the items of a QTreeView
问题描述
我遇到以下情况:我需要创建一个自定义树控件,其复选框也是自定义的。我通过使用样式表很容易做出树控件的大部分定制;我已经成功地添加复选框到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.
我认为这可以做的地方是在我的子类QStyledItemDelegate,在paint事件(即CheckBoxItemDelegate :: paint)。我需要的是显示文本,图标和项目的复选框。但是问题是:
- 我无法获得项目的样式(我使用样式表设置) - 否则我的文本可能用不正确的颜色绘制;
- 我不知道每个子项的rect(复选框,图标,文本);
- 我不知道如何获取一个项目的图标(给定它的QModelIndex)绘制
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.
PS我已经子类化了QTreeView(显然),并且,因为我正在使用QFileSystemModel,我已经将它子类化了,以便添加复选框功能。
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.
请帮帮我?
是QStyledItemDelegate :: paint适当的地方更改树项目的复选框的视觉效果吗?如果是,你能给我一个小例子或什么,我怎么能这样做?
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?
推荐答案
这是我如何渲染QTreeView中的可检查项目有两个图像(眼睛打开/眼睛闭合, )而不是复选框:
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.
这篇关于自定义QTreeView项目的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!