多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# Qt5 工具包简介 > 原文: [http://zetcode.com/gui/qt5/introduction/](http://zetcode.com/gui/qt5/introduction/) 在 Qt5 教程的这一部分中,我们将介绍 Qt5 库。 我们将安装 Qt5 库并创建我们的第一个小型 Qt5 应用。 Qt 最初是由挪威软件公司 Trolltech 开发的。 2008 年,该公司被诺基亚收购。 2012 年 8 月,一家芬兰开发公司 Digia 从诺基亚那里收购了 Qt 软件技术。 同时,创建了一个 Qt 项目,其中开源 Qt 的开发继续进行。 开源 Qt 工具包的网站可以在 [qt.io](http://www.qt.io) 中找到。 目前,由 Digia 的子公司 Qt 公司和开放源代码治理下的 Qt 项目(包括个人开发者和公司)共同开发 Qt。 ## Qt Qt 是一个跨平台的应用开发框架。 使用 Qt 开发的一些知名应用是 KDE,Opera,Google Earth,Skype,VLC,Maya 或 Mathematica。 Qt 于 1995 年 5 月首次公开发布。它具有双重许可。 它可以用于创建开源应用以及商业应用。 Qt 工具箱是一个非常强大的工具箱。 它在开源社区中已经建立。 全世界有成千上万的开源开发者在使用 Qt。 ## 下载并解压缩 我们转到 [download.qt.io/official_releases/qt/](http://download.qt.io/official_releases/qt/) 页面。 (由于下载链接过去经常更改,因此您可能需要用 Google 搜索当前链接。)我们选择最新的 Qt 5.x 来源。 在创建本教程时,最新的数据是 Qt 5.5.1。 接下来,我们将从源代码安装 Qt。 ```cpp $ ls qt-everywhere-opensource-src-5.5.1.tar.gz qt-everywhere-opensource-src-5.5.1.tar.gz ``` 从下载页面,我们下载 Qt5 源。 我们使用 TAR 文件。 (我们为自己省了一些麻烦。ZIP 文件具有 Windows 行尾。) ```cpp $ tar -xzvf qt-everywhere-opensource-src-5.5.1.tar.gz ``` 该命令会将所有文件解压缩到目录`qt-everywhere-opensource-src-5.5.1/`。 ```cpp $ du -hs qt-everywhere-opensource-src-5.5.1 2.0G qt-everywhere-opensource-src-5.5.1 ``` 现在目录的大小为 2G。 ```cpp $ cd qt-everywhere-opensource-src-5.5.1/ ``` 我们转到创建的目录。 在`README`文件中,有安装说明。 安装简单明了,但需要花费大量时间。 ## 从源安装 在开始构建 Qt5 之前,我们可能需要安装一些其他库。 例如,如果要从 Qt 连接到 MySQL,则需要在系统上安装`libmysqld-dev`。 我们以经典方式安装库。 在 Unix 系统上,软件的安装分为三个步骤。 * 配置 * 构建 * 安装 ```cpp $ ./configure -prefix /usr/local/qt5 Which edition of Qt do you want to use ? Type 'c' if you want to use the Commercial Edition. Type 'o' if you want to use the Open Source Edition. ``` 首先,我们运行配置脚本。 该脚本将询问我们是否需要 Qt5 库的商业版或开源版。 该脚本将为我们的机器类型配置库。 默认情况下,Qt 将安装在`/usr/local/Qt-5.5.1/`目录中。 这可以通过配置脚本的`-prefix`参数进行更改。 我们将库安装到`/usr/local/qt5/`目录中。 请注意,此处的安装字有两个含义。 这是整个过程,包括所有三个步骤。 这也意味着“将文件移动到特定目录”,这是最后第三步。 ```cpp This is the Open Source Edition. You are licensed to use this software under the terms of the Lesser GNU General Public License (LGPL) versions 2.1. You are also licensed to use this software under the terms of the GNU General Public License (GPL) versions 3. Type '3' to view the GNU General Public License version 3. Type 'L' to view the Lesser GNU General Public License version 2.1. Type 'yes' to accept this license offer. Type 'no' to decline this license offer. Do you accept the terms of either license? yes ``` 确认许可协议。 ```cpp Running configuration tests... The test for linking against libxcb and support libraries failed! You might need to install dependency packages, or pass -qt-xcb. ``` 如果脚本失败并显示上述消息,则我们需要安装一些其他 xcb 库,或者使用`-qt-xcb`选项再次运行该脚本。 ```cpp $ ./configure -prefix /usr/local/qt5 -qt-xcb ``` 使用`-qt-xcb`选项,可以构建某些库,而不是针对系统库进行链接。 ```cpp ... Qt modules and options: Qt D-Bus ............... yes (loading dbus-1 at runtime) Qt Concurrent .......... yes Qt GUI ................. yes Qt Widgets ............. yes Large File ............. yes QML debugging .......... yes Use system proxies ..... no ... SQL drivers: DB2 .................. no InterBase ............ no MySQL ................ yes (plugin) OCI .................. no ODBC ................. no PostgreSQL ........... yes (plugin) SQLite 2 ............. no SQLite ............... yes (plugin, using bundled copy) TDS .................. no tslib .................. no udev ................... yes xkbcommon-x11........... yes (bundled copy, XKB config root: /usr/share/X11/xkb) xkbcommon-evdev......... yes zlib ................... yes (system library) Qt is now configured for building. Just run 'make'. Once everything is built, you must run 'make install'. Qt will be installed into /usr/local/qt5 Prior to reconfiguration, make sure you remove any leftovers from the previous build. ``` 这是配置脚本的部分输出。 输出告诉我们准备构建哪些组件。 例如,将创建用于 MySQL 和 PostgreSQL 的 SQL 驱动程序,而不用于 DB2 或 InterBase。 ```cpp $ make ``` 我们使用`make`命令开始构建过程。 Qt 工具包的构建可能要花费几个小时。 这取决于处理器的能力。 最后一步是安装文件或将文件移动到目录中。 ```cpp $ sudo make install ``` 此命令完成安装过程。 该库现在安装在`/usr/local/qt5/`目录中。 我们要做的最后一件事是将 Qt5 路径添加到`PATH`系统变量。 bash 用户需要编辑`.profile`文件或`.bashrc`文件。 ```cpp $ PATH=/usr/local/qt5/bin:$PATH $ export PATH ``` 我们已经将 Qt5 库的 bin 目录的路径添加到`PATH`环境变量。 再次登录后,更改将处于活动状态。 ## 从包安装 从包安装 Qt 更加容易。 Linux 包通常不包含最新的 Qt 版本。 ```cpp $ sudo apt-get install qt5-default ``` 上面的命令在基于 Debian 的 Linux 上安装 Qt5。 ## 版本 我们的第一个程序打印 Qt5 库的版本。 `version.cpp` ```cpp #include <QtCore> #include <iostream> int main() { std::cout << "Qt version: " << qVersion() << std::endl; } ``` `qVersion()`函数在运行时以字符串形式返回 Qt 的版本号。 ```cpp $ g++ -o version version.cpp -I/usr/local/qt5/include/QtCore -I/usr/local/qt5/include -L/usr/local/qt5/lib -lQt5Core -fPIC ``` 上面的命令将编译示例。 请注意,您的 Qt5 库可能安装在其他位置。 ```cpp $ ./version Qt version: 5.5.1 ``` 本教程中使用的 Qt5 库的版本是 5.5.1。 ## 测试一个小的 GUI 示例 最后,我们编写一个小应用。 该应用包含一个普通窗口。 `simple.cpp` ```cpp #include <QApplication> #include <QWidget> int main(int argc, char *argv[]) { QApplication app(argc, argv); QWidget window; window.resize(250, 150); window.setWindowTitle("Simple example"); window.show(); return app.exec(); } ``` 要构建此示例,我们使用`qmake`工具。 ```cpp $ qmake -project ``` 该命令创建一个扩展名为`.pro`的项目文件。 `simple.pro` ```cpp ###################################################################### # Automatically generated by qmake (3.0) Fri Oct 30 17:11:00 2015 ###################################################################### TEMPLATE = app TARGET = simple INCLUDEPATH += . # Input SOURCES += simple.cpp QT += widgets ``` 默认情况下,项目中不包括 Qt Widgets 模块。 因此,我们将模块添加到文件的末尾。 ```cpp $ qmake $ make ``` 我们使用上述命令构建程序。 `qmake`创建一个`Makefile`,`make`命令生成该程序。 如果 Qt5 安装目录不是`PATH`变量的一部分,我们可以提供`qmake`工具的完整路径。 ```cpp $ /usr/local/qt5/bin/qmake -project $ /usr/local/qt5/bin/qmake $ make ``` ![Simple example](https://img.kancloud.cn/24/aa/24aadd0240e978e0f93a92e1c265c6cb_252x176.jpg) 图:简单 example 本章是 Qt5 库的简介。