Qt程序在Mac系统更加自然

2014年10月24日 4300点热度 0人点赞 0条评论

效果预览

Qt程序在Mac系统更加自然

说明

可以看到上图中的关于,设置,退出都为Mac系统的样式,这样的样式在Qt并不难实现,只要几条简单在命令就可以完成。

代码

ui->action_About->setMenuRole(QAction::AboutRole);
ui->action_Quit->setMenuRole(QAction::QuitRole);
ui->action_SetPassword->setMenuRole(QAction::PreferencesRole);

讲解

可以看到上面关键的代码在于QAction::AboutRole,QAction::QuitRole,QAction::PreferencesRole这些命令
其实QAction它不是只有这么多在选项,具体的可以查看官方链接

Constant Value Description
QAction::NoRole 0 This action should not be put into the application menu
QAction::TextHeuristicRole 1 This action should be put in the application menu based on the action's text as described in the QMenuBar documentation.
QAction::ApplicationSpecificRole 2 his action should be put in the application menu with an application specific role
QAction::AboutQtRole 3 This action matches handles the "About Qt" menu item.
QAction::AboutRole 4 This action should be placed where the "About" menu item is in the application menu. The text of the menu item will be set to "About <application name>". The application name is fetched from the Info.plist file in the application's bundle (See Qt for Mac OS X - Deployment).
QAction::PreferencesRole 5 This action should be placed where the "Preferences..." menu item is in the application menu.
QAction::QuitRole 6 This action should be placed where the Quit menu item is in the application menu.

官方链接: http://qt-project.org/doc/qt-5/qaction.html#MenuRole-enum

Sollyu

保持饥渴的专注,追求最佳的品质