Qt (简体中文)

Qt 是一个跨平台的应用程序和组件工具,使用标准 C++编写,通过大量使用代码生成器 Meta Object Compiler(moc)以及数个宏来扩展语言的功能。它有一些更重要的特性包括:

  • 支持各种主流桌面平台和部分手机平台。
  • 完善的国际化支持。
  • 提供 SQL 数据访问、XML 解析、线程管理、网络支持和统一的文件处理跨平台应用编程接口。
翻译状态:本文是 Qt翻译。上次翻译日期:2019-03-01。如果英文版本有所更改,则您可以帮助同步翻译。

Qt 框架是 KDE 软件社区和其它一些重要开源和闭源应用的基石,例如 VLCVirtualBoxOperaMathematica 等等。

安装

现在官方源中有四个版本的Qt,能用以下软件包来安装

  • Qt 6.x:软件包 qt6-base,文档包是 qt6-doc
  • Qt 5.x:软件包 ,文档包是 qt5-doc
  • Qt 4.x:软件包 ,文档包是 qt4-docAUR
  • Qt 3.x:在软件包 ,文档包是 。

默认 Qt 库

Qt软件包不再提供通常的二进制文件/usr/bin/qmake,而是将它们改名为 qmake-qt5, qmake-qt4, qmake-qt3 软链接,这可能导致 Qt3/4 程序编译失败。安装可以恢复 /usr/bin/qmake 并设置要使用的 Qt 版本,默认是使用 Qt5。

修改环境变量

可以通过 环境变量 设置默认的 QT. 例如要使用 Qt4,可以设置 export QT_SELECT=4

使用配置文件

创建 软链接,链接到目录中需要的 .conf 文件上。例如要使用 Qt4,将 软链接到 。

$ ln -s  

外观

Qt5

Qt5基于当前使用的桌面环境来决定所使用的样式:

  • 在 KDE Plasma 桌面环境中, 呈现实际选择的Qt风格. 可以在 KDE System Settings (systemsettings)中更改, 这项设置的具体位置是 Appearance > Application Style > Widget Style.
  • 在 Cinnamon, GNOME, MATE, LXDE, Xfce 等桌面环境中, 呈现GTK风格 (QGtkStyle).
  • 在其他桌面环境中, 呈现 Fusion 风格.

如果要强制指定一种样式,你可以设置环境变量(environment variable)。特别的,如果你想要使用GTK主题,把它设置成(注意:你将需要安装在下文中提到的Qt样式插件来获取GTK样式)。Qt5应用同时也支持标志,你可以用它来使用指定的样式运行一个Qt5应用程序。

Qt5中自带两种样式: Fusion, Windows. 其他的可以通过官方仓库安装:

  • Breeze Artwork, styles and assets for the Breeze visual style for the Plasma Desktop.
https://projects.kde.org/projects/kde/workspace/breeze || breeze
  • Qt style plugins Additional style plugins for Qt5, including GTK, Cleanlooks, Motif, Plastique.
https://code.qt.io/cgit/qt/qtstyleplugins.git || qt5-stylepluginsAUR

Qt4

Qt4 应用程序会尝试模仿所运行的桌面环境的行为,除非碰到了某些问题或者进行了强制配置。要修改 Qt 程序的外观,可以使用 Qt 配置工具( 或 ).

尽管不是 Qt 的一部分,KDE 系统设置 提供了许多定制设置,Qt 程序也会使用这些设置。

  • 在 KDE Plasma 桌面环境中, 呈现实际选择的Qt风格. 可以在 KDE System Settings (systemsettings)中更改, 这项设置的具体位置是 Appearance > Application Style > Widget Style.
  • 在 Cinnamon, GNOME, Xfce 等桌面环境中, 呈现GTK风格 (QGtkStyle).
  • 在其他桌面环境中, 呈现 Windows 风格.

要修改 Qt4 程序的外观,可以使用 提供的 Qt 配置工具qtconfig-qt4。这个程序可以配置 Qt4 程序的样式、颜色、字体等。

将所有的配置信息保存在 (系统级别) 或者 (只适用于特定用户). 该文件内容很多,因为它还包含许多与外观无关的信息,但是要更改它,您只需添加到文件末尾并覆盖任何以前的值(注意要把修改添加在[Qt]标题下)。

例如要将主题更改为 QtCurve,请添加:

~/.config/Trolltech.conf
...
[Qt]
style=QtCurve

Qt4 已经包含数种样式,例如 GTK 样式、Windows 样式、CDE 样式等,其它的主题(大多数为 KDE Plasma 桌面编写)可以从官方源或者 AUR 中安装:

Qt 样式表

An interesting way of customizing the look and feel of a Qt application is using Style Sheets, which are just simple CSS files. Using Style Sheets, one can modify the appearance of every widget in the application.

To run an application with a different style just execute:

$ qt_application -stylesheet style.qss

For more information on Qt Style Sheets see the official documentation or other tutorials. As an example Style Sheet see this Dolphin modification.

GTK 和 Qt

如果你有 GTK 和 Qt 应用程序,它们的外观可能无法融合到一起。如果你希望使 GTK 风格与 Qt 风格匹配,请阅读 统一 GTK 和 Qt 应用程序外观.

在KDE以外的环境下配置Qt编写的应用程序

Unlike Qt4, Qt5 does not ship a qtconfig utility to configure fonts, icons or styles. Instead, it will try to use the settings from the running DE. In KDE or GNOME this works well, but in other less popular DEs or WM it can lead to missing icons in Qt5 applications. One way to solve this is to fake the running desktop environment by setting

or , and then using the corresponding configuration application to set the desired icon set.

Another solution is provided by the package, which provides a DE independent Qt5 QPA and a configuration utility. After installing the package, run to set an icon theme, and set the environment variable QT_QPA_PLATFORMTHEME="qt5ct" so that the settings are picked up by Qt applications. Alternatively, use as argument to the Qt5 application.

如果遇到了下列错误,并且一些图标依然不在一些应用程序中出现,安装和oxygen-icons

Icon theme "oxygen" not found.
Icon theme "oxygen" not found.
Error: standard icon theme "oxygen" not found!

开发

支持的平台

Qt supports most platforms that are available today, even some of the more obscure ones, with more ports appearing every once in a while. For a more complete list see the Qt Wikipedia article.

工具

以下是官方 Qt 工具:

  • qmake A tool that helps simplify the build process for development project across different platforms, similar to cmake, but with fewer options and tailored for Qt applications.
https://doc.qt.io/qt-5/qmake-manual.html || Qt 5: qt5-base, Qt 4: qt4AUR

绑定

Qt 提供了所有流行编程语言的绑定。以下示例会在一个窗口中显示一句 'Hello world!' 消息。

C++

  • Package:
    • - Version 4.x of the Qt toolkit.
    • - Version 5.x of the Qt toolkit.
  • Website: http://qt-project.org/
  • Build:
    • The Qt4 version:
    • The Qt5 version:
  • Run with:

QML

注意: For version 5.x of the Qt toolkit, we need to import QtQuick 2.y.

Python (PyQt)

  • Package:
    • - Python 3.x bindings for Qt 4
    • - Python 2.x bindings for Qt 4
    • - Python 3.x bindings for Qt 5
    • - Python 2.x bindings for Qt 5

The Qt 5.x version is slighly different:

Python (PySide)

C#

参考: QtSharp.

Ruby

Perl

Lua

问题解决

Disable/Change Qt journal logging behaviour

When using KDE and/or any other Qt desktop environment debug info may be frequently be logged in the systemd journal.

Set as environment variable to change this behaviour, e.g. to completely disable logging:

/etc/environment
QT_LOGGING_RULES='*=false'

To allow only debug logging, use .

Icon theme is not applied

Since Qt 5.1 SVG support has moved into a module. Since does not depend on it may happen that the is installed but not . This results in deceptive icon theme behaviour. Since SVG is not supported the icons are silently skipped and the icon theme may seem to be unused. Installing explicitly solves the problem.

Theme not applied to root applications

As the user theme file ($XDG_CONFIG_HOME/Trolltech.conf), are not read by other accounts, the selected theme will not apply to X applications run as root. Possible solutions include:

  • Create symlinks, e.g
# ln -s /home/[username]/.config/Trolltech.conf /etc/xdg/Trolltech.conf
  • Configure system-wide theme file:
  • Adjust the theme as root

Qt4 style not respected

If pure Qt4 (non-KDE) applications do not stick with your selected Qt4 style, then you will probably need to tell Qt4 how to find KDE's styles (Oxygen, Phase etc.). You just need to set the environment variable QT_PLUGIN_PATH. E.g. put

export QT_PLUGIN_PATH=$HOME/.kde4/lib/kde4/plugins/:/usr/lib/kde4/plugins/

into your (or if you do not have root access). should then be able to find your kde styles and everything should look nice again!

Alternatively, you can symlink the Qt4 styles directory to the KDE4 styles one:

# ln -s /usr/lib/{kde,qt}4/plugins/styles/[theme name]

参阅

This article is issued from Archlinux. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.