QPushButton & shortcut

I have a problem with Qt 4.6.0 and a shortcut for QPushButtons :

I want to display special text in QTextEdit when the user presses the button button , but only when the button pressed, as soon as it is released, I need another text.

Everything works fine, but now I want to add a shortcut (say F1 ) to perform the same operation, when I press F1 , it displays something special in QTextEdit 'until I release the key . How should I do it?

I added shortCut to my button , but when I press F1 it flickers, it likes while I press F1 , a lot of signals sent ... I want my QTextEdit when I press F1 , and then change back when I release key ...

Hope my question is clear!

Thanks so much for your advice!

+4
source share
2 answers

I think the easiest solution to this problem is to use installEventFilter() for the parent object (window) and filter the QEvent::MouseButtonPress and QEvent::MouseButtonRelease .

 bool MainWindow::eventFilter(QObject *obj, QEvent *event) { if (obj == textEdit) { if (event->type() == QEvent::KeyPress) { QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event); qDebug() << "Ate key press" << keyEvent->key(); return true; } else { return false; } } else { // pass the event on to the parent class return QMainWindow::eventFilter(obj, event); } } 
+4
source

The button presses the () button when it is activated with the mouse, spacebar, or keyboard shortcut. You will need to handle keystrokes and keys to do what you want.

+2
source

All Articles