How to use QKeyEvents in Qt

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Tested with
Devices(s): S60 Emulator
Platform(s): Qt 4.5
Keywords: QKeyEvents
Created: mind_freak (26 Jul 2009)
Last edited: hamishwillee (11 Oct 2012)


This example shows how you can send key events to the widget with keyboard input focus when keys are pressed or released. A key event contains a special accept flag that indicates whether the receiver will handle the key event.

Classes used in this code: QKeyEvents-Describes a key event.

Event Code

void my_app::clicked0 () //For tab Event
QKeyEvent key(QKeyEvent::KeyPress, Qt::Key_Tab, Qt::NoModifier, "Tab", false, 0 );
QApplication::sendEvent(this, &key);
void my_app::clicked1 () //For tab shift
QKeyEvent key(QKeyEvent::KeyPress, Qt::Key_Tab, Qt::ShiftModifier, "Tab-Shift", false, 0 );
QApplication::sendEvent(this, &key);
void my_app::clicked2 () //For space
QKeyEvent key(QKeyEvent::KeyPress, Qt::Key_Return, Qt::NoModifier, "Return", false, 0 );
QApplication::sendEvent(this, &key);
void my_app::clicked3 () //For enter Event
QKeyEvent key(QKeyEvent::KeyPress, Qt::Key_Space, Qt::NoModifier, "Space", false, 0 );
QApplication::sendEvent(this, &key);
This page was last modified on 11 October 2012, at 04:17.
120 page views in the last 30 days.