【发布时间】:2011-03-15 14:02:21
【问题描述】:
我在 QTableView 中有一些嵌入式 QComboBox。为了让它们默认显示,我将这些索引设为“持久编辑器”。但是现在每次我在它们上面滚动鼠标时,它们都会破坏我当前的表格选择。
那么基本上我怎样才能禁用 QComboBox 的鼠标滚动?
【问题讨论】:
我在 QTableView 中有一些嵌入式 QComboBox。为了让它们默认显示,我将这些索引设为“持久编辑器”。但是现在每次我在它们上面滚动鼠标时,它们都会破坏我当前的表格选择。
那么基本上我怎样才能禁用 QComboBox 的鼠标滚动?
【问题讨论】:
当我发现这个问题时,当我试图找出(基本上)相同问题的解决方案时:在我的情况下,我想在 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)
可以通过这种方式调用:
self.scrollArea = QtGui.QScrollArea(self)
self.frmScroll = QtGui.QFrame(self.scrollArea)
cmbOption = MyQComboBox(self.frmScroll)
基本上是link Ralph Tandetzky pointed out中的emkey08's answer,但这次是python。
【讨论】:
QSpinBox(将 QtGui.QComboBox 替换为 Qwidgets.QSpinBox),谢谢!
ignore()。这会将事件传递给父级,因此您不必明确指定谁接收滚动。 ``` def wheelEvent(self, event): if self.hasFocus(): return QtGui.QComboBox.wheelEvent(self, event) else: event.ignore()
QSpinBox 或 QDoubleSpinBox 也会发生同样的情况。在QSpinBox inside a QScrollArea: How to prevent Spin Box from stealing focus when scrolling? 上,您可以找到一个非常好的且解释清楚的解决方案,以解决代码 sn-ps 的问题。
【讨论】:
您应该能够通过在您的 QComboBox 上安装 eventFilter 来禁用鼠标滚轮滚动并忽略鼠标滚轮生成的事件,或者子类 QComboBox 并重新定义 wheelEvent 以不执行任何操作。
【讨论】: