QScrollArea里面的QSpinBox:滚动时如何防止Spin Box偷走焦点?

时间:2011-04-28 16:19:32

标签: c++ qt qscrollarea qspinbox

我在QScrollArea中有几个QSpinBox对象的控件。除非鼠标恰好位于其中一个QSpinBox上,否则在滚动区域滚动时一切正常。然后QSpinBox窃取焦点,轮子事件操纵旋转框值而不是滚动滚动区域。

我不想完全禁用鼠标滚轮来操纵QSpinBox,但我只希望在用户明确点击或标签进入QSpinBox时发生。有没有办法阻止QSpinBox从QScrollArea窃取焦点?

如下面的答案评论中所述,设置Qt :: StrongFocus确实会阻止焦点矩形出现在控件上,但它仍然会窃取鼠标滚轮并调整旋转框中的值并停止QScrollArea滚动。与Qt :: ClickFocus相同。

7 个答案:

答案 0 :(得分:15)

尝试从旋转框“Qt::WheelFocus

中删除QWidget::focusPolicy
spin->setFocusPolicy( Qt::StrongFocus );

此外,您需要防止车轮事件到达旋转框。您可以使用事件过滤器执行此操作:

explicit Widget( QWidget * parent=0 )
    : QWidget( parent )
{
    // setup ...
    Q_FOREACH( QSpinBox * sp, findChildren<QSpinBox*>() ) {
        sp->installEventFilter( this );
        sp->setFocusPolicy( Qt::StrongFocus );
    }

}

/* reimp */ bool eventFilter( QObject * o, QEvent * e ) {
    if ( e->type() == QEvent::Wheel &&
         qobject_cast<QAbstractSpinBox*>( o ) )
    {
        e->ignore();
        return true;
    }
    return QWidget::eventFilter( o, e );
}

编辑Grant Limberg的完整性,因为这让我有90%的路程:

除了mmutz上面所说的,我还需要做一些其他事情。我必须创建QSpinBox的子类并实现focusInEvent(QFocusEvent*)focusOutEvent(QFocusEvent*)。基本上,在focusInEvent上,我将焦点政策更改为Qt::WheelFocus,并在focusOutEvent上将其更改回Qt::StrongFocus

void MySpinBox::focusInEvent(QFocusEvent*)
{
     setFocusPolicy(Qt::WheelFocus);
}

void MySpinBox::focusOutEvent(QFocusEvent*)
{
     setFocusPolicy(Qt::StrongFocus);
}

此外,事件过滤器类中的eventFilter方法实现根据spinbox子类的当前焦点策略更改其行为:

bool eventFilter(QObject *o, QEvent *e)
{
    if(e->type() == QEvent::Wheel &&
       qobject_cast<QAbstractSpinBox*>(o))
    {
        if(qobject_cast<QAbstractSpinBox*>(o)->focusPolicy() == Qt::WheelFocus)
        {
            e->accept();
            return false;
        }
        else
        {
            e->ignore();
            return true;
        }
    }
    return QWidget::eventFilter(o, e);
}

答案 1 :(得分:13)

为了解决这个问题,我们需要关注两个以下内容:

  1. 使用鼠标滚轮,旋转框不能获得焦点。这可以通过将焦点政策设置为Qt::StrongFocus
  2. 来完成
  3. 如果已经焦点,旋转框必须仅接受滚轮事件。这可以通过在QWidget::wheelEvent子类中重新实现QSpinBox来完成。
  4. 实现此目的的MySpinBox类的完整代码:

    class MySpinBox : public QSpinBox {
    
        Q_OBJECT
    
    public:
    
        MySpinBox(QWidget *parent = 0) : QSpinBox(parent) {
            setFocusPolicy(Qt::StrongFocus);
        }
    
    protected:
    
        virtual void wheelEvent(QWheelEvent *event) {
            if (!hasFocus()) {
                event->ignore();
            } else {
                QSpinBox::wheelEvent(event);
            }
        }
    };
    

    就是这样。请注意,如果您不想创建新的QSpinBox子类,那么您也可以使用事件过滤器来解决此问题。

答案 2 :(得分:9)

我尝试解决方案。易于使用,无需子类化。

首先,我创建了一个新的辅助类:

#include <QObject>

class MouseWheelWidgetAdjustmentGuard : public QObject
{
public:
    explicit MouseWheelWidgetAdjustmentGuard(QObject *parent);

protected:
    bool eventFilter(QObject* o, QEvent* e) override;
};

#include <QEvent>
#include <QWidget>

MouseWheelWidgetAdjustmentGuard::MouseWheelWidgetAdjustmentGuard(QObject *parent) : QObject(parent)
{
}

bool MouseWheelWidgetAdjustmentGuard::eventFilter(QObject *o, QEvent *e)
{
    const QWidget* widget = static_cast<QWidget*>(o);
    if (e->type() == QEvent::Wheel && widget && !widget->hasFocus())
    {
        e->ignore();
        return true;
    }

    return QObject::eventFilter(o, e);
}

然后我在运行时或Qt Designer中将有问题的小部件的焦点策略设置为StrongFocus。 然后我安装了我的事件过滤器:

ui.comboBox->installEventFilter(new MouseWheelWidgetAdjustmentGuard(ui.comboBox));

完成。当父对象 - 组合框被破坏时,MouseWheelWidgetAdjustmentGuard将被自动删除。

答案 3 :(得分:3)

只是为了扩展,您可以使用eventFilter执行此操作,而无需派生新的QMySpinBox类型类:

bool eventFilter(QObject *obj, QEvent *event)
{
    QAbstractSpinBox* spinBox = qobject_cast<QAbstractSpinBox*>(obj);
    if(spinBox)
    {
        if(event->type() == QEvent::Wheel)
        {
            if(spinBox->focusPolicy() == Qt::WheelFocus)
            {
                event->accept();
                return false;
            }
            else
            {
                event->ignore();
                return true;
            }
        }
        else if(event->type() == QEvent::FocusIn)
        {
            spinBox->setFocusPolicy(Qt::WheelFocus);
        }
        else if(event->type() == QEvent::FocusOut)
        {
            spinBox->setFocusPolicy(Qt::StrongFocus);
        }
    }
    return QObject::eventFilter(obj, event);
}

答案 4 :(得分:2)

在本文的帮助下,我们为 Python / PySide 解决了一个问题。如果有人偶然发现这一点。就像我们一样:]

class HumbleSpinBox(QtWidgets.QDoubleSpinBox):
    def __init__(self, *args):
        super(HumbleSpinBox, self).__init__(*args)
        self.setFocusPolicy(QtCore.Qt.StrongFocus)

    def focusInEvent(self, event):
        self.setFocusPolicy(QtCore.Qt.WheelFocus)
        super(HumbleSpinBox, self).focusInEvent(event)

    def focusOutEvent(self, event):
        self.setFocusPolicy(QtCore.Qt.StrongFocus)
        super(HumbleSpinBox, self).focusOutEvent(event)

    def wheelEvent(self, event):
        if self.hasFocus():
            return super(HumbleSpinBox, self).wheelEvent(event)
        else:
            event.ignore()

答案 5 :(得分:0)

只是为了帮助任何有需要的人,它缺少一个小细节:

call focusInEvent and focusOutEvent from QSpinBox :

    void MySpinBox::focusInEvent(QFocusEvent* pEvent)
    {
        setFocusPolicy(Qt::WheelFocus);
        QSpinBox::focusInEvent(pEvent);
    }

    void MySpinBox::focusOutEvent(QFocusEvent*)
    {
        setFocusPolicy(Qt::StrongFocus);
        QSpinBox::focusOutEvent(pEvent);
    }

答案 6 :(得分:0)

这是我的紫罗兰色长颈鹿的Python PyQt5端口答案:


def preventAnnoyingSpinboxScrollBehaviour(self, control: QAbstractSpinBox) -> None:
    control.setFocusPolicy(Qt.StrongFocus)
    control.installEventFilter(self.MouseWheelWidgetAdjustmentGuard(control))

class MouseWheelWidgetAdjustmentGuard(QObject):
    def __init__(self, parent: QObject):
        super().__init__(parent)

    def eventFilter(self, o: QObject, e: QEvent) -> bool:
        widget: QWidget = o
        if e.type() == QEvent.Wheel and not widget.hasFocus():
            e.ignore()
            return True
        return super().eventFilter(o, e)