如何使用 Qt 创建自定义快捷方式?
在我的应用程序中,有一个按钮,当您单击它时,主窗口将被隐藏,然后如果您键入快捷方式,主窗口将再次显示。
但捷径不起作用。
这是我的快捷方式代码:
QShortcut *shortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_K), this);
QObject::connect(shortcut, &QShortcut::activated, this, &MainWindow::openWindow);
In my app there is button that when you click on it the main window will be hid then if you type a shortcut the main window will show again.
But the shortcut doesn't work.
Here's my code for the shortcut:
QShortcut *shortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_K), this);
QObject::connect(shortcut, &QShortcut::activated, this, &MainWindow::openWindow);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要添加
快捷方式
,您需要设置其父this
。如果关闭或隐藏 MainWindow 意味着快捷方式无法工作,因为未调用或找到其父窗口。
当您关闭或隐藏窗口时,会显示另一个屏幕(例如桌面),因此快捷方式不起作用。
例如,如果您编写以下代码:
当您按 Ctrl+k 时,您会看到主窗口将隐藏并且快捷方式可以正常工作。
但之后,您的桌面将出现并且其快捷方式将起作用。
For adding
shortcut
you set its parentthis
.If you close or hide MainWindow means that the shortcut couldn't work because its parent was not called or found.
When you close or hide the window, another screen like your desktop is displayed, so the shortcut does not work.
For example if you write this code :
when you press Ctrl+k you see that your main window will hide and the shortcut works correctly.
but after that, your desktop will appear and its shortcut will work.