使用 PyQT,如何过滤具有自定义列表的 QComboBox 的 mousePressEvent

发布于 2024-07-14 08:11:43 字数 462 浏览 8 评论 0原文

我有一个带有自定义列表对象的 QComboBox。

Screenshot.

自定义列表对象具有自定义 mousePressEvent,以便当用户单击其中之一时带有 +/-(扭曲)的圆圈,列表会展开/折叠。

当我将列表与组合框一起使用时,当用户单击扭曲时,列表会展开/折叠,但选择会更改,并且列表会隐藏。 我如何过滤它,以便当用户单击扭曲时,选择不会更改,并且列表不会隐藏。

其他屏幕截图

所有节点都崩溃了: 所有节点折叠。

列表隐藏: 隐藏列表的屏幕截图。

I've got a QComboBox with a custom list object.

Screenshot.

The custom list object has a custom mousePressEvent so that when the user click on one of the circles with a +/- (a twisty), the list is expanded/collapsed.

When I use the list with the combo box, when the user clicks on a twisty, the list is expanded/collapsed, but the selection is changed, and the list is hidden. How can I filter this so that when the user click on a twisty, the selection is not changed, and the list not hidden.

Additional screenshots

All of the nodes collapsed:
All nodes collapsed.

List hidden:
Screenshot with the list hidden.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

<逆流佳人身旁 2024-07-21 08:11:43

QT 有一个“捕获”QEvent.MouseButtonReleaseeventFilter。 因此,我所做的就是安装我自己的 eventFilter,如果用户单击节点,它会过滤 QEvent.MouseButtonRelease 事件。

在我的列表对象中,我有以下方法:

def mousePressEvent (self, e):
    self.colapse_expand_click = False
    if <user clicked node>:
        colapse_expand_node()
        e.accept ()
        self.colapse_expand_click = True

mousePressEventmouseReleaseEvent 之前运行。

然后在自定义组合框中,我过滤事件:

class RevisionSelectorWidget(QtGui.QComboBox):
    def __init__(self, parent = None):
        QtGui.QComboBox.__init__(self, parent)

        self.log_list = RevisionSelectorLogList(self)
        self.setView(self.log_list)
        self.log_list.installEventFilter(self)
        self.log_list.viewport().installEventFilter(self)

    def eventFilter(self, object, event):
        if event.type() == QtCore.QEvent.MouseButtonRelease:
            if self.log_list.colapse_expand_click:
                return True
        return False

QT has a eventFilter that "captures" QEvent.MouseButtonRelease. So what I have done is installed my own eventFilter that filters QEvent.MouseButtonRelease events if the user click on a node.

In my list object I have the following method:

def mousePressEvent (self, e):
    self.colapse_expand_click = False
    if <user clicked node>:
        colapse_expand_node()
        e.accept ()
        self.colapse_expand_click = True

The mousePressEvent runs before mouseReleaseEvent.

Then in the custom combobox, I filter the event:

class RevisionSelectorWidget(QtGui.QComboBox):
    def __init__(self, parent = None):
        QtGui.QComboBox.__init__(self, parent)

        self.log_list = RevisionSelectorLogList(self)
        self.setView(self.log_list)
        self.log_list.installEventFilter(self)
        self.log_list.viewport().installEventFilter(self)

    def eventFilter(self, object, event):
        if event.type() == QtCore.QEvent.MouseButtonRelease:
            if self.log_list.colapse_expand_click:
                return True
        return False
甚是思念 2024-07-21 08:11:43

在我的脑海中,您可以子类 QComboBox 并覆盖 hideEvent(QHideEvent) (继承自 QWidget

def hideEvent(self, event):
  if self.OkToHide():
    event.accept()
  else:
    event.ignore()

您的屏幕截图看起来像是一个有趣的用途组合框,我很好奇为什么你没有使用 TreeView 样式控件而不是列表?

编辑(2009年3月14日):

我查看了Qt源代码,看起来当捕获键盘和鼠标事件时,一旦qt决定发出“activated( int index)" 信号,"hidePopup()" 已被调用。

因此,除了重写事件过滤器代码之外,另一个选择是将 "activated(int index)""highlighted(int index)" 信号连接到一个插槽,该插槽可以调用 "showPopup()" 这将重新引发列表项。 如果您遇到令人讨厌的消失/出现绘画问题,您可能必须让 Qt 在弹出窗口可见时延迟绘画事件。

希望有帮助!

Off the top of my head, you could subclass QComboBox and override hideEvent(QHideEvent) (inherited from QWidget)

def hideEvent(self, event):
  if self.OkToHide():
    event.accept()
  else:
    event.ignore()

Your screenshot looks like an interesting use of a combo box, I'm curious as to why you haven't used a TreeView style control instead of a list?

Edit (Mar 14 2009):

I looked at the Qt source code and it looks like when the keyboard and mouse events are captured, that as soon as qt has decided to emit the "activated(int index)" signal, "hidePopup()" has been called.

So apart from rewriting their event filter code, another option is to connect the "activated(int index)" or "highlighted(int index)" signal to a slot that can call "showPopup()" which would re-raise the list items. If you get a nasty disappear/appear paint issue you may have to get Qt to delay the paint events while the popup is visible.

Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文