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.参数文 ...
随机推荐
- Linux基础之定时任务
30.1)什么是定时任务 定时任务命令是cond,crond就是计划任务,类似于我们平时生活中的闹钟,定点执行. 30.2)为什么要用crond 计划任务主要是做一些周期性的任务,比如凌晨3点定时备份 ...
- 安装使用xen虚拟化工具
换了一家新公司,需要拿出一套虚拟化方案,就把业界的主流虚拟化技术划拉了一遍,给领导交了一份报告,具体的技术部分已经在之前的随笔里了,本篇文章主要介绍的是xen虚拟化工具的安装: Xen官方部署文档:h ...
- UE4 坐标系 坐标轴旋转轴
Pitch是围绕Y轴旋转,也叫做俯仰角. Yaw是围绕Z轴旋转,也叫偏航角. Roll是围绕X轴旋转,也叫翻滚角. UE4里,蓝图中的rotation的三个依次为roll,pitch,yaw.C++中 ...
- 后端小白的VUE入门笔记, 前端高能慎入
因为项目需要前后端分离,后端竟然不用控制view层了,页面的跳转后端不再干涉,(前端的vue经过打包后成了一张index.html) 后端只需要响应给前端json串就ok,其实这不是爽歪歪?但是觉得还 ...
- 数据结构-二叉搜索树和二叉树排序算法(python实现)
今天我们要介绍的是一种特殊的二叉树--二叉搜索树,同时我们也会讲到一种排序算法--二叉树排序算法.这两者之间有什么联系呢,我们一起来看一下吧. 开始之前呢,我们先来介绍一下如何创建一颗二叉搜索树. 假 ...
- 恐怖的Hibernate和JavaFX Table CallBack!
目录 [隐藏] 1 Hibernate 2 JavaFX Table Hibernate 最近在做 JavaFX 应用,不管再怎么避免数据持久化,但面对几十万的数据量的时候也只能乖乖的去配置持久层框架 ...
- java基础精选题
Integer比较 看下面这段有意思的代码,对数字比较敏感的小伙伴有没有发现异常? public static void main(String[] args) { Integer a = 128,b ...
- 【Java例题】1.2计算n的m次方
package study; import java.util.*; import java.math.*; public class study { public static void main( ...
- CentOS 7服务器安装brook和bbr加速
一.安装Brook 执行一键部署脚本 $ wget -N --no-check-certificate wget -N --no-check-certificate https://raw.githu ...
- WIZnet-io6Library下载及使用
概观 io6Library是一个IPv6集成库,可以轻松集成和管理使用WIZnet硬连线双TCP / IP堆栈控制器(WIZCHIP)产品系列的用户应用程序. io6Library用于管理依赖于用户特 ...