2010-07-13 1230 views
6

我在QTableView中有一些嵌入的QComboBox。为了让他们默认显示,我做了这些索引“持久性编辑器”。但是现在每次我在他们上面做一个鼠标滚动时,他们都会破坏我当前的表格选择。Qt如何禁用QComboBox的鼠标滚动?

那么基本上我怎么能禁用鼠标滚动QComboBox?

回答

2

您应该可以通过在您的QComboBox上安装eventFilter来禁用鼠标滚轮,并忽略由鼠标滚轮或QComboBox子类生成的事件,并重新定义wheelEvent什么也不做。

+1

我也改变了组合框的焦点策略来点击。这也有帮助。谢谢! – 2010-07-15 15:22:41

+0

但是如果我翻转它,QComboBox仍然是对焦的。为什么?如何预防它?我不想禁用焦点,但只关注轮子。 – 18C 2017-09-03 14:30:46

2

正如我发现这个问题,当我试图找出解决方案(基本上)相同的问题:在我的情况下,我想在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) 

它基本上是emkey08's answerlink Ralph Tandetzky pointed out,但这次是在蟒蛇。

+0

这对我有'QSpinBox'(用'Qwidgets.QSpinBox'替换'QtGui.QComboBox'),谢谢! – ElDog 2018-01-11 16:55:16