PDA

View Full Version : Key Event handler



jano_alex_es
29th April 2009, 17:15
Hi,

I would like to ask what's the name, and how to implement, of the "press a key" event handler. I mean, what's the name of the function or how I can call that event handler and execute a code where a key or a key-combination from the keyboard is pressed.

I have a code, inside "void FirstClass::on_actionXxxxxx_triggered(){}" which is executed when I press a button, but I'd like to execute it also when the user presses "delete" from the keyboard.

Thanks!!!!

fullmetalcoder
29th April 2009, 21:01
Read the docs (http://doc.trolltech.com) Luke.

Seriously, this is as simple as :


subclassing the widget of interest (here I assume that you know what that means)
reimplementing the QWidget::keyPressEvent(QKeyEvent*) event handler
doing whatever you see fit in that event handler (presumably calling your slot of choice upon press of Qt::Key_Delete and forwarding the event to the handler of the base class, again assuming you know what that means)

If any of the terms above sound cryptic you'd save both our times by learning C++ before learning Qt as someone would say.

naren82
5th December 2009, 10:32
First of all look up into ur code if you are any how capturing Delete key. Check if you have used :

setShortcut(tr("Delete"));

any where in your code.

As this will capture Delete key on application level prevent the KeyPressEvent to capture Qt::Key_Delete. I have also faced the similar problem. The moment i commented this piece of line KeyPressEvent captured the Qt::Key_Delete. Thiswas the only possible way we were not able to capture key Qt::Key_Delete with keypressevent.