QSpoolBox里面的QScrollArea:如何防止Spin Box在滚动时偷焦点? [英] QSpinBox inside a QScrollArea: How to prevent Spin Box from stealing focus when scrolling?

查看:445
本文介绍了QSpoolBox里面的QScrollArea:如何防止Spin Box在滚动时偷焦点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控件与几个QSpinBox对象在QScrollArea。在滚动区域滚动时,所有工作正常,除非鼠标碰巧超过一个QSpinBoxes。然后QSpinBox偷焦点和轮事件操纵旋转框值,而不是滚动滚动区域。

I have a control with several QSpinBox objects inside a QScrollArea. All works fine when scrolling in the scroll area unless the mouse happens to be over one of the QSpinBoxes. Then the QSpinBox steals focus and the wheel events manipulate the spin box value rather than scrolling the scroll area.

我不想完全禁用使用鼠标滚轮操纵QSpinBox,但我只想要它发生,如果用户明确地点击或标签到QSpinBox。有没有办法防止QSpinBox从QScrollArea偷焦点?

I don't want to completely disable using the mouse wheel to manipulate the QSpinBox, but I only want it to happen if the user explicitly clicks or tabs into the QSpinBox. Is there a way to prevent the QSpinBox from stealing the focus from the QScrollArea?

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

As said in a comment to an answer below, setting Qt::StrongFocus does prevent the focus rect from appearing on the control, however it still steals the mouse wheel and adjusts the value in the spin box and stops the QScrollArea from scrolling. Same with Qt::ClickFocus.

推荐答案

尝试删除 Qt :: WheelFocus 从旋转框的 QWidget :: focusPolicy

Try removing Qt::WheelFocus from the spinbox' QWidget::focusPolicy:

spin->setFocusPolicy( Qt::StrongFocus );

此外,您需要防止wheel事件到达纺纱箱。你可以使用事件过滤器:

In addition, you need to prevent the wheel event from reaching the spinboxes. You can do that with an event filter:

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 );
}






完整性,因为这让我有90%的方式在那里:


edit from Grant Limberg for completeness as this got me 90% of the way there:

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

In addition to what mmutz said above, I needed to do a few other things. I had to create a subclass of QSpinBox and implement focusInEvent(QFocusEvent*) and focusOutEvent(QFocusEvent*). Basically, on a focusInEvent, I change the Focus Policy to Qt::WheelFocus and on the focusOutEvent I change it back to Qt::StrongFocus.

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

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

此外,事件过滤器类中的eventFilter方法实现基于当前焦点策略的spinbox子类:

Additionally, the eventFilter method implementation in the event filter class changes its behavior based on the current focus policy of the spinbox subclass:

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);
}

这篇关于QSpoolBox里面的QScrollArea:如何防止Spin Box在滚动时偷焦点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆