# Qt (简体中文)

相关文章

- KDE (简体中文)
- Uniform Look for QT and GTK Applications (简体中文)
- GTK+ (简体中文)

本页面需要更新翻译，内容可能已经与英文脱节。要贡献翻译，请访问简体中文翻译组。

附注: please use the first argument of the template to provide more detailed indications.
翻译状态： 本文是英文页面 Qt 的翻译，最后翻译时间：2013-10-6，点击这里可以查看翻译后英文页面的改动。

Qt 是一个跨平台的应用程序，组件工具使用标准 C++，但也大量使用了特殊的代码生成器（称为 Meta Object Compiler，或者 moc）以及数个宏来充实语言。它有一些更重要的特性：

- 运行于主流桌面平台和部分手机平台。
- 广泛的国际化支持。
- 提供 SQL 数据访问、XML 解析、线程管理、网络支持和统一的文件处理跨平台应用编程接口（API）的完整类库。

Qt 框架正在成为主要的开发平台，同时是 KDE 软件社区和重要的开源和闭源应用，如 VLC、VirtualBox、Opera、Mathematica、Skype以及许多其它应用的基石。

## 安装

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

- Qt 5.x 在软件包 qt5-base 内，文档在软件包 qt5-doc 内。
- Qt 4.x 在软件包 qt4 内。
- Qt 3.x 在软件包 qt3AUR 内，文档在软件包 qt3-docAUR 内。

警告: Qt软件包不再提供通常的二进制文件`/usr/bin/qmake`,而是将它们改名为 qmake-qt5, qmake-qt4, qmake-qt3 软链接，这可能导致 Qt3/4 程序编译失败。

## 默认 Qt 库

安装qtchooserAUR可以恢复 `/usr/bin/qmake` 并设置要使用的 Qt 版本，默认是使用 Qt5。

### 修改环境变量

要使用 Qt4，可以在`~/.{bash,zsh}_profile.`中设置`QT_SELECT=4`。

### 使用配置文件

要使用 Qt4，将 `/etc/xdg/qtchooser/4.conf` 软链接到 `~/.config/qtchooser/default.conf`。

## 外观

### 配置

Qt 应用程序会尝试模仿所运行的桌面环境的行为，除非碰到了某些问题或者硬编码的配置。要修改 Qt 程序的外观，可以使用 Qt 配置工具(`qtconfig-qt4` 或 `qt3config`).

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

#### 主题

Qt 已经包含数种样式，例如 GTK+ 样式、Windows 样式、CDE 样式等，但其它的主题（大多数为 KDE 桌面编写）可以从官方源或者 AUR 中安装：

- Oxygen — A desktop theme that comes with the KDE desktop.

  http://www.oxygen-icons.org/ || kdebase-runtime

- QtCurve — A very configurable and popular desktop theme with support for GTK+ and Qt applications.

  http://kde-look.org/content/show.php?content=40492 || qtcurve-kde3AUR[broken link: archived in aur-mirror] qtcurve-kde4[broken link: package not found]

- 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 || skulptureAUR[broken link: archived in aur-mirror]

- Polymer — A port of the KDE Plastik Style to Qt3.

  http://kde-look.org/content/show.php?content=21748 || polymerAUR

- Bespin — A very configurable KDE theme.

  http://cloudcity.sourceforge.net/frame.php || bespin-svnAUR[broken link: archived in aur-mirror]

#### 字体

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 Freedesktop.org Icon Specification, any theme set for X is picked up by Qt.

### 手动配置

Qt keeps all its configuration information in `~/.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 `[Qt]` header).

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

    ~/.config/Trolltech.conf

    ...
    [Qt]
    style=QtCurve

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

## 开发

### 支持的平台

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://qt-project.org/doc/qtcreator/ || qtcreator

- 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 || qt

- Qt Assistant — A configurable and redistributable documentation reader for Qt qch files.

  http://qt-project.org/doc/qt-4.8/assistant-manual.html || qt

- 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 || qt

- 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 || qtcreator

- 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 || 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://qt-project.org/doc/qt-4.8/qmake-manual.html || qt

- uic — A tool that reads *.ui XML files and generates the corresponding C++ files.

  http://qt-project.org/doc/qt-4.8/uic.html || qt

- 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 || qt

- 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 || qt

### 绑定

Qt has bindings for all of the more popular languages, for a full list see this list.

以下示例会在一个窗口中显示一句 'Hello world!' 消息。

#### C++

- Package: qt4
- Website: http://qt-project.org/
- Build with: `g++ `pkg-config --cflags --libs QtCore QtGui` -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

- Package: qt4
- Website: http://qt-project.org/
- Run with: `qmlviewer hello.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
        }
    }

#### Python

- Package:
  - python-pyqt4 - Python 3.x bindings
  - python2-pyqt4 - Python 2.x bindings
- Website: http://www.riverbankcomputing.co.uk/software/pyqt/intro
- Run with: `python hello-pyqt.py` or `python2 hello-pyqt.py`

    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_())

- Package:
  - python-pyside - Python 3.x bindings
  - python2-pyside - Python 2.x bindings
- Website: http://www.pyside.org/
- Run with: `python hello-pyside.py` or `python2 hello-pyside.py`

    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#

- Package: kdebindings-qyotoAUR[broken link: archived in aur-mirror]
- Website: http://techbase.kde.org/Development/Languages/Qyoto
- Build with: `mcs -pkg:qyoto hello.cs`
- Run with: `mono hello.exe`

    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

- Package: kdebindings-qtrubyAUR[broken link: archived in aur-mirror]
- Website: http://rubyforge.org/projects/korundum/
- Run with: `ruby hello.rb`

    hello.rb

    require 'Qt4'

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

    hello.show
    app.exec

#### Java

- Package: qtjambiAUR[broken link: archived in aur-mirror]
- Website: http://qt-jambi.org/

    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();
        }
    }

#### Perl

- Package: kdebindings-perlqtAUR[broken link: archived in aur-mirror]
- Website: http://code.google.com/p/perlqt4/
- Run with: `perl hello.pl`

    hello.pl

    use QtGui4;

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

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

#### Lua

- Package: libqtluaAUR[broken link: archived in aur-mirror]
- Website: http://www.nongnu.org/libqtlua/
- Run with: `qtlua hello.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.

## 资源

- 官方网站
- Qt 项目
- Qt 文档
- Planet Qt
- Qt 应用程序

Category:

- Widget toolkits (简体中文)

Hidden category:

- Pages with broken package links