如何验证快捷键序列是否有效?
在我的应用程序中,我允许用户在行编辑中输入快捷方式。例如,如果用户输入Ctrl + F10,这意味着如果用户按下F10,将执行特定操作。
我使用 QKeySequence 类将字符串 Ctrl + F10 转换为 QKeySequence,然后将此 QKeySequence 传递给 QShortCut 的构造函数。
有没有办法验证 QKeySequence 对于快捷方式是否有效?例如,如果用户输入 Ctrl + Shift,则会将其转换为 QKeySequence,但我不想允许将此作为快捷方式。 与 Ctrl + F10 + Ctrl 等字符串相同(变成 Ctrl),...
In my application I allow the user to enter shortcuts in a line-edit. E.g., if the user enters Ctrl + F10, this means that a certain action will be executed if the user presses F10.
I use the QKeySequence class to convert the string Ctrl + F10 to a QKeySequence, and then pass this QKeySequence to the QShortCut's constructor.
Is there a way to verify that the QKeySequence is valid for a shortcut? E.g. if the user enters Ctrl + Shift, this is converted to a QKeySequence, but I don't want to allow this for a shortcut.
Same with strings like Ctrl + F10 + Ctrl (which becomes Ctrl), ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以阻止用户自己输入快捷方式文本,并直接捕获键(您仍然需要手动过滤“孤立”修饰键)。
请参阅我该如何根据当前键盘布局从 QKeyEvent 捕获 QKeySequence? 作为一个工作示例。
You could prevent the user from entering himself the shortcut text, and capture the key directly (you would still have to filter "orphan" modifier keys manually).
See How can I capture QKeySequence from QKeyEvent depending on current keyboard layout? for a working example.
现在偶然发现了同样的问题,在调试到 qkeysequence.cpp 后,您可以检查键是否不是 Qt::Key_unknown (至少使用 5.6.3...):
just stumbled on same question now, and after having debugged into the qkeysequence.cpp you can check the keys to be not Qt::Key_unknown (at least using 5.6.3...):