macOS平台下Qt应用程序菜单翻译及调整
一、翻译
在macOS平台上,系统会为应用程序菜单添加一些额外的菜单项。先来看一些典型的例子:

这个是Qt Creator的菜单,系统为应用程序菜单添加了一些桌面显示操作相关的菜单项;

这个是Qt Designer的应用程序菜单,也添加了一些额外的菜单项。并且我们可以注意到,这些菜单项在中文语言环境是翻译好的。那么我们自己的应用程序,怎么去让它们也翻译好呢?
Qt应用程序的国际化都是通过ts文件来做翻译的。通过lupdate程序扫描源代码中被tr()宏包裹的字符串,得到需要翻译的字符串。但是这些系统添加的字符串,并没有存在我们的源码当中。这个应该如何处理嗯?通过网上的一番搜寻发现,有一个直接且简单的方法,直接在ts文件里面添加这些字符串并做好翻译即可。如下所示:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="zh_CN">
<context>
<name>demoMainWindow</name>
<message>
<location filename="../mainwindow.ui" line="14"/>
<source>MainWindow</source>
<translation>主窗口</translation>
</message>
<message>
<location filename="../mainwindow.ui" line="28"/>
<source>File</source>
<translation>文件</translation>
</message>
<message>
<location filename="../mainwindow.ui" line="40"/>
<source>Edit</source>
<translation>编辑</translation>
</message>
<message>
<location filename="../mainwindow.ui" line="50"/>
<source>View</source>
<translation>视图</translation>
</message>
<message>
<location filename="../mainwindow.ui" line="57"/>
<source>Settings</source>
<translation>设置</translation>
</message>
<message>
<location filename="../mainwindow.ui" line="65"/>
<source>Help</source>
<translation>帮助</translation>
</message>
<message>
<location filename="../mainwindow.ui" line="81"/>
<source>Open File</source>
<translation>打开文件</translation>
</message>
<message>
<location filename="../mainwindow.ui" line="86"/>
<source>Open Project</source>
<translation>打开工程</translation>
</message>
<message>
<location filename="../mainwindow.ui" line="91"/>
<source>Open Folder</source>
<translation>打开文件夹</translation>
</message>
<message>
<location filename="../mainwindow.ui" line="96"/>
<source>Recent</source>
<translation>最近文件</translation>
</message>
<message>
<location filename="../mainwindow.ui" line="101"/>
<source>Save</source>
<translation>保存</translation>
</message>
<message>
<location filename="../mainwindow.ui" line="106"/>
<source>Quit</source>
<translation>退出</translation>
</message>
<message>
<location filename="../mainwindow.ui" line="111"/>
<source>Redo</source>
<translation>重做</translation>
</message>
<message>
<location filename="../mainwindow.ui" line="116"/>
<source>Undo</source>
<translation>撤销</translation>
</message>
<message>
<location filename="../mainwindow.ui" line="121"/>
<source>Cut</source>
<translation>剪切</translation>
</message>
<message>
<location filename="../mainwindow.ui" line="126"/>
<source>Paste</source>
<translation>粘贴</translation>
</message>
<message>
<location filename="../mainwindow.ui" line="131"/>
<source>Delete</source>
<translation>删除</translation>
</message>
<message>
<location filename="../mainwindow.ui" line="136"/>
<source>Tile</source>
<translation>平铺</translation>
</message>
<message>
<location filename="../mainwindow.ui" line="141"/>
<source>Stack</source>
<translation>堆叠</translation>
</message>
<message>
<location filename="../mainwindow.ui" line="146"/>
<source>Preference</source>
<translation>偏好设置</translation>
</message>
<message>
<location filename="../mainwindow.ui" line="151"/>
<source>Log</source>
<translation>日志</translation>
</message>
<message>
<location filename="../mainwindow.ui" line="156"/>
<source>Tools</source>
<translation>工具</translation>
</message>
<message>
<location filename="../mainwindow.ui" line="161"/>
<source>About Demo</source>
<translation>关于Demo</translation>
</message>
<message>
<location filename="../mainwindow.ui" line="166"/>
<source>Check for updates</source>
<translation>检查更新</translation>
</message>
<message>
<location filename="../mainwindow.ui" line="171"/>
<source>Website</source>
<translation>官网</translation>
</message>
<message>
<location filename="../mainwindow.ui" line="176"/>
<source>Tutorial</source>
<translation>教程</translation>
</message>
</context>
<context>
<name>MAC_APPLICATION_MENU</name>
<message>
<source>Services</source>
<translation>服务</translation>
</message>
<message>
<source>Hide %1</source>
<translation>隐藏 %1</translation>
</message>
<message>
<source>Hide Others</source>
<translation>隐藏其他</translation>
</message>
<message>
<source>Show All</source>
<translation>显示所有</translation>
</message>
<message>
<source>Preferences...</source>
<translation>偏好设置…</translation>
</message>
<message>
<source>Quit %1</source>
<translation>退出 %1</translation>
</message>
<message>
<source>About %1</source>
<translation>关于 %1</translation>
</message>
</context> </TS>
手动添加一个context,并在该context下面添加翻译。然后用lupdate更新ts文件即可。这样,我们自己的应用程序也可以翻译好了:


二、菜单项重排
为了契合不同平台的使用习惯,Qt给菜单添加了Menu Role这样一个定义。通过定义不同的Menu Role,mac系统会调整应用程序的位置,以保证平台习惯的统一。Qt库本身已经定义了几个常用的Role:

这些Role类型的菜单项都会被系统重新调整位置到应用菜单里。如果我们还有一些其他的菜单项,也想放到应用菜单里面,应该怎么做呢?其实也非常简单,直接将菜单的Role设置为ApplicationSpecificRole即可。按照设置顺序,各个菜单项将依次出现在应用菜单中。比如说上面截图中的Check for Updates菜单项,就是通过设置ApplicationSpecificRole定义来实现的。
macOS平台下Qt应用程序菜单翻译及调整的更多相关文章
- 在Windows平台下Qt的exe报错问题排查步骤
在Windows平台下Qt的exe报错问题排查步骤 工具介绍: 1. Dependency Worker Dependency Worker是一个免费的用具用来扫描任何的32bit 或者64bit 的 ...
- Windows平台下Qt QOpenGL中glutSolidSphere()方法未定义的解决方法
Windows平台下Qt中glut库的使用 用Qt中的QGLWidget窗体类中是不包括glut工具库的,难怪在myGLWidget(在我的程序中是QGLWidget的派生类)中绘制实心球体是 ...
- [修正] Firemonkey Windows & macOS 平台下 Edit & Memo 中文输入后会取消原选取文字的 BUG
问题:Firemonkey Windows & macOS 平台下 Edit & Memo 中文输入后会取消原选取文字的 BUG 适用版本:Delphi 10.1.2 & 10 ...
- macOS上实现Qt应用程序做文件关联打开
一.背景介绍 用Qt开发的应用程序要实现文件关联,双击时用默认关联的程序打开文件,在Windows上这个功能非常容易实现.Windows应用程序在安装的时候可以在注册表中写入相关的键值对.打开文件的时 ...
- Windows平台下Qt中glut库的使用
用Qt中的QGLWidget窗体类中是不包括glut工具库的,难怪在myGLWidget(在我的程序中是QGLWidget的派生类)中绘制实心球体是说“glutSolidSphere”: 找不到标识符 ...
- Windows平台下Qt开发环境的搭建
Qt 是采用开源和商用双协议发布的开放源代码的图形开发类库,现在很多图形化的开源软件都使用了Qt. 下载地址:http://qt-project.org/downloads 1. 下载安装包 你可以从 ...
- MacOS平台下@rpath在动态链接库中的应用
一.背景介绍 公司开发的一个底层库被用在了Mac平台的多个产品中.在开发这个底层库的初期,对于Mac OSX下的Install name 并没有过多的了解.对于XCode中的install name项 ...
- WINCE平台下C#应用程序中使用看门狗
看门狗定时器(WDT,Watch Dog Timer)是单片机的一个组成部分,它实际上是一个计数器,一般给看门狗一个大数,程序开始运行后看门狗开始倒计数.如果程序运行正常,过一段时间CPU应发出指令让 ...
- freecplus框架,Linux平台下C/C++程序员提高开发效率的利器
目录 一.freecplus框架简介 二.freecplus开源许可协议 三.freecplus框架内容 字符串操作 2.xml解析 3.日期时间 4.目录操作 5.文件操作 6.日志文件 7.参数文 ...
随机推荐
- bitset的简单用法
1.头文件 #include<bitset> 2.基本操作 bitset<n> b; b有n位,每位都为0. 参数n可以为一个表达式.如bitset<5> b, 则 ...
- 一个C++的ElasticSearch Client
ElasticSearch官方是没有提供C++的client的:因此决定自己写一个,命名为ESClient https://github.com/ATinyAnt/ESClient(手下留星 star ...
- x32下PsSetLoadImageNotifyRoutine的逆向
一丶简介 纯属兴趣爱好.特来逆向玩玩. PsSetLoadImageNotifyRoutine 是内核中用来监控模块加载.操作系统给我们提供的回调. 我们只需要填写对应的回调函数原型即可进行加监控. ...
- c#小灶——输出语句
前面我我们学习了如何在控制台输出一句话,今天我们学习一下更详细的输出方式. Console.WriteLine();和Console.Write(); 我们来看一下下面几行代码, using Syst ...
- javaweb入门-----jsp概念
jsp是什么? JSP:Java Server Pages java服务器端页面 *可以理解为 一个特殊的页面,其中既可以直接定义html标签,又可以定义java代码 *用于简化书写 <% %& ...
- 【Java例题】2.1复数类
1.定义复数类,包括实部和虚部变量.构造方法. 加减乘除方法.求绝对值方法和显示实部.虚部值的方法. 然后编写一个主类,在其主方法中通过定义两个复数对象来 显示每一个复数的实部值.虚部值和绝对值, 显 ...
- java 学习路线、java 入门、java自学、java 教程
以前学习知识都是用到什么学什么,不是很系统.今天看到一个网站感觉挺不错的,分享给大家. 这个页面是学习路线功能的简介,如下图 点击选择学习路线 进入后可以选择循序渐进或者由终至始 上图标出 ...
- CSS: hack 方式一览
本文引自:http://blog.csdn.net/freshlover/article/details/12132801 什么是CSS hack 由于不同厂商的流览器或某浏览器的不同版本(如IE6- ...
- python 闭包,装饰器,random,os,sys,shutil,shelve,ConfigParser,hashlib模块
闭包 def make_arerage(): l1 = [] def average(price): l1.append(price) total = sum(l1) return total/len ...
- windows查看端口被占用
1.打开控制台终端 2.在命令行下输入netstat -ano|findstr "8080"(8080是被占用的端口) 3.记住最后一列的数字PID如4684 4.输入taskli ...