问题描述
在QTableWidget中滚动滚轮,如果遇到QSpinBox、combobox会被捕获,导致无法继续滚动,以及无意之中改变了某个值
解决方案:
spinbox->setFocusPolicy(Qt::StrongFocus); //这一行没啥用,可以不添加
spinbox->installEventFilter(this);
//this:
bool eventFilter(QObject *watched, QEvent *event)
{
if(event->type() == QEvent::Wheel && ((dynamic_cast<QDoubleSpinBox*>(watched) && !dynamic_cast<QDoubleSpinBox*>(watched)->hasFocus()) ||
(dynamic_cast<QSpinBox*>(watched) && !dynamic_cast<QSpinBox*>(watched)->hasFocus()))
){
return true;
}
return QWidget::eventFilter(watched,event);
}
过滤其鼠标事件,在滚轮事件中,如果没有获取焦点,就忽略该滚轮事件。需要使用滚轮时,只需点击以下控件获取焦点,即可使用。