如何对自定义键盘快捷方式进行编程 [英] How to Program custom Keyboard Shortcuts

查看:384
本文介绍了如何对自定义键盘快捷方式进行编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Linux上有一个Qt应用程序.

I have a Qt application on Linux.

我想编写自定义的键盘快捷方式,例如 CTRL - Q ,这些快捷方式然后将调用一个退出程序的子例程.

I'd like to program custom keyboard shortcuts such as CTRL-Q which will then call a subroutine which quits the program.

我该怎么做?

推荐答案

尝试一下:

new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q), this, SLOT(close()));

您可以在表单的构造器中创建它.这样可以避免使用指针访问快捷方式来污染您的类.如果以后要访问它,可能仍要添加指向该快捷方式的指针.该应用程序退出后,该快捷方式将成为父项,因此将被删除.它会自动建立连接,您无需手动进行连接.

You can create it in the contructor of your form. This allows to avoid polluting your class with a pointer to access the shortcut. You may still want to add a pointer to the shortcut if you want to access it later on. The shortcut will be deleted when the application exits, since it is parented to it. It automatically does the connection, you don't have to do it manually.

还要注意,在Windows上没有默认的Ctrl + Q序列,但是在Linux和MacOS上却没有默认的Ctrl + Q序列.

Also note that there is no default Ctrl+Q sequence on Windows, but there is one on Linux and MacOS.

这篇关于如何对自定义键盘快捷方式进行编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆