QScrollArea 内禁用的 QToolButtons 防止滚动(PySide) [英] Disabled QToolButtons inside QScrollArea prevent scrolling (PySide)

查看:96
本文介绍了QScrollArea 内禁用的 QToolButtons 防止滚动(PySide)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论鼠标是否位于禁用的 QToolButton 上方,我都希望能够在 QScrollArea 内使用鼠标滚动.文档提到禁用的小部件不会将鼠标事件传播给父母,我发现我需要安装事件过滤器来克服我遇到的这个问题.这是我设法做到的程度.我认为事件过滤器是在检测到车轮事件时安装的,但我不确定下一步应该是什么:

I would like to be able to scroll with the mouse inside QScrollArea regardless if the mouse is positioned above disabled QToolButton or not. The documentation mentions disabled widgets don't propagate mouse events to parents and i figured it out i need to install event filter to overcome this issue i am experiencing. This is how far i managed to do it. I think the event filter is installed as wheel event is detected but i am not sure what should be the next steps:

from PySide import QtGui
from PySide.QtCore import *
import sys

app = QtGui.QApplication(sys.argv)

wid = QtGui.QWidget()
wid.resize(200, 200)

scroll = QtGui.QScrollArea()
scroll.setWidget(wid)

layout = QtGui.QVBoxLayout()
wid.setLayout(layout)

class installEvent(QObject):
    def eventFilter(self, i_obj, i_event):

        if i_event.type() == QEvent.Wheel:
            print "QEvent: " + str(i_event.type())


buttonEnabled = QtGui.QToolButton()
layout.addWidget(buttonEnabled)


buttonDisabled = QtGui.QToolButton()
buttonDisabled.setEnabled(False)
layout.addWidget(buttonDisabled)

buttonDisabled.installEventFilter(installEvent(buttonDisabled))

scroll.show()

sys.exit(app.exec_())

推荐答案

一旦您捕捉到正确的事件,您需要将事件发布到父小部件.我以前这样做过:

Once you have caught the correct event, you need to post the event to the parent widget. I've done this before like this:

def eventFilter(self, obj, event):
    if obj and not obj.isEnabled() and event.type() == QEvent.Wheel:
        newEvent = QWheelEvent(obj.mapToParent(event.pos()), event.globalPos(),
                               event.delta(), event.buttons(),
                               event.modifiers(), event.orientation())
        QApplication.instance().postEvent(obj.parent(), newEvent)
        return True

    return QObject.eventFilter(self, obj, event)

此事件过滤器仅在按钮被禁用且事件类型为 QEvent.Wheel 时执行某些操作.否则它会实现 eventFilter 的默认行为.

This event filter only does something if the button is disabled and the event type is QEvent.Wheel. Otherwise it implements the default behaviour of eventFilter.

if 语句中的登录首先将坐标映射到父小部件,然后构造一个新的 QWEheelEvent,其参数与原始事件相同(坐标映射除外).最后一部分是获取您的 QApplication 实例并使用 postEvent 方法将事件发送到 Qt 事件循环,Qt 会将事件分发到适当的小部件(禁用按钮的父级).

The login inside the if statement first maps the coordinates to the parent widget, before constructing a new QWEheelEvent with the identical parameters as the original event (except for the coordinate mapping). The final part is to grab an instance of your QApplication and use the postEvent method to send the event to the Qt event loop, where Qt will distribute the event to the appropriate widget (the parent of the disabled button).

这篇关于QScrollArea 内禁用的 QToolButtons 防止滚动(PySide)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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