gpt4 book ai didi

c++ - 如何让 Ctrl+Q 退出没有菜单栏的 QT5 应用程序

转载 作者:搜寻专家 更新时间:2023-10-31 01:30:01 25 4
gpt4 key购买 nike

我正在开发一个用 QT5 编写的 KDE 应用程序,它没有菜单栏,也没有可见的退出按钮。我想让 Ctrl+Q 退出应用程序,而不必将操作绑定(bind)到用户可见的 GUI 元素。我在为此使用 QAction 时遇到问题:

    // Allow Ctrl+Q to quit the app
QAction *actionQuit = KStandardAction::quit(QApplication::instance(), SLOT(quit()), this);
actionQuit->setShortcut(QKeySequence(QKeySequence::Quit));

显然行不通。我做错了什么,和/或如果这种方法是错误的,那么最明智的做法是什么?

最佳答案

您需要向小部件添加actionQuit,以使其监听按键事件。假设 this 是您的小部件:

addAction(actionQuit);

此外,您可能希望将操作的快捷方式上下文设置为QApplicationShortcut,以使其在应用程序范围内使用,例如

actionQuit->setShortcutContext(QApplicationShortcut);

关于c++ - 如何让 Ctrl+Q 退出没有菜单栏的 QT5 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48717693/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com