PDA

View Full Version : QShortcut in QWidget



migel
9th August 2011, 11:29
From MainWindow I cretae simple QWidget which has a construct :) in the construct i have the code below to call an action on Enter (key), but that does not being called.


QShortcut *ok = new QShortcut(this);
ok->setKey(Qt::Key_Enter);
connect(ok, SIGNAL(activated()), this, SLOT(action()));

in MainWindow:


Dialog *d = new Dialog(this);
d->show();

Am I doing something wrong, The Dialog should has focus on show() right ? or not ?


when I use:


ok->setKey(QKeySequence::Delete);

works fine but there is no QKeySequence::Enter ???

Tried also

void Dialog::keyPressEvent(QKeyEvent * event)

but event->text() returns empty value


Any ideas ??

wysota
9th August 2011, 12:41
For shortcuts to work, they need to be attached to some visible and active element such as a button, menu or widget with focus. By the way, there is Key_Enter and Key_Return, make sure you're using the right one.

migel
9th August 2011, 12:44
the winner is Qt::Key_Return instead of Qt::Key_Enter

many thanks