如何在 PySide/PyQt 的 QListView 中选择自定义 QStyledItemDelegate? [英] How to make a custom QStyledItemDelegate selectable in a QListView in PySide/PyQt?

查看:40
本文介绍了如何在 PySide/PyQt 的 QListView 中选择自定义 QStyledItemDelegate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为我的上一个问题,已解决,我现在想让项目可选择.如前所述,我在视图中激活了各种与选择相关的参数,并且我还在模型的flags-method 中返回了core.Qt.ItemIsSelectable.但仍然 option.state &gui.QStyle.State_Selected 永远不会填满.任何帮助都受到高度赞赏.另请注意链接问题中的最小示例.

As a follow-up to my previous question, which was solved, I now want to make the items selectable. As mentioned earlier, I activated all kinds of selection-related parameters in the view and I also return core.Qt.ItemIsSelectable in the flags-method of the model. But still option.state & gui.QStyle.State_Selected is never fullfilled. Any help is highly appreciated. Please also note the minimal example in the linked question.

推荐答案

我自己解决了.问题是,自定义 QStyledItemDelegate 捕获鼠标事件,因此它不会传递给 QListView.所以在 QStyledItemDelegate.editor(Event) 中只需添加

I solved it myself. The problem was, that the custom QStyledItemDelegate catches the mouse event, so that it is not passed to the QListView. So in the QStyledItemDelegate.editor(Event) one simply needs to add

if event.type() == core.QEvent.MouseButtonPress:
    return False

现在可以使用 option.state & 在 paint() 方法中检测选择.gui.QStyle.State_Selected.

Now the selection is detectable in the paint()-method using option.state & gui.QStyle.State_Selected.

这篇关于如何在 PySide/PyQt 的 QListView 中选择自定义 QStyledItemDelegate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆