Qt如何禁用QComboBox的鼠标滚动?

Alb*_*lia 7 mouse qt scroll qcombobox

我在QTableView中有一些嵌入式QComboBox.为了使它们默认显示,我将这些索引设为"持久编辑器".但是现在每当我在它们上面滚动鼠标时它们会破坏我当前的表格选择.

所以基本上如何禁用QComboBox的鼠标滚动?

Mar*_*hke 6

当我发现这个问题时,当我试图找出(基本上)同样问题的解决方案时:在我的情况下,我想在pyside(python QT lib)的QScrollArea中有一个QComboBox.

这里是我重新定义的QComboBox类:

#this combo box scrolls only if opend before.
#if the mouse is over the combobox and the mousewheel is turned,
# the mousewheel event of the scrollWidget is triggered
class MyQComboBox(QtGui.QComboBox):
    def __init__(self, scrollWidget=None, *args, **kwargs):
        super(MyQComboBox, self).__init__(*args, **kwargs)  
        self.scrollWidget=scrollWidget
        self.setFocusPolicy(QtCore.Qt.StrongFocus)

    def wheelEvent(self, *args, **kwargs):
        if self.hasFocus():
            return QtGui.QComboBox.wheelEvent(self, *args, **kwargs)
        else:
            return self.scrollWidget.wheelEvent(*args, **kwargs)
Run Code Online (Sandbox Code Playgroud)

以这种方式可调用:

self.scrollArea = QtGui.QScrollArea(self)
self.frmScroll = QtGui.QFrame(self.scrollArea)
cmbOption = MyQComboBox(self.frmScroll)
Run Code Online (Sandbox Code Playgroud)

它基本上是emkey08的回答,链接Ralph Tandetzky指出,但这次是在python中.


Ral*_*zky 5

同样的情况也可能发生在 aQSpinBox或 中QDoubleSpinBox。在QScrollArea 内的 QSpinBox 上:如何防止 Spin Box 在滚动时窃取焦点?您可以使用代码片段找到一个非常好的解释清楚的解决方案。


Dav*_*cic 4

您应该能够通过在 QComboBox 上安装eventFilter来禁用鼠标滚轮滚动,并忽略鼠标滚轮生成的事件,或者子类化 QComboBox 并重新定义wheelEvent以不执行任何操作。