【问题标题】:Qt How to disable mouse scrolling of QComboBox?Qt如何禁用QComboBox的鼠标滚动?
【发布时间】:2011-03-15 14:02:21
【问题描述】:

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

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

【问题讨论】:

    标签: qt scroll mouse qcombobox


    【解决方案1】:

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

    QSpinBoxQDoubleSpinBox 也会发生同样的情况。在QSpinBox inside a QScrollArea: How to prevent Spin Box from stealing focus when scrolling? 上,您可以找到一个非常好的且解释清楚的解决方案,以解决代码 sn-ps 的问题。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 我还将组合框的焦点策略更改为单击。这也有帮助。谢谢!
      • 但是,如果我将 QComboBox 翻过来,它仍然是焦点。为什么?如何预防?我不想禁用焦点,只关注轮子。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-08
      • 1970-01-01
      • 2020-07-19
      • 1970-01-01
      • 1970-01-01
      • 2014-08-23
      • 1970-01-01
      相关资源
      最近更新 更多