假如一个界面中有存放两个widget如w1, w2.对w2界面进行鼠标上移下移能操作。
第一种方法重写 w2,处理相关的鼠标操作
第二种就是事件过滤器, 在eventFilter可能会触发两次,pressed, realse等等
w2->installEventFilter(this);
bool eventFilter(QObject *object, QEvent *event)
{
QWidget *w1 = (QWidget*)(object);
if(w1 == w2 && QEvent::KeyPress == event->type())
{
QKeyEvent *e = (QKeyEvent*)event;
int curIndex = -1;
QPushButton *btn;
QList<QAbstractButton *> btnlist = m_pButtonGroupCoupon->buttons();
for (int i = 0; i < btnlist.length(); i++)
{
if(btnlist[i] == m_curBtn)
{
curIndex = i;
break;
}
}
if(e->key() == Qt::Key_Up)
{
curIndex -=3;
if(curIndex >= 0)
{
btn = (QPushButton *)btnlist[curIndex];
btnCheck(btn);
}
}
else if(e->key() == Qt::Key_Down)
{
curIndex +=3;
if(curIndex < btnlist.length())
{
btn = (QPushButton *)btnlist[curIndex];
btnCheck(btn);
}
}
else if(e->key() == Qt::Key_Left)
{
curIndex -= 1;
if(curIndex >= 0)
{
btn = (QPushButton *)btnlist[curIndex];
btnCheck(btn);
}
}
else if(e->key() == Qt::Key_Right)
{
curIndex +=1;
if(curIndex < btnlist.length())
{
btn = (QPushButton *)btnlist[curIndex];
btnCheck(btn);
}
}
}
return false;
}