You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

MadCSilver

Beginner

  • "MadCSilver" is male
  • "MadCSilver" started this thread

Posts: 19

Location: Moldova, Kishinev

Occupation: Engineer - programmer

  • Send private message

1

Tuesday, November 16th 2004, 7:26pm

QKeyEvent posting problem

Hello!

How to relay an event from one widget to enother?

I need to do such thing:
- grab all keyboard inputs on main widget (mWidget);
- only then press Qt::Key_Space or Qt::Key_Enter - send keyPressEvent to focused widget on mWidget

I tryed to do such thing:
mWidget::init()
{
grabKeyboard(); //to grab the keyboard inputs
};

mWidget::keyPressEvent( QKeyEvent *e )
{
if( (e->key()==Qt::Key_Space) || (e->key()==Qt::Key_Enter) )
{
QWidget* pWidget = focusWidget();
if( pWidget )
{
QKeyEvent my(QEvent::KeyPress, e->key(), 0, 0);
qApp->postEvent(pWidget, &my);
qApp->processEvents();
};
};
};

Behavior of the application became strange:
- button then pressing space turn to puched state and stay so
- on list view then press enter application quits at all

Please, can some one point me to my mistakes or misunderstandingsm and specify the correct way how to transmit an Event from one widget to another?

Posts: 2,162

Location: Graz, Austria

Occupation: Student

  • Send private message

2

Tuesday, November 16th 2004, 8:38pm

In case you want to treat all keyboard events of the application, install an event filter on the QApplication instance.

Just filter everything that is not Space or Enter
I think there is no need to posting yourself, the keyevent will be routed to the focus widget anyway.

Cheers,
_
Qt/KDE Developer
Debian User

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

3

Tuesday, November 16th 2004, 8:56pm

Here is how events in Qt work:

1. A widget gets an event. It checks if it should process it, if so, it accepts the event, otherwise it ignores it.
2. If the event was accepted at this point, then the processing ends. If not, the event is forwarded to the parent widget.
....
3. If all widgets in the hierarchy ignore the event, it is sent to the application object. The application should always accept it after (or without) processing it.

So my guess is that you should just process the key event in the focused widget and check wheater it is one of the keys you want. If not, just ignore the event and if parent widgets don't accept it by an accident, it should end up in the main window's event handler by itself.

MadCSilver

Beginner

  • "MadCSilver" is male
  • "MadCSilver" started this thread

Posts: 19

Location: Moldova, Kishinev

Occupation: Engineer - programmer

  • Send private message

4

Wednesday, November 17th 2004, 9:20am

Quoted

Originally posted by anda_skoa
Just filter everything that is not Space or Enter
I think there is no need to posting yourself, the keyevent will be routed to the focus widget anyway.


Thanks, nice idea. I'll try :)

Quoted

Originally posted by wysota
So my guess is that you should just process the key event in the focused widget and check wheater it is one of the keys you want. If not, just ignore the event and if parent widgets don't accept it by an accident, it should end up in the main window's event handler by itself.


This was the very first variant of what to do, but I don't like it because of such reason: to process keyPressEvent inside standart widget i must create a subclass of that widget and reimplement in it this keyPressEvent. This imply creation of the additional classes, writing much more additional code, plying with manual positioninig this widget on form. maybe I'm wrong but that was the only mechanism I found for that case.

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

5

Wednesday, November 17th 2004, 9:38am

Quoted

Originally posted by MadCSilver
This was the very first variant of what to do, but I don't like it because of such reason: to process keyPressEvent inside standart widget i must create a subclass of that widget and reimplement in it this keyPressEvent. This imply creation of the additional classes, writing much more additional code, plying with manual positioninig this widget on form. maybe I'm wrong but that was the only mechanism I found for that case.


Not quite... you can always put an event filter on that widget to process it in another class as part of normal event forwarding.

MadCSilver

Beginner

  • "MadCSilver" is male
  • "MadCSilver" started this thread

Posts: 19

Location: Moldova, Kishinev

Occupation: Engineer - programmer

  • Send private message

6

Wednesday, November 17th 2004, 11:08am

Quoted

Originally posted by wysota
Not quite... you can always put an event filter on that widget to process it in another class as part of normal event forwarding.


Good decision indeed. Thanks!