Qt在Mac OSX下的系统菜单添加
来源:互联网 发布:宿舍管理系统java 编辑:程序博客网 时间:2024/06/05 00:12
标题:Qt在Mac OSX下的系统菜单添加
头文件:#include <QMenuBar>
方法:
QAction minAction = new QAction(tr("Min"), this);
minAction->setShortcut(QKeySequence(tr("Ctrl+M")));
QAction maxAction = new QAction(tr("Max"), this);
maxAction->setShortcut(QKeySequence(tr("Ctrl+W")));
//1. 获取菜单栏
QMenuBar menuBar = new QMenuBar(0);
//2. 给菜单栏添加菜单
QMenu *wnd = menuBar->addMenu(tr("Window"));
//3. 给菜单添加菜单项
wnd->addAction(m_minAction);
wnd->addSeparator();
wnd->addAction(m_maxAction);
可以继续给menuBar添加更多菜单:QMenu *tmp=menuBar->addMenu(...),
然后给菜单tmp添加更多项:tmp->addAction(...),
或者用tmp->addSeparator()可以给菜单添加一个分界线。
Qt帮助对添加菜单的描述如下:
QMenuBar *QMainWindow::menuBar() const
Returns the menu bar for the main window. This function creates and returns an empty menu bar if the menu bar does not exist.
If you want all windows in a Mac application to share one menu bar, don't use this function to create it, because the menu bar created here will have this QMainWindow as its parent. Instead, you must create a menu bar that does not have a parent, which you can then share among all the Mac windows. Create a parent-less menu bar this way:
QMenuBar *menuBar = new QMenuBar(0);
因此,一般会用QMenuBar(0)构造一个没有父窗口的菜单栏,而QMainWindow::menuBar()函数会构造一个
基于QMainWindow为父的菜单栏。
QAction的setShortcut(QKeySequence(tr("Ctrl+M")))会创建快捷键“Ctrl+M”进行响应该菜单项的点击,
用信号槽进行连接,比如:
QObject::connect(m_minAction, SIGNAL(triggered()), this, SLOT(onMinClicked()));
这样在按下响应快捷键或用鼠标点击菜单项时,会调用相应的槽函数,如:onMinClicked()函数。
值得是键盘上的control键[具体应用可以查看Qt的帮助文档]。而且此快捷键不能与系统预设的快捷键冲突,否则以系统预设优先。
另外,如果Qt的控件有接收按键事件(如QML中的Keys.onPressed)且该控件被激活,则设置的快捷键
也会失效,会优先调用控件的按钮事件。
2. Mac的系统菜单在应用启动时会默认出现的桌面的左上角,第一项为以应用名为菜单的项,主要
依据Mac下每个应用的Info.plist文件,他里面有个<key>CFBundleName</key> <string>appName</string>
进行确定,第一个菜单项显示的是appName,其国际化通过Info.plist的<key>CFBundleDevelopmentRegion</key> <string>English/China</string>
进行确定。
踩过的坑:
在Qt的槽函数中不能对代码进行平台宏的控制,否则,在发送信号转到槽函数时不能正常响应。
例如如下槽函数:
public slots:
#ifndef _WIN32_
void onSlotFunc();
#endif
这样即使你用QObject::connect(sender, SIGNAL(signalFunc()), reciver, SLOT(onSlotFunc));链接了信号槽,
当发送信号emit signalFunc()时,是不能转到槽函数onSlotFunc()的。
头文件:#include <QMenuBar>
方法:
QAction minAction = new QAction(tr("Min"), this);
minAction->setShortcut(QKeySequence(tr("Ctrl+M")));
QAction maxAction = new QAction(tr("Max"), this);
maxAction->setShortcut(QKeySequence(tr("Ctrl+W")));
//1. 获取菜单栏
QMenuBar menuBar = new QMenuBar(0);
//2. 给菜单栏添加菜单
QMenu *wnd = menuBar->addMenu(tr("Window"));
//3. 给菜单添加菜单项
wnd->addAction(m_minAction);
wnd->addSeparator();
wnd->addAction(m_maxAction);
可以继续给menuBar添加更多菜单:QMenu *tmp=menuBar->addMenu(...),
然后给菜单tmp添加更多项:tmp->addAction(...),
或者用tmp->addSeparator()可以给菜单添加一个分界线。
Qt帮助对添加菜单的描述如下:
QMenuBar *QMainWindow::menuBar() const
Returns the menu bar for the main window. This function creates and returns an empty menu bar if the menu bar does not exist.
If you want all windows in a Mac application to share one menu bar, don't use this function to create it, because the menu bar created here will have this QMainWindow as its parent. Instead, you must create a menu bar that does not have a parent, which you can then share among all the Mac windows. Create a parent-less menu bar this way:
QMenuBar *menuBar = new QMenuBar(0);
因此,一般会用QMenuBar(0)构造一个没有父窗口的菜单栏,而QMainWindow::menuBar()函数会构造一个
基于QMainWindow为父的菜单栏。
QAction的setShortcut(QKeySequence(tr("Ctrl+M")))会创建快捷键“Ctrl+M”进行响应该菜单项的点击,
用信号槽进行连接,比如:
QObject::connect(m_minAction, SIGNAL(triggered()), this, SLOT(onMinClicked()));
这样在按下响应快捷键或用鼠标点击菜单项时,会调用相应的槽函数,如:onMinClicked()函数。
效果如下:
注意:1. Mac OSX下在设快捷键时,Ctrl、Qt::CTRL等的Ctrl都指的是command键,而Qt::Key_Meta才值得是键盘上的control键[具体应用可以查看Qt的帮助文档]。而且此快捷键不能与系统预设的快捷键冲突,否则以系统预设优先。
另外,如果Qt的控件有接收按键事件(如QML中的Keys.onPressed)且该控件被激活,则设置的快捷键
也会失效,会优先调用控件的按钮事件。
2. Mac的系统菜单在应用启动时会默认出现的桌面的左上角,第一项为以应用名为菜单的项,主要
依据Mac下每个应用的Info.plist文件,他里面有个<key>CFBundleName</key> <string>appName</string>
进行确定,第一个菜单项显示的是appName,其国际化通过Info.plist的<key>CFBundleDevelopmentRegion</key> <string>English/China</string>
进行确定。
踩过的坑:
在Qt的槽函数中不能对代码进行平台宏的控制,否则,在发送信号转到槽函数时不能正常响应。
例如如下槽函数:
public slots:
#ifndef _WIN32_
void onSlotFunc();
#endif
这样即使你用QObject::connect(sender, SIGNAL(signalFunc()), reciver, SLOT(onSlotFunc));链接了信号槽,
当发送信号emit signalFunc()时,是不能转到槽函数onSlotFunc()的。
1 0
- Qt在Mac OSX下的系统菜单添加
- 在Mac OSX系统下安装Tomcat
- 如何在Mac OSX系统下安装Tomcat
- Qt 在mac 下添加第三方 framework的方法
- Mac osx 10.10系统下MySQL-Python的配置
- 在MAC OSX下对文件加密的技巧
- Scrapy框架在mac osx下的安装
- mac下virtualbox安装osx系统
- 如何在Mac OSX下编译 ACE
- Mac OSX 系统下 android studio 停留在Feching Android SDK component information
- OSX: Mac系统认证服务的API
- mac osx系统的ftp客户端:FileZilla
- 在MAC下的QT配置openCV
- Mac osX下的Xcode编程
- mac osx下环境变量的加载顺序
- MAC osx下的plist文件读写
- 关于mac OSX下的sed
- Mac OSx下的APK反编译
- 算法时间复杂度的计算
- 25.OpenGL--帧缓冲区
- 11款最棒的Linux数据恢复工具
- NYOJ - 119 - 士兵杀敌(三) ( RMQ问题 )
- 编程常用书籍追踪列表
- Qt在Mac OSX下的系统菜单添加
- android listview子控件点击事件(点赞,收藏功能)
- ArcGIS自定义坐标变换中的方法说明
- 图片懒加载(移动端)
- Tomcat SSL / TLS (HTTPS) 配置
- JSON Web Token - 在Web应用间安全地传递信息
- php之摘录
- 去耦电容
- 张小龙首次全面阐述小程序