在 QTreeView 中设置粗体行
我在 pyqt 应用程序中有一个 QTreeView 的自定义子类。我试图让用户能够突出显示和“低亮”(由于缺乏更好的术语)行。突出显示的行应具有粗体文本和(可选)不同的背景颜色。有什么想法吗?
我正在考虑将样式表作为一种选择,但到目前为止还无法使其发挥作用。如果我设置 QTreeView 的样式表:
self.setStyleSheet("QTreeView::item:selected {border: 1px solid #567dbc;}")
我无法弄清楚如何手动启用仅将所需的行保留在特定状态的“状态”。如果我尝试设置单个项目的样式表:
#modelIndex is a valid QModelIndex
modelIndex.internalPointer().setStyleSheet()
我会收到段错误。
我不相信样式表是正确的选择,我对所有想法持开放态度。谢谢!
I have a custom subclass of a QTreeView in a pyqt application. I'm trying to give the user the ability to highlight and "lowlight" (for lack of a better term) rows. Highlighted rows should have bold text and (optionally) a different background color. Any ideas?
I'm considering StyleSheets as an option, but have so far been unable to get that to work. If I set the QTreeView's stylesheet:
self.setStyleSheet("QTreeView::item:selected {border: 1px solid #567dbc;}")
I can't figure out how to manually enable 'states' that would keep only the desired rows at a particular state. If I try setting an individual item's stylesheet:
#modelIndex is a valid QModelIndex
modelIndex.internalPointer().setStyleSheet()
I get a segfault.
I'm not convinced stylesheets are the way to go, I'm open to all ideas. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从你所说的看来,最简单的解决方案是定义一个 自定义项目委托 为您的树视图并在需要时将项目字体粗细设置为粗体。请检查下面的示例是否适合您,它应该创建一个带有自定义项目委托的树视图,这将更改项目的字体样式。
希望这有帮助,问候
From what you've said it looks like the easiest solution would be to define a custom item delegate for your treeview and set items font weight to bold whenever it's needed. Pls, check if an example below would work for you, it should create a treeview with custom item delegate which would change item's font style.
hope this helps, regards
我可以想出几种方法来做到这一点。如果您有权访问模型,最简单的方法是在模型中添加一些索引的状态跟踪,并为 data() 函数中请求的角色返回正确的选项。这样做的缺点是,如果您在不同的视图中使用相同的模型,并且希望将高光保留在一个视图的本地。
第二简单的方法可能是创建一个代理模型,它跟踪数据本身,并从原始模型中获取所有其他数据。在这种情况下(您不更改原始模型的行或列),这可能非常容易。
最难的是创建一个自定义委托来跟踪应突出显示哪些行/列,并根据其正在绘制的模型索引的行/列以不同的方式绘制自身。您必须保留对委托的访问权限,以便您可以告诉它需要设置哪些行和列。您还需要处理模型更改时发生的情况。
I can think of a few ways to do this. The easiest, if you have access to the model, is to add some state tracking of the indexes in the model, and return the proper options for the roles requested in the data() function. The drawback to this is if you are using the same model in different views, and want to keep the highlights local to one view.
The second-easiest is probably to make a proxy model, which keeps track of the data itself, and gets all other data from the original model. In this situation (where you aren't changing the rows or columns of the original model) it would probably be pretty easy.
The hardest would be to make a custom delegate that keeps track of which rows/columns should be highlighted, and draws itself differently based on the row/column of the model index it is drawing. You would have to keep access to the delegate so that you could tell it which rows and columns need to be set. You would also need to deal with what happens when the model changes.