why does my keyEvent activate after mousepress
My program is base on a stackedWidget and I create a base widget class,all the widget in the stackedWidget are inherite form the base class,I also write a eventFilter to response the key event,codes are below
:
void KeyManage::installFilter(QWidget* pwidget)
{
qDebug()<<pwidget->objectName();
widget = pwidget;
qDebug()<<widget->children().count();
int a = 0;
foreach(QObject *obj, widget->children())
{
QWidget *item = qobject_cast<QWidget*>(obj);
qDebug()<<item->objectName();
if(a==0)
{
currentControl =item;
a=1;
}
item->installEventFilter(widget);
}
}
bool KeyManage::setCurrentControl(QWidget* control)
{
currentControl = control;
currentControl->setFocus();
}
bool KeyManage::eventFilter(QObject obj, QEvent event)
{
QKeyEvent keyEvent;
if(event->type() == QEvent::KeyPress)
{
keyEvent = (QKeyEvent)event;
if(Qt::Key_Up == keyEvent->key()||
Qt::Key_Right == keyEvent->key()||
Qt::Key_Left == keyEvent->key()||
Qt::Key_Down == keyEvent->key())
{
operateForDirectionKey(keyEvent);
return true;
}
else if (Qt::Key_Return == keyEvent->key())
{
operateForEnterKey();
}
}
else
{
return false;
}
return false;
}
but when I remove a widget and add a widget to the stackedWidget , when I start the program ,the first widget in the StackedWisget are response right after I press a direction key,but From the second widget ,the direction key has no effect ,I must press the mouse on the Button(only the button,label or table are still no effect),then pressing the direction key will do right.Please give me some advise ,I will very appreciate it .
Re: why does my keyEvent activate after mousepress
Key events are delivered to the focus widget.
Does the widget get focus when you switch between pages in the stack?
Also: why an event filter if you derive from the same base class anyway?
Cheers,
_
Re: why does my keyEvent activate after mousepress
Quote:
Originally Posted by
anda_skoa
Key events are delivered to the focus widget.
Does the widget get focus when you switch between pages in the stack?
Also: why an event filter if you derive from the same base class anyway?
Cheers,
_
THX,I think I did it in a right way.But How can I know the widget get the focus,or what should i do can make the widget get focus ?
Re: why does my keyEvent activate after mousepress
Try calling setFocus() in whereever your program switches the pages.
Cheers,
_
Re: why does my keyEvent activate after mousepress
Quote:
Originally Posted by
anda_skoa
Try calling setFocus() in whereever your program switches the pages.
Cheers,
_
It is still no effect,I forgotten to say something,when i switch to a new widget ,I must press a control to activate the key event ,even press on the other part of the widget,it still no effect.But I can sure the problem is the widget isnt get the focus or activate,but i have no idea to solve it.Do you think the problem due to the eventFilter written in the base class?
Re: why does my keyEvent activate after mousepress
Quote:
Originally Posted by
mike king
Do you think the problem due to the eventFilter written in the base class?
Maybe.
Hard to tell since you didn't post the actual code of your event filter.
Have you tried with proper key event handling in the base class instead of resorting to a filter?
Cheers,
_
Re: why does my keyEvent activate after mousepress
Quote:
Originally Posted by
anda_skoa
Maybe.
Hard to tell since you didn't post the actual code of your event filter.
Have you tried with proper key event handling in the base class instead of resorting to a filter?
Cheers,
_
you meant I write a keyPressEvent() in the base class? But if the KeyPressEvent written in the base class, can the event deliver to the widget from the control,because the event be filtered after the eventFilter,firstly delivered to the relate control,not to the widget.
Re: why does my keyEvent activate after mousepress
Quote:
Originally Posted by
mike king
you meant I write a keyPressEvent() in the base class?
Yes. You said that all your page widgets had the same base class.
Quote:
Originally Posted by
mike king
But if the KeyPressEvent written in the base class, can the event deliver to the widget from the control,because the event be filtered after the eventFilter,firstly delivered to the relate control,not to the widget.
You need an event filter if you need to keep events reaching their actual destinations or if the destination consumes them but you need them as well.
Maybe that is the case for you.
Cheers,
_
Re: why does my keyEvent activate after mousepress
Quote:
Originally Posted by
anda_skoa
Yes. You said that all your page widgets had the same base class.
You need an event filter if you need to keep events reaching their actual destinations or if the destination consumes them but you need them as well.
Maybe that is the case for you.
Cheers,
_
please see the code in the first reply,I written the code of eventFilter,I directly changed the focus control after key pressed。