Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Respond to application-wide "hotkey" in Qt

Tags:

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.

like image 697
sidewinderguy Avatar asked Dec 03 '10 00:12

sidewinderguy


People also ask

Which shortcut keys can you use on the keyboard to close an application program?

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.

How do I create a shortcut in Qt?

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.

What is the shortcut key used for quick closing of the current application?

The correct answer is Ctrl + W. To quickly close the current application, press Alt+F4.


2 Answers

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.

like image 93
Jérôme Avatar answered Sep 28 '22 08:09

Jérôme


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.

like image 40
Prof Avatar answered Sep 28 '22 06:09

Prof