简体   繁体   English

自定义QTreeView项目的复选框

[英]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. 我已成功将复选框添加到QTreeView的项目中,但我在定制它们时遇到了很大问题 - 我需要为已检查状态显示自定义图像,而为未检查状态显示另一个图像。

The place I concluded this can be done is in my subclass of QStyledItemDelegate, in the paint event (ie CheckBoxItemDelegate::paint). 我得出结论的地方可以在我的子类QStyledItemDelegate中完成,在paint事件中(即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. - 我不知道如何获取项目的图标(给定其QModelIndex)来绘制它。

PS 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. PS我已经将QTreeView(显然)子类化,并且,当我使用QFileSystemModel时,我也将其子类化,以便为其添加复选框功能。

Can anybody help me, please? 请问有人帮帮我吗? Is QStyledItemDelegate::paint the proper place for changing the visuals of the checkbox of the tree items? QStyledItemDelegate ::绘制适当的位置来更改树项复选框的视觉效果吗? If yes, can you please give me a small example or something, how I can do that? 如果是的话,你能给我一个小例子,我怎么能这样做?

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: 这就是我在QTreeView中使用两个图像(睁眼/闭眼,表示其可见性状态)而不是复选框来呈现可检查项目的方式:

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. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM