Difference between revisions of "Qt (简体中文)"

From ArchWiki
Jump to: navigation, search
(Update translation.)
(Update translation status.)
 
(22 intermediate revisions by 6 users not shown)
Line 1: Line 1:
[[Category:Widget Toolkits (简体中文)]]
+
[[Category:Widget toolkits (简体中文)]]
 
[[en:Qt]]
 
[[en:Qt]]
[[es:Qt]]
+
[[ja:Qt]]
{{Article summary start|摘要}}
+
[[ru:Qt]]
{{Article summary text|Qt 是一个跨平台应用和用户界面框架,面向使用 C++ 或者类似于的 CSS 和 JavaScript 的 QML 语言的开发者。本文涵盖了 Qt 的安装和使用,以及配置主题、字体和其它选项的工具。}}
+
{{Related articles start (简体中文)}}
{{Article summary heading|相关}}
+
{{Related|KDE (简体中文)}}
{{Article summary wiki|KDE (简体中文)}}
+
{{Related|Uniform Look for QT and GTK Applications (简体中文)}}
{{Article summary wiki|Uniform Look for QT and GTK Applications (简体中文)}}
+
{{Related|GTK+ (简体中文)}}
{{Article summary wiki|GTK+ (简体中文)}}
+
{{Related articles end}}
{{Article summary end}}
+
{{TranslationStatus (简体中文)|Qt|2016-11-15|456523}}
{{Translateme (简体中文)}}
+
{{TranslationStatus (简体中文)|Qt|2013-10-6|277311}}
+
  
[http://qt-project.org/ Qt] 是一个跨平台的应用程序,组件工具使用标准 C++,但也大量使用了特殊的代码生成器(称为 [http://qt-project.org/doc/qt-4.8/moc.html Meta Object Compiler],或者 moc)以及数个宏来充实语言。它有一些更重要的特性:
+
[http://qt-project.org/ Qt] 是一个跨平台的应用程序和组件工具,使用标准 C++编写,通过大量使用代码生成器 [http://qt-project.org/doc/qt-4.8/moc.html Meta Object Compiler(moc)]以及数个宏来扩展语言的功能。它有一些更重要的特性包括:
* 运行于主流桌面平台和部分手机平台。
+
* 支持各种主流桌面平台和部分手机平台。
* 广泛的国际化支持。
+
* 完善的国际化支持。
* 提供 SQL 数据访问、XML 解析、线程管理、网络支持和统一的文件处理跨平台应用编程接口(API)的完整类库。
+
* 提供 SQL 数据访问、XML 解析、线程管理、网络支持和统一的文件处理跨平台应用编程接口。
  
Qt 框架正在成为主要的开发平台,同时是 [[KDE]] 软件社区和重要的开源和闭源应用,如 [[VLC]]、[[VirtualBox]]、[[Opera]]、[[Mathematica]][[Skype]]、[[Maya]]以及许多其它应用的基石。
+
Qt 框架正在成为主要的开发平台,是 [[KDE]] 软件社区和其它一些重要开源和闭源应用的基石,例如 [[VLC]]、[[VirtualBox]]、[[Opera]]、[[Mathematica]][[Skype]] 等等。
  
 
== 安装 ==
 
== 安装 ==
Line 23: Line 21:
 
现在[[official repositories|官方源]]中有三个版本的 Qt,能用以下软件包来[[Pacman|安装]]:
 
现在[[official repositories|官方源]]中有三个版本的 Qt,能用以下软件包来[[Pacman|安装]]:
  
* '''Qt 5.x''' 在软件包 {{Pkg|qt5-base}} 内,文档在软件包 {{Pkg|qt5-doc}} 内。
+
* '''Qt 5.x''':软件包 {{Pkg|qt5-base}},文档包是 {{Pkg|qt5-doc}}
* '''Qt 4.x''' 在软件包 {{Pkg|qt4}} 内。
+
* '''Qt 4.x''':软件包 {{Pkg|qt4}},文档包是 {{AUR|qt4-doc}}。
* '''Qt 3.x''' 在软件包 {{Pkg|qt3}} 内,文档在软件包 {{Pkg|qt3-doc}} 内。
+
* '''Qt 3.x''':在软件包 {{AUR|qt3}},文档包是 {{AUR|qt3-doc}}
 
+
{{警告|Qt软件包不再提供通常的二进制文件{{ic|/usr/bin/qmake}},而是将它们改名为 qmake-qt5, qmake-qt4, qmake-qt3 软链接,这可能导致 Qt3/4 程序编译失败。}}
+
  
 
== 默认 Qt 库 ==
 
== 默认 Qt 库 ==
安装{{Pkg|qtchooser}}可以恢复 {{ic|/usr/bin/qmake}} 并设置要使用的 Qt 版本,默认是使用 Qt5。
+
 
 +
Qt软件包不再提供通常的二进制文件{{ic|/usr/bin/qmake}},而是将它们改名为 qmake-qt5, qmake-qt4, qmake-qt3 软链接,这可能导致 Qt3/4 程序编译失败。安装{{AUR|qtchooser}}可以恢复 {{ic|/usr/bin/qmake}} 并设置要使用的 Qt 版本,默认是使用 Qt5。
 +
 
 +
{{警告|现在 {{AUR|qtchooser}} 与 {{Pkg|qt5-base}} 相冲突,可以自行编译并安装到{{ic|/usr/local}}。Arch 官方不支持这种方式[https://bugs.archlinux.org/task/51308]。}}
  
 
=== 修改环境变量 ===
 
=== 修改环境变量 ===
要使用 Qt4,可以在{{ic|~/.{bash,zsh}_profile.}}中设置{{ic|QT_SELECT=4}}。  
+
可以通过 {{ic|QT_SELECT}} [[environment variable|环境变量]] 设置默认的 QT. 例如要使用 Qt4,可以在{{ic|~/.bash_profile}} 或 {{ic|~/.zsh_profile}} {{ic|1=export QT_SELECT=4}}。  
 
 
 
=== 使用配置文件 ===
 
=== 使用配置文件 ===
要使用 Qt4,将 {{ic|/etc/xdg/qtchooser/4.conf}} 软链接到 {{ic|~/.config/qtchooser/default.conf}}。
+
创建 {{ic|~/.config/qtchooser/default.conf}} 软链接,链接到{{ic|/etc/xdg/qtchooser/}}目录中需要的 ''.conf'' 文件上。例如要使用 Qt4,将 {{ic|/etc/xdg/qtchooser/4.conf}} 软链接到 {{ic|~/.config/qtchooser/default.conf}}。
 +
 
 +
$ ln -s {{ic|/etc/xdg/qtchooser/4.conf}} {{ic|~/.config/qtchooser/default.conf}}
  
 
== 外观 ==
 
== 外观 ==
  
=== 配置 ===
+
=== Qt4 ===
  
Qt 应用程序会尝试模仿所运行的桌面环境的行为,除非碰到了某些问题或者硬编码的配置。要修改 Qt 程序的外观,可以使用 Qt 配置工具({{ic|qtconfig-qt4}} 或 {{ic|qt3config}}).  
+
Qt4 应用程序会尝试模仿所运行的桌面环境的行为,除非碰到了某些问题或者进行了强制配置。要修改 Qt 程序的外观,可以使用 Qt 配置工具({{ic|qtconfig-qt4}} 或 {{ic|qt3config}}).  
  
 
尽管不是 Qt 的一部分,''KDE 系统设置'' 提供了许多定制设置,Qt 程序也会使用这些设置。
 
尽管不是 Qt 的一部分,''KDE 系统设置'' 提供了许多定制设置,Qt 程序也会使用这些设置。
  
==== 主题 ====
+
* In KDE Plasma, it uses the actually selected Qt style. It can be configured using ''KDE System Settings'' (''systemsettings5''), the settings can be found in ''Appearance > Application Style > Widget Style''.
 
+
* In Cinnamon, GNOME, Xfce, it uses GTK+ ([[Uniform look for Qt and GTK applications#QGtkStyle|QGtkStyle]]).
Qt 已经包含数种样式,例如 GTK+ 样式、Windows 样式、CDE 样式等,但其它的主题(大多数为 KDE 桌面编写)可以从官方源或者 [[AUR]] 中安装:
+
* In other desktop environments, it uses Windows.
 
+
* {{App|[[Wikipedia:Oxygen Project|Oxygen]]|A desktop theme that comes with the KDE desktop.|http://www.oxygen-icons.org/|{{Pkg|kdebase-runtime}}}}
+
* {{App|[[Wikipedia:QtCurve|QtCurve]]|A very configurable and popular desktop theme with support for GTK+ and Qt applications.|http://kde-look.org/content/show.php?content=40492|{{Pkg|qtcurve-kde3}} {{Pkg|qtcurve-kde4}}}}
+
* {{App|Skulpture|A GUI style addon for KDE and Qt programs that features a classical three dimensional artwork with shadows and smooth gradients to enhance the visual experience.|http://kde-look.org/content/show.php/?content=59031|{{AUR|skulpture}}}}
+
* {{App|Polymer|A port of the KDE Plastik Style to Qt3.|http://kde-look.org/content/show.php?content=21748|{{AUR|polymer}}}}
+
* {{App|Bespin|A very configurable KDE theme.|http://cloudcity.sourceforge.net/frame.php|{{AUR|bespin-svn}}}}
+
 
+
==== 字体 ====
+
 
+
Qt fonts can be configured from ''QtConfig'' under ''Fonts > Default Font''.
+
 
+
==== 图标 ====
+
  
There is no way of setting the icon theme from ''QtConfig'', but since Qt follows the [http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html Freedesktop.org Icon Specification], any theme set for X is picked up by Qt.
+
要修改 Qt4 程序的外观,可以使用 {{Pkg|qt4}} 提供的 Qt 配置工具''qtconfig-qt4''。这个程序可以配置 Qt4 程序的样式、颜色、字体等。
  
=== 手动配置 ===
+
{{Note|如果使用 ''GTK+'' 样式,将忽略颜色和字体设置,直接使用 GTK+2 的值。}}
  
Qt keeps all its configuration information in {{ic|~/.config/Trolltech.conf}}. The file is rather difficult to navigate because it contains a lot of information not related to appearance, but for any changes you can just add to the end of the file and overwrite any previous values (make sure to add your modification under the {{ic|[Qt]}} header).  
+
Qt keeps all its configuration information in {{ic|/etc/xdg/Trolltech.conf}} (system-wide) or {{ic|~/.config/Trolltech.conf}} (user-specific). The file is rather difficult to navigate because it contains a lot of information not related to appearance, but for any changes you can just add to the end of the file and overwrite any previous values (make sure to add your modification under the [Qt] header).
  
 
For example, to change the theme to QtCurve, add:
 
For example, to change the theme to QtCurve, add:
Line 75: Line 64:
 
</nowiki>}}
 
</nowiki>}}
  
=== Qt 样式表 ===
+
Qt4 已经包含数种样式,例如 GTK+ 样式、Windows 样式、CDE 样式等,其它的主题(大多数为 KDE Plasma 桌面编写)可以从官方源或者 [[AUR]] 中安装:
  
 +
* {{App|Breeze|Artwork, styles and assets for the Breeze visual style for the Plasma Desktop.|https://projects.kde.org/projects/kde/workspace/breeze|{{Pkg|breeze-kde4}}}}
 +
* {{App|[[Wikipedia:Oxygen Project|Oxygen]]|KDE Oxygen style.|https://projects.kde.org/projects/kde/workspace/oxygen|{{Pkg|oxygen-kde4}}}}
 +
* {{App|QtCurve|A configurable set of widget styles for KDE and Gtk.|https://projects.kde.org/projects/playground/base/qtcurve|{{Pkg|qtcurve-qt4}}}}
 +
* {{App|Adwaita-Qt| A style to bend Qt applications to look like they belong into GNOME Shell.|https://github.com/MartinBriza/adwaita-qt|{{AUR|adwaita-qt4}}}}
 +
 +
=== Qt5 ===
 +
Qt5 decides the style to use based on what desktop environment is used:
 +
* In KDE Plasma, it uses the actually selected Qt style. It can be configured using ''KDE System Settings'' (''systemsettings5''), the settings can be found in ''Appearance > Application Style > Widget Style''.
 +
* In Cinnamon, GNOME, MATE, LXDE, Xfce, it uses GTK+ ([[Uniform look for Qt and GTK applications#QGtkStyle|QGtkStyle]]).
 +
* In other desktop environments, it uses Fusion.
 +
 +
To force a specific style, you can set the {{ic|1=QT_STYLE_OVERRIDE}} [[environment variable]]. Specifically, set it to {{ic|gtk2}} if you want to use the [[GTK+]] theme (Note: you will need to install the Qt style plugins mention below to get the GTK+ style). Qt5 applications also support the {{ic|-style}} flag, which you can use to launch a Qt5 application with a specific style.
 +
 +
The following styles are included in Qt5: ''Fusion'', ''Windows''. Others can be installed from the official repositories:
 +
* {{App|Breeze|Artwork, styles and assets for the Breeze visual style for the Plasma Desktop.|https://projects.kde.org/projects/kde/workspace/breeze|{{Pkg|breeze}}}}
 +
* {{App|Oxygen|KDE Oxygen style.|https://projects.kde.org/projects/kde/workspace/oxygen|{{Pkg|oxygen}}}}
 +
* {{App|QtCurve|A configurable set of widget styles for KDE and Gtk.|https://projects.kde.org/projects/playground/base/qtcurve|{{Pkg|qtcurve-qt5}}}}
 +
* {{App|Adwaita-Qt|A style to bend Qt applications to look like they belong into GNOME Shell.|https://github.com/MartinBriza/adwaita-qt|{{AUR|adwaita-qt5}}}}
 +
* {{App|Qt style plugins|Additional style plugins for Qt5, including ''GTK+'', ''Cleanlooks'', ''Motif'', ''Plastique''.|http://code.qt.io/cgit/qt/qtstyleplugins.git|{{AUR|qt5-styleplugins}}}}
 +
 +
=== 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.
 
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:
 
To run an application with a different style just execute:
  $ qt_application --stylesheet style.qss
+
  $ qt_application -stylesheet ''style.qss''
  
For more information on Qt Style Sheets see the [http://qt-project.org/doc/qt-4.8/stylesheet-reference.html official documentation] or other [http://thesmithfam.org/blog/2009/09/10/qt-stylesheets-tutorial/ tutorials]. As an example Style Sheet see this [http://kde-apps.org/content/show.php/roxydoxy?content&#61;125979 Dolphin modification].
+
For more information on Qt Style Sheets see the [http://doc.qt.io/qt-5/stylesheet-reference.html official documentation] or other [http://thesmithfam.org/blog/2009/09/10/qt-stylesheets-tutorial/ tutorials]. As an example Style Sheet see this [http://kde-apps.org/content/show.php/roxydoxy?content&#61;125979 Dolphin modification].
  
 
=== GTK+ 和 Qt ===
 
=== GTK+ 和 Qt ===
  
如果你有 GTK+ 和 Qt 应用程序,它们的外观可能无法融合到一起。如果你希望使 GTK+ 风格与 Qt 风格匹配,请阅读 [[Uniform Look for QT and GTK Applications (简体中文)|统一 GTK+ 和 Qt 应用程序外观]].
+
如果你有 GTK+ 和 Qt 应用程序,它们的外观可能无法融合到一起。如果你希望使 GTK+ 风格与 Qt 风格匹配,请阅读 [[Uniform look for Qt and GTK applications (简体中文)|统一 GTK+ 和 Qt 应用程序外观]].
 +
 
 +
=== Configuration of Qt5 apps under environments other than KDE ===
 +
 
 +
Unlike Qt4, Qt5 doesn't 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
 +
{{ic|1=XDG_CURRENT_DESKTOP=KDE}} or {{ic|GNOME}}, and then using the corresponding configuration application to set the desired icon set.
 +
 
 +
Another solution is provided by the {{pkg|qt5ct}} package, which provides a DE independent Qt5 QPA and a configuration utility. After installing the package, run {{ic|qt5ct}} to set an icon theme, and set the environment variable {{ic|1=QT_QPA_PLATFORMTHEME="qt5ct"}} so that the settings are picked up by Qt applications. Alternatively, use {{ic|--platformtheme qt5ct}} as argument to the Qt5 application.
 +
 
 +
To automatically set {{ic|QT_QPA_PLATFORMTHEME}} for user session, add the following line to {{ic|~/.profile}}.
 +
 
 +
[ "$XDG_CURRENT_DESKTOP" = "KDE" ] || [ "$XDG_CURRENT_DESKTOP" = "GNOME" ] || export QT_QPA_PLATFORMTHEME="qt5ct"
 +
 
 +
This will export {{ic|QT_QPA_PLATFORMTHEME}} environment variable for the whole user session. Note that this doesn't work on [[enlightenment]] because it has its own custom environment variable setting instead of getting it from {{ic|~/.profile}} file.
 +
 
 +
If the below errors are received, and some icons still do not appear in some of the apps, install {{pkg|oxygen}} and {{pkg|oxygen-icons}}:
 +
 
 +
Icon theme "oxygen" not found.
 +
Icon theme "oxygen" not found.
 +
Error: standard icon theme "oxygen" not found!
  
 
== 开发 ==
 
== 开发 ==
Line 98: Line 127:
 
以下是官方 Qt 工具:
 
以下是官方 Qt 工具:
  
* {{App|[[Wikipedia: Qt Creator|Qt Creator]]|A cross-platform IDE tailored for Qt that supports all of its features.|http://qt-project.org/doc/qtcreator/|{{Pkg|qtcreator}}}}
+
* {{App|[[Wikipedia:Qt Creator|Qt Creator]]|A cross-platform IDE tailored for Qt that supports all of its features.|http://doc.qt.io/qtcreator/|{{Pkg|qtcreator}}}}
* {{App|Qt Linguist|A set of tools that speed the translation and internationalization of Qt applications.|http://qt-project.org/doc/qt-4.8/linguist-manual.html|{{Pkg|qt}}}}
+
* {{App|Qt Linguist|A set of tools that speed the translation and internationalization of Qt applications.|http://doc.qt.io/qt-5/qtlinguist-index.html|Qt 5: {{Pkg|qt5-tools}}, Qt 4: {{Pkg|qt4}}}}
* {{App|Qt Assistant|A configurable and redistributable documentation reader for Qt ''qch'' files.|http://qt-project.org/doc/qt-4.8/assistant-manual.html|{{Pkg|qt}}}}
+
* {{App|Qt Assistant|A configurable and redistributable documentation reader for Qt ''qch'' files.|http://doc.qt.io/qt-5/qtassistant-index.html|Qt 5: {{Pkg|qt5-tools}}, Qt 4: {{Pkg|qt4}}}}
* {{App|Qt Designer|A powerful cross-platform GUI layout and forms builder for Qt widgets.|http://qt-project.org/doc/qt-4.8/designer-manual.html|{{Pkg|qt}}}}
+
* {{App|Qt Designer|A powerful cross-platform GUI layout and forms builder for Qt widgets.|http://doc.qt.io/qt-5/qtdesigner-manual.html|Qt 5: {{Pkg|qt5-tools}}, Qt 4: {{Pkg|qt4}}}}
* {{App|Qt Quick Designer|A visual editor for QML files which supports WYSIWYG. It allows you to rapidly design and build Qt Quick applications and components from scratch.|http://qt-project.org/doc/qtcreator-2.8/creator-using-qt-quick-designer.html|{{Pkg|qtcreator}}}}
+
* {{App|Qt Quick Designer|A visual editor for QML files which supports WYSIWYG. It allows you to rapidly design and build Qt Quick applications and components from scratch.|http://doc.qt.io/qtcreator/creator-using-qt-quick-designer.html|{{Pkg|qtcreator}}}}
* {{App|QML Viewer|A tool for loading QML documents that makes it easy to quickly develop and debug QML applications.|http://qt-project.org/doc/qt-4.8/qmlviewer.html|{{Pkg|qt}}}}
+
* {{App|qmlscene|A tool for loading QML documents that makes it easy to quickly develop and debug QML applications.|http://doc.qt.io/qt-5/qtquick-qmlscene.html|Qt 5: {{Pkg|qt5-declarative}}, Qt 4 QML Viewer: {{Pkg|qt4}}}}
* {{App|[[Wikipedia:Qmake|qmake]]|A tool that helps simplify the build process for development project across different platforms, similar to [[Wikipedia:CMake|cmake]], but with fewer options and tailored for Qt applications.|https://qt-project.org/doc/qt-4.8/qmake-manual.html|{{Pkg|qt}}}}
+
* {{App|[[Wikipedia:Qmake|qmake]]|A tool that helps simplify the build process for development project across different platforms, similar to [[Wikipedia:CMake|cmake]], but with fewer options and tailored for Qt applications.|http://doc.qt.io/qt-5/qmake-manual.html|Qt 5: {{Pkg|qt5-base}}, Qt 4: {{Pkg|qt4}}}}
* {{App|uic|A tool that reads ''*.ui'' XML files and generates the corresponding C++ files.|http://qt-project.org/doc/qt-4.8/uic.html|{{Pkg|qt}}}}
+
* {{App|uic|A tool that reads ''*.ui'' XML files and generates the corresponding C++ files.|http://doc.qt.io/qt-5/uic.html|Qt 5: {{Pkg|qt5-base}}, Qt 4: {{Pkg|qt4}}}}
* {{App|rcc|A tool that is used to embed resources (such as pictures) into a Qt application during the build process. It works by generating a C++ source file containing data specified in a Qt resource (.qrc) file.|http://qt-project.org/doc/qt-4.8/rcc.html|{{Pkg|qt}}}}
+
* {{App|rcc|A tool that is used to embed resources (such as pictures) into a Qt application during the build process. It works by generating a C++ source file containing data specified in a Qt resource (.qrc) file.|http://doc.qt.io/qt-5/rcc.html|Qt 5: {{Pkg|qt5-base}}, Qt 4: {{Pkg|qt4}}}}
* {{App|moc|A tool that handles Qt's C++ extensions (the signals and slots mechanism, the run-time type information, and the dynamic property system, etc.).|http://doc.qt.digia.com/4.7-snapshot/moc.html|{{Pkg|qt}}}}
+
* {{App|moc|A tool that handles Qt's C++ extensions (the signals and slots mechanism, the run-time type information, and the dynamic property system, etc.).|http://doc.qt.io/qt-5/moc.html|Qt 5: {{Pkg|qt5-base}}, Qt 4: {{Pkg|qt4}}}}
  
 
=== 绑定 ===
 
=== 绑定 ===
Qt has bindings for all of the more popular languages, for a full list see [[Wikipedia:Qt (framework)#Programming language bindings|this list]].
+
Qt 提供了所有流行编程语言的[[Wikipedia:Qt (framework)#Programming language bindings|绑定]]。以下示例会在一个窗口中显示一句 'Hello world!' 消息。
 
+
以下示例会在一个窗口中显示一句 'Hello world!' 消息。
+
  
 
==== C++ ====
 
==== C++ ====
 
+
* Package:  
* Package: {{Pkg|qt4}}
+
** {{Pkg|qt4}} - Version 4.x of the Qt toolkit.
 +
** {{Pkg|qt5-base}} - Version 5.x of the Qt toolkit.
 
* Website: http://qt-project.org/
 
* Website: http://qt-project.org/
* Build with: {{ic|g++ `pkg-config --cflags --libs QtCore QtGui` -o hello hello.cpp}}
+
* Build:
 +
** The Qt4 version: {{ic|g++ $(pkg-config --cflags --libs QtGui) -o hello hello.cpp}}
 +
** The Qt5 version: {{ic|g++ $(pkg-config --cflags --libs Qt5Widgets) -fPIC -o hello hello.cpp}}
 
* Run with: {{ic|./hello}}
 
* Run with: {{ic|./hello}}
 
{{hc|hello.cpp|
 
{{hc|hello.cpp|
Line 128: Line 158:
 
     QApplication app(argc, argv);
 
     QApplication app(argc, argv);
 
     QLabel hello("Hello world!");
 
     QLabel hello("Hello world!");
   
+
 
 
     hello.show();
 
     hello.show();
 
     return app.exec();
 
     return app.exec();
Line 135: Line 165:
  
 
==== QML ====
 
==== QML ====
 
+
* Package: {{Pkg|qt4}} or {{Pkg|qt5-declarative}}.
* Package: {{Pkg|qt4}}
+
 
* Website: http://qt-project.org/
 
* Website: http://qt-project.org/
* Run with: {{ic|qmlviewer hello.qml}}
+
* Run with: {{ic|qmlviewer-qt4 hello.qml}} or {{ic|qmlscene-qt5 hello.qml}}
 
{{hc|hello.qml|
 
{{hc|hello.qml|
 
import QtQuick 1.0
 
import QtQuick 1.0
Line 156: Line 185:
 
}
 
}
 
}}
 
}}
 +
{{Note|For version 5.x of the Qt toolkit, we need to import QtQuick 2.y.}}
  
==== Python ====
+
==== Python (PyQt) ====
 +
* Package:
 +
** {{Pkg|python-pyqt4}} - Python 3.x bindings for Qt 4
 +
** {{Pkg|python2-pyqt4}} - Python 2.x bindings for Qt 4
 +
** {{Pkg|python-pyqt5}} - Python 3.x bindings for Qt 5
 +
** {{Pkg|python2-pyqt5}} - Python 2.x bindings for Qt 5
  
* Package:
 
** {{Pkg|python-pyqt4}} - Python 3.x bindings
 
** {{Pkg|python2-pyqt4}} - Python 2.x bindings
 
 
* Website: http://www.riverbankcomputing.co.uk/software/pyqt/intro
 
* Website: http://www.riverbankcomputing.co.uk/software/pyqt/intro
* Run with: {{ic|python hello-pyqt.py}} or {{ic|python2 hello-pyqt.py}}
+
* Run with: {{ic|python hello-pyqt.py}} or {{ic|python2 hello-pyqt.py}}.
 
{{hc|hello-pyqt.py|<nowiki>
 
{{hc|hello-pyqt.py|<nowiki>
 
import sys
 
import sys
Line 175: Line 207:
 
}}
 
}}
  
 +
The Qt 5.x version is slighly different:
 +
{{hc|hello-pyqt.py|<nowiki>
 +
import sys
 +
from PyQt5 import QtWidgets
 +
 +
app = QtWidgets.QApplication(sys.argv)
 +
label = QtWidgets.QLabel("Hello world!")
 +
 +
label.show()
 +
sys.exit(app.exec_())</nowiki>
 +
}}
 +
 +
==== Python (PySide) ====
 
* Package:
 
* Package:
** {{AUR|python-pyside}} - Python 3.x bindings
+
** {{Pkg|python-pyside}} - Python 3.x bindings
** {{AUR|python2-pyside}} - Python 2.x bindings
+
** {{Pkg|python2-pyside}} - Python 2.x bindings
 
* Website: http://www.pyside.org/
 
* Website: http://www.pyside.org/
 
* Run with: {{ic|python hello-pyside.py}} or {{ic|python2 hello-pyside.py}}
 
* Run with: {{ic|python hello-pyside.py}} or {{ic|python2 hello-pyside.py}}
Line 184: Line 229:
 
from PySide.QtCore import *
 
from PySide.QtCore import *
 
from PySide.QtGui import *
 
from PySide.QtGui import *
+
 
 
app = QApplication(sys.argv)
 
app = QApplication(sys.argv)
 
label = QLabel("Hello world!")
 
label = QLabel("Hello world!")
Line 193: Line 238:
  
 
==== C# ====
 
==== C# ====
 
+
* Package: {{AUR|kdebindings-qyoto}}{{Broken package link|{{aur-mirror|kdebindings-qyoto}}}}
* Package: {{Pkg|kdebindings-qyoto}}
+
 
* Website: http://techbase.kde.org/Development/Languages/Qyoto
 
* Website: http://techbase.kde.org/Development/Languages/Qyoto
 
* Build with: {{ic|mcs -pkg:qyoto hello.cs}}
 
* Build with: {{ic|mcs -pkg:qyoto hello.cs}}
Line 213: Line 257:
  
 
==== Ruby ====
 
==== Ruby ====
 
+
* Package: {{AUR|kdebindings-qtruby}}{{Broken package link|{{aur-mirror|kdebindings-qtruby}}}}
* Package: {{Pkg|kdebindings-qtruby}}
+
 
* Website: http://rubyforge.org/projects/korundum/
 
* Website: http://rubyforge.org/projects/korundum/
 
* Run with: {{ic|ruby hello.rb}}
 
* Run with: {{ic|ruby hello.rb}}
 
{{hc|hello.rb|<nowiki>
 
{{hc|hello.rb|<nowiki>
 
require 'Qt4'
 
require 'Qt4'
+
 
 
app = Qt::Application.new(ARGV)
 
app = Qt::Application.new(ARGV)
 
hello = Qt::Label.new('Hello World!')
 
hello = Qt::Label.new('Hello World!')
  
hello.show  
+
hello.show
 
app.exec</nowiki>
 
app.exec</nowiki>
 
}}
 
}}
  
 
==== Java ====
 
==== Java ====
 
+
* Package: {{AUR|qtjambi}}{{Broken package link|{{aur-mirror|qtjambi}}}} or {{AUR|qtjambi-beta}}{{Broken package link|{{aur-mirror|qtjambi-beta}}}}.
* Package: {{AUR|qtjambi}}
+
 
* Website: http://qt-jambi.org/
 
* Website: http://qt-jambi.org/
 +
* Build with: {{ic|javac Hello.java -cp /opt/qtjambi-beta/qtjambi-linux64-community-4.7.0/qtjambi-4.7.0.jar}}.
 +
* Run with: {{ic|java -cp /opt/qtjambi-beta/qtjambi-linux64-community-4.7.0/qtjambi-4.7.0.jar:. Hello}}.
 
{{hc|Hello.java|<nowiki>
 
{{hc|Hello.java|<nowiki>
 
import com.trolltech.qt.gui.*;
 
import com.trolltech.qt.gui.*;
Line 246: Line 290:
 
}</nowiki>
 
}</nowiki>
 
}}
 
}}
 +
{{Note|The build instructions and example have been tested on the beta version of Qt Jambi 4.7.0.}}
  
 
==== Perl ====
 
==== Perl ====
 
+
* Package: {{AUR|kdebindings-perlqt}}{{Broken package link|{{aur-mirror|kdebindings-perlqt}}}}
* Package: {{Pkg|kdebindings-perlqt}}
+
 
* Website: http://code.google.com/p/perlqt4/
 
* Website: http://code.google.com/p/perlqt4/
 
* Run with: {{ic|perl hello.pl}}
 
* Run with: {{ic|perl hello.pl}}
Line 264: Line 308:
  
 
==== Lua ====
 
==== Lua ====
 
+
* Package: {{AUR|libqtlua}}{{Broken package link|{{aur-mirror|libqtlua}}}}
* Package: {{AUR|libqtlua}}
+
 
* Website: http://www.nongnu.org/libqtlua/
 
* Website: http://www.nongnu.org/libqtlua/
 
* Run with: {{ic|qtlua hello.lua}}
 
* Run with: {{ic|qtlua hello.lua}}
Line 277: Line 320:
 
{{Note|QtLua is not designed to develop an application in pure Lua but rather to extend a Qt C++ application using Lua as scripting language.}}
 
{{Note|QtLua is not designed to develop an application in pure Lua but rather to extend a Qt C++ application using Lua as scripting language.}}
  
== 资源 ==
+
== 问题解决 ==
 +
 
 +
=== Icon theme is not applied ===
 +
 
 +
Since Qt 5.1 SVG support has moved into a module. Since {{Pkg|qt5-base}} does not depend on {{Pkg|qt5-svg}} it may happen that the {{Pkg|qt5-base}} is installed but not {{Pkg|qt5-svg}}. 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 {{Pkg|qt5-svg}} explicitly solves the problem.
 +
 
 +
=== Theme not applied to root applications ===
 +
 
 +
As the user theme file ({{ic|$XDG_CONFIG_HOME/Trolltech.conf}}), are not read by other accounts, the selected theme will not apply to [[Running_X_apps_as_root|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: {{ic|/etc/xdg/Trolltech.conf}}
 +
* 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'll 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 {{ic|/etc/profile}} (or {{ic|~/.profile}} if you do not have root access). {{ic|qtconfig-qt4}} 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]
 +
 
 +
== 参阅 ==
  
 
* [http://qt.digia.com/ 官方网站]
 
* [http://qt.digia.com/ 官方网站]

Latest revision as of 02:38, 15 November 2016

翻译状态: 本文是英文页面 Qt翻译,最后翻译时间:2016-11-15,点击这里可以查看翻译后英文页面的改动。

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

  • 支持各种主流桌面平台和部分手机平台。
  • 完善的国际化支持。
  • 提供 SQL 数据访问、XML 解析、线程管理、网络支持和统一的文件处理跨平台应用编程接口。

Qt 框架正在成为主要的开发平台,是 KDE 软件社区和其它一些重要开源和闭源应用的基石,例如 VLCVirtualBoxOperaMathematicaSkype 等等。

安装

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

默认 Qt 库

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

警告: 现在 qtchooserAURqt5-base 相冲突,可以自行编译并安装到/usr/local。Arch 官方不支持这种方式[1]

修改环境变量

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

使用配置文件

创建 ~/.config/qtchooser/default.conf 软链接,链接到/etc/xdg/qtchooser/目录中需要的 .conf 文件上。例如要使用 Qt4,将 /etc/xdg/qtchooser/4.conf 软链接到 ~/.config/qtchooser/default.conf

$ ln -s /etc/xdg/qtchooser/4.conf ~/.config/qtchooser/default.conf

外观

Qt4

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

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

  • In KDE Plasma, it uses the actually selected Qt style. It can be configured using KDE System Settings (systemsettings5), the settings can be found in Appearance > Application Style > Widget Style.
  • In Cinnamon, GNOME, Xfce, it uses GTK+ (QGtkStyle).
  • In other desktop environments, it uses Windows.

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

Note: 如果使用 GTK+ 样式,将忽略颜色和字体设置,直接使用 GTK+2 的值。

Qt keeps all its configuration information in /etc/xdg/Trolltech.conf (system-wide) or ~/.config/Trolltech.conf (user-specific). The file is rather difficult to navigate because it contains a lot of information not related to appearance, but for any changes you can just add to the end of the file and overwrite any previous values (make sure to add your modification under the [Qt] header).

For example, to change the theme to QtCurve, add:

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

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

  • Breeze — Artwork, styles and assets for the Breeze visual style for the Plasma Desktop.
https://projects.kde.org/projects/kde/workspace/breeze || breeze-kde4
https://projects.kde.org/projects/kde/workspace/oxygen || oxygen-kde4
  • QtCurve — A configurable set of widget styles for KDE and Gtk.
https://projects.kde.org/projects/playground/base/qtcurve || qtcurve-qt4
  • Adwaita-Qt — A style to bend Qt applications to look like they belong into GNOME Shell.
https://github.com/MartinBriza/adwaita-qt || adwaita-qt4AUR

Qt5

Qt5 decides the style to use based on what desktop environment is used:

  • In KDE Plasma, it uses the actually selected Qt style. It can be configured using KDE System Settings (systemsettings5), the settings can be found in Appearance > Application Style > Widget Style.
  • In Cinnamon, GNOME, MATE, LXDE, Xfce, it uses GTK+ (QGtkStyle).
  • In other desktop environments, it uses Fusion.

To force a specific style, you can set the QT_STYLE_OVERRIDE environment variable. Specifically, set it to gtk2 if you want to use the GTK+ theme (Note: you will need to install the Qt style plugins mention below to get the GTK+ style). Qt5 applications also support the -style flag, which you can use to launch a Qt5 application with a specific style.

The following styles are included in Qt5: Fusion, Windows. Others can be installed from the official repositories:

  • Breeze — Artwork, styles and assets for the Breeze visual style for the Plasma Desktop.
https://projects.kde.org/projects/kde/workspace/breeze || breeze
  • Oxygen — KDE Oxygen style.
https://projects.kde.org/projects/kde/workspace/oxygen || oxygen
  • QtCurve — A configurable set of widget styles for KDE and Gtk.
https://projects.kde.org/projects/playground/base/qtcurve || qtcurve-qt5
  • Adwaita-Qt — A style to bend Qt applications to look like they belong into GNOME Shell.
https://github.com/MartinBriza/adwaita-qt || adwaita-qt5AUR
  • Qt style plugins — Additional style plugins for Qt5, including GTK+, Cleanlooks, Motif, Plastique.
http://code.qt.io/cgit/qt/qtstyleplugins.git || qt5-stylepluginsAUR

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 应用程序外观.

Configuration of Qt5 apps under environments other than KDE

Unlike Qt4, Qt5 doesn't 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 XDG_CURRENT_DESKTOP=KDE or GNOME, and then using the corresponding configuration application to set the desired icon set.

Another solution is provided by the qt5ct package, which provides a DE independent Qt5 QPA and a configuration utility. After installing the package, run qt5ct 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 --platformtheme qt5ct as argument to the Qt5 application.

To automatically set QT_QPA_PLATFORMTHEME for user session, add the following line to ~/.profile.

[ "$XDG_CURRENT_DESKTOP" = "KDE" ] || [ "$XDG_CURRENT_DESKTOP" = "GNOME" ] || export QT_QPA_PLATFORMTHEME="qt5ct"

This will export QT_QPA_PLATFORMTHEME environment variable for the whole user session. Note that this doesn't work on enlightenment because it has its own custom environment variable setting instead of getting it from ~/.profile file.

If the below errors are received, and some icons still do not appear in some of the apps, install oxygen and 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 工具:

  • Qt Creator — A cross-platform IDE tailored for Qt that supports all of its features.
http://doc.qt.io/qtcreator/ || qtcreator
  • Qt Linguist — A set of tools that speed the translation and internationalization of Qt applications.
http://doc.qt.io/qt-5/qtlinguist-index.html || Qt 5: qt5-tools, Qt 4: qt4
  • Qt Assistant — A configurable and redistributable documentation reader for Qt qch files.
http://doc.qt.io/qt-5/qtassistant-index.html || Qt 5: qt5-tools, Qt 4: qt4
  • Qt Designer — A powerful cross-platform GUI layout and forms builder for Qt widgets.
http://doc.qt.io/qt-5/qtdesigner-manual.html || Qt 5: qt5-tools, Qt 4: qt4
  • Qt Quick Designer — A visual editor for QML files which supports WYSIWYG. It allows you to rapidly design and build Qt Quick applications and components from scratch.
http://doc.qt.io/qtcreator/creator-using-qt-quick-designer.html || qtcreator
  • qmlscene — A tool for loading QML documents that makes it easy to quickly develop and debug QML applications.
http://doc.qt.io/qt-5/qtquick-qmlscene.html || Qt 5: qt5-declarative, Qt 4 QML Viewer: qt4
  • 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.
http://doc.qt.io/qt-5/qmake-manual.html || Qt 5: qt5-base, Qt 4: qt4
  • uic — A tool that reads *.ui XML files and generates the corresponding C++ files.
http://doc.qt.io/qt-5/uic.html || Qt 5: qt5-base, Qt 4: qt4
  • rcc — A tool that is used to embed resources (such as pictures) into a Qt application during the build process. It works by generating a C++ source file containing data specified in a Qt resource (.qrc) file.
http://doc.qt.io/qt-5/rcc.html || Qt 5: qt5-base, Qt 4: qt4
  • moc — A tool that handles Qt's C++ extensions (the signals and slots mechanism, the run-time type information, and the dynamic property system, etc.).
http://doc.qt.io/qt-5/moc.html || Qt 5: qt5-base, Qt 4: qt4

绑定

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

C++

  • Package:
    • qt4 - Version 4.x of the Qt toolkit.
    • qt5-base - Version 5.x of the Qt toolkit.
  • Website: http://qt-project.org/
  • Build:
    • The Qt4 version: g++ $(pkg-config --cflags --libs QtGui) -o hello hello.cpp
    • The Qt5 version: g++ $(pkg-config --cflags --libs Qt5Widgets) -fPIC -o hello hello.cpp
  • Run with: ./hello
hello.cpp
#include <QApplication>
#include <QLabel>

int main(int argc, char **argv)
{
    QApplication app(argc, argv);
    QLabel hello("Hello world!");

    hello.show();
    return app.exec();
}

QML

hello.qml
import QtQuick 1.0

Rectangle {
    id: page
    width: 400; height: 100
    color: "lightgray"

    Text {
        id: helloText
        text: "Hello world!"
        anchors.horizontalCenter: page.horizontalCenter
        anchors.verticalCenter: page.verticalCenter
        font.pointSize: 24; font.bold: true
    }
}
Note: For version 5.x of the Qt toolkit, we need to import QtQuick 2.y.

Python (PyQt)

hello-pyqt.py
import sys
from PyQt4 import QtGui

app = QtGui.QApplication(sys.argv)
label = QtGui.QLabel("Hello world!")

label.show()
sys.exit(app.exec_())

The Qt 5.x version is slighly different:

hello-pyqt.py
import sys
from PyQt5 import QtWidgets

app = QtWidgets.QApplication(sys.argv)
label = QtWidgets.QLabel("Hello world!")

label.show()
sys.exit(app.exec_())

Python (PySide)

hello-pyside.py
import sys
from PySide.QtCore import *
from PySide.QtGui import *

app = QApplication(sys.argv)
label = QLabel("Hello world!")

label.show()
sys.exit(app.exec_())

C#

hello.cs
using System;
using Qyoto;

public class Hello {
    public static int Main(String[] args) {
        new QApplication(args);
        new QLabel("Hello world!").Show();

        return QApplication.Exec();
    }
}

Ruby

hello.rb
require 'Qt4'

app = Qt::Application.new(ARGV)
hello = Qt::Label.new('Hello World!')

hello.show
app.exec

Java

Hello.java
import com.trolltech.qt.gui.*;

public class Hello
{
    public static void main(String args[])
    {
        QApplication.initialize(args);
        QLabel hello = new QLabel("Hello World!");

        hello.show();
        QApplication.exec();
    }
}
Note: The build instructions and example have been tested on the beta version of Qt Jambi 4.7.0.

Perl

hello.pl
use QtGui4;

my $a = Qt::Application(\@ARGV);
my $hello = Qt::Label("Hello World!", undef);

$hello->show;
exit $a->exec;

Lua

hello.lua
label = qt.new_widget("QLabel")

label:setText("Hello World!")
label:show()
Note: QtLua is not designed to develop an application in pure Lua but rather to extend a Qt C++ application using Lua as scripting language.

问题解决

Icon theme is not applied

Since Qt 5.1 SVG support has moved into a module. Since qt5-base does not depend on qt5-svg it may happen that the qt5-base is installed but not qt5-svg. 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 qt5-svg 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: /etc/xdg/Trolltech.conf
  • 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'll 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 /etc/profile (or ~/.profile if you do not have root access). qtconfig-qt4 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]

参阅