问题描述
我有一个可用的应用程序.我向带有一些菜单的主窗口添加了一个 menuBar().然后,我将其隐藏以释放屏幕空间.我写了下面的代码,这样当用户按下 ALT 键时,菜单栏如果隐藏就会出现,如果显示则隐藏.
如你所见,我还添加了一个 QMessageBox 来查看 menuBar 何时获得焦点.而这个框只出现一半的时间.它是这样的:
- 应用启动,菜单栏隐藏
- 我按下 ALT,显示菜单栏,没有消息框,没有焦点
- 我按下 ALT,隐藏菜单栏
- 我按下 ALT,显示菜单栏,消息框,焦点
- 我按下 ALT,隐藏菜单栏
- 我按下 ALT,显示菜单栏,没有消息框,没有焦点
- 我按下 ALT,隐藏菜单栏
- 我按下 ALT,显示菜单栏,消息框,焦点
- 等
如何确保menuBar在显示时始终有焦点?
我想做同样的事情.我的解决方案,完整示例,作为要点:
https://gist.github.com/xim/ee56564f425151ea2fa70f7830d
针对 Qt 5.9.4 进行测试.
因为它包含很多其他垃圾,一个最小的例子:
I have a working application. I added a menuBar() to the main window with some menus. Then, I hid it to free screen space. I wrote the code below so that when user presses ALT key, the menu bar appears if it's hidden, and it hides if it's displayed.
As you can see, I also added a QMessageBox to see when the menuBar has the focus. And this box appears only half of the time. It goes like this :
- Application launched, menubar hidden
- I press ALT, menubar displayed, no message box, no focus
- I press ALT, menubar hidden
- I press ALT, menubar displayed, message box, focus
- I press ALT, menubar hidden
- I press ALT, menubar displayed, no message box, no focus
- I press ALT, menubar hidden
- I press ALT, menubar displayed, message box, focus
- etc.
How to make sure when the menuBar is displayed, it always has focus ?
I wanted to do the same thing. My solution, complete example, as a gist:
https://gist.github.com/xim/ee56564f425151ea2fa70f730d644873
Tested against Qt 5.9.4.
As it contains a lot of other junk, a minimal example:
这篇关于如何使用 Qt 聚焦 menuBar()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!