如何使用 Qt 创建自定义快捷方式?

发布于 2025-01-13 12:27:49 字数 288 浏览 4 评论 0原文

在我的应用程序中,有一个按钮,当您单击它时,主窗口将被隐藏,然后如果您键入快捷方式,主窗口将再次显示。

但捷径不起作用。

这是我的快捷方式代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦罢 2025-01-20 12:27:49

要添加快捷方式,您需要设置其父this
如果关闭或隐藏 MainWindow 意味着快捷方式无法工作,因为未调用或找到其父窗口。
当您关闭或隐藏窗口时,会显示另一个屏幕(例如桌面),因此快捷方式不起作用。

例如,如果您编写以下代码:

 shortcut = new QShortcut(QKeySequence(tr("Ctrl+k", "Open/Close window")), this);
 connect(shortcut, &QShortcut::activated, this, &MainWindow::hide);

当您按 Ctrl+k 时,您会看到主窗口将隐藏并且快捷方式可以正常工作。
但之后,您的桌面将出现并且其快捷方式将起作用。

For adding shortcut you set its parent this.
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 :

 shortcut = new QShortcut(QKeySequence(tr("Ctrl+k", "Open/Close window")), this);
 connect(shortcut, &QShortcut::activated, this, &MainWindow::hide);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文