Respond to application-wide “hotkey” in Qt

匆匆过客 提交于 2019-12-02 23:50:24
Jérôme

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!