How to make a custom QStyledItemDelegate selectable in a QListView in PySide/PyQt?

人走茶凉 提交于 2019-12-13 04:36:50

问题


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.


回答1:


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

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



来源:https://stackoverflow.com/questions/33431527/how-to-make-a-custom-qstyleditemdelegate-selectable-in-a-qlistview-in-pyside-pyq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!