PDA

View Full Version : QKeyPress or ReleaseEvent



hgedek
18th October 2007, 16:56
Hi.
I reimplemented keyPressEvent and keyReleaseEvents functions.Iam using F1,F2,F3 buttons.
User will use f1 for page_down and f2 for page_up buttons.So I need to create this events and send them?

I created these.
QKeyEvent *e = new QKeyEvent(QEvent::KeyPress,Qt::Key_PageDown, Qt::NoModifier);
QKeyEvent *e = new QKeyEvent(QEvent::KeyPress,Qt::Key_Up, Qt::NoModifier);

and

switch(key)
{
case Qt::Key_F1:
e = new QKeyEvent(QEvent::KeyPress,Qt::Key_PageDown, Qt::NoModifier);
result=QWidget::event(e);
break;
case Qt::Key_F2:
e = new QKeyEvent(QEvent::KeyPress, Qt::Key_PageUp, Qt::NoModifier);
result=QWidget::event(e);
break;
}

wrote this function.But QWidget::event(e) didnt answer my action.To which function will I send this event?

ToddAtWSU
18th October 2007, 17:17
When you capture the F1 or F2 key, why don't you just tell it to run the code Page_Up and Page_Down run? If you have to create the event and run the event, the reason you may not see the Page_Up and Page_Down happening is because you have over-ridden the keyPressEvent function and you don't do anything when you receive the Page_Up and Page_Down keys. So you will have to tell those keys what to do which takes you back to the first sentence I wrote, just tell the F1 and F2 keys what to do instead of making them create Page_Up and Page_Down events.

high_flyer
18th October 2007, 22:38
User will use f1 for page_down and f2 for page_up buttons.So I need to create this events and send them?
No you don't - they are defined by Qt already.
You only need to use them.


I created these.
QKeyEvent *e = new QKeyEvent(QEvent::KeyPress,Qt::Key_PageDown, Qt::NoModifier);
QKeyEvent *e = new QKeyEvent(QEvent::KeyPress,Qt::Key_Up, Qt::NoModifier);
Where? and how do you use them?

If you want to catch the keyPressEvent in a widget you should reimplement the keyPressedEvent() method so:


void MyWidget::keyPressEvent ( QKeyEvent * event )
{
switch(evet->key())
{
case Qt::Key_F1: //do something
accept(); //we want to handle this
break;
case Qt::Key_F2: //do something else
accept(); //we want to handle this
break;
default: ignore(); //let others handle this

}
}

its all in the docs:
http://doc.trolltech.com/4.2/qwidget.html#keyPressEvent

Widgets that accept keyboard input need to reimplement a few more event handlers:

* keyPressEvent() is called whenever a key is pressed, and again when a key has been held down long enough for it to auto-repeat. Note that the Tab and Shift+Tab keys are only passed to the widget if they are not used by the focus-change mechanisms. To force those keys to be processed by your widget, you must reimplement QWidget::event().

hgedek
19th October 2007, 08:28
I know.I have already reimplemented this function.Iam catching f1,f2,f3 presses too.But subject is that:
My keyboard on embedded device has not page_down and page_up buttons.So user will use f1 and f2 keys for this.I know that Qt event system can control these page_up and page_down keys automatically.I want to create QKeyEvents for this keys.And created.
void ProgramList::keyReleaseEvent(QKeyEvent *event)
{

Qt::Key key =(Qt::Key)event->key();
e=NULL;
bool result;

if(event->isAutoRepeat()==false)
{
switch(key)
{
case Qt::Key_F1:
e = new QKeyEvent(QEvent::KeyRelease,Qt::Key_PageDown, Qt::NoModifier);
result=QWidget::event(e);
break;
case Qt::Key_F2:
e = new QKeyEvent(QEvent::KeyRelease, Qt::Key_PageUp, Qt::NoModifier);
result=QWidget::event(e);
break;
case Qt::Key_F3:
emit setIndex(UIprogramList->treeWidget->indexOfTopLevelItem(UIprogramList->treeWidget->currentItem()));
break;
}
}
}


When released f1 and f2 my function will catch it.Then I will create an event "e".It is not f1 or f2 realased event.It is an page-up or page-down event and I am sending it to QWidget::event() function.
I want to ask that:My function doesnt catch Key_PageUp and Key_PageDown.But which function catches this?QWidget::event() ?

high_flyer
19th October 2007, 10:26
I still don't understand why do you create new events since you ALWAYS create new page up or down events on F1 for example, just do your handling when you catch F1.
But it could be I am still not getting right what you are doing.


I want to ask that:My function doesnt catch Key_PageUp and Key_PageDown.But which function catches this?QWidget::event() ?
Well, there is not case to catch Key_PageUp or Key_PageDown in your code - you should add them to the case switch in order to catch them.

ToddAtWSU
19th October 2007, 15:26
Either tell F1 to do what you want the Page_Up to do and tell the F2 to tell the Page_Down what to do.

The other option is if you must create these unnecessary events, then you will have to capture them in the same keyPressEvent handler you create adding 2 new cases to your switch and then put the code for Page_Up and Page_Down in this same event handler. This would be silly to capture 2 events to do 1 thing when you could just tell F1 and F2 what to do instead of having them create events to do the tasks.

Are you trying to pass the Page_Up and Page_Down events to different widgets? If so, then you should just have those widgets capture the F1 and F2 keys so the widget you want receiving the Page_Up and Page_Down commands capture the F1 and F2 commands.