I've got a simple Qt app, and I just want to respond to the F12 key, regardless of which widget has focus.
Is there some easy signal or something I can hook in to?
I want to use the F12 key to toggle the main window fullscreen on/off.
Alt + F4: The Windows keyboard shortcut for closing applications, explained. Alt + F4 is a Windows keyboard shortcut that completely closes the application you're using.
The simplest way to create a shortcut for a particular widget is to construct the shortcut with a key sequence. For example: shortcut = new QShortcut(QKeySequence(tr("Ctrl+O", "File|Open")), parent); When the user types the key sequence for a given shortcut, the shortcut's activated() signal is emitted.
The correct answer is Ctrl + W. To quickly close the current application, press Alt+F4.
I haven't tried, but here is what I would do :
Create a QShortcut and make sure its context (with setContext()
) is Qt::ApplicationShortcut
.
shortcut = new QShortcut(QKeySequence(Qt::Key_F12), parent); shortcut->setContext(Qt::ApplicationShortcut);
Then you just need to connect a slot to the QShortcut::activated() signal.
If you have a "central widget" which all of the other widgets are children of, then you can simply set that as the widget argument for QShortcut.
(Python, qt5)
self.centralwidget = QtWidgets.QWidget(MainWindow) QtWidgets.QShortcut(QtGui.QKeySequence("F12"), self.centralwidget, self.goFullScreen)
I added this as an answer because the shortcut context flag: Qt.ApplicationShortcut
did not work for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With