Qt program guide released

Qt program guide released

Mighty Taoge blog posts address  https://jaredtao.github.io/2019/09/02/Qt%E5%AE%9E%E7%94%A8%E6%8A%80%E8%83%BD6- % E7% A8% 8B% E5 % BA% 8F% E5% 8F% 91% E5% B8% 83% E6% 8C% 87% E5% 8D% 97 /

Know almost article addresses  https://zhuanlan.zhihu.com/p/80919147

 

Brief introduction

This article is the sixth article "Qt practical skills" series, discussed here Taoge knowledge released Qt application.

(Part V "Qt signals and slots to master practical skills 5- usage details" still finishing in a few days will be issued)

background

There are a lot of people to Taoge asked questions Qt program published on the web although one can find a lot of tutorials, but less reliable.

So this time do my best round, finishing in detail some of the knowledge Qt release of the program, hoping to help more people.

For veterans, many have stepped on a pit, nothing more than the right dll in the right path.

For starters, the details can say a few words, would be a great help, detours, save a few hours, or even days are possible.

If there are omissions, mistakes, and welcome to add, correct me.

Qt installation

Qt official website to download at this address:  http://download.qt.io/official_releases

Online or offline installation package installation package will do.

About Qt version of choice, Taoge recommendations:

体验新特性,就用最新版本;项目开发,用长期支持版(LTS)的最后一个修正版本,稳定、bug最少。

You can view information on the official Qt Wiki  https://wiki.qt.io/Main

So far (2019/9/2), the latest version is 5.13.0, LTS versions 5.9 and 5.12, and 5.9 last modified version is 5.9.8, is 5.12 to 5.12.4

FIG example, the installation package is offline 5.9.8, provides the executable program windows, mac and linux three kinds of systems.

Wherein the windows Setup "qt-opensource-windoiws-x86-5.9.8.exe", with a size of 2.4G, which

It contains multiple versions of msvc_x86, msvc_x64, mingw, Android and other Qt tools chain. In the download is complete, installation

The process can be checked separately. Other versions are also similar.

How to install Qt, not elaborate, all else fails to refer to the entry-level tutorial it ...

Qt directory structure

It is assumed that we are, ready for Qt, first look at the path where Qt installed what are things.

Taoge using a Windows 10 system, installed Qt version is 5.12.4, as an example to illustrate other systems and versions of the actual subject.

Qt installation path

Taoge installed in D: \ Qt under Online path \, as shown:

Where "vcredist" folder contains the run-time library installer msvc2015 and msvc2017 of (the back will say how to use, not msvc compiler does not require)

"Tools" folder, including QtCreator, OpenSSL library (optional) and two versions of MinGW (optional).

(There are further Qt3DStudio, negligible)

"5.12.4" folder, the path is at the heart of Qt, Qt tool chain which contains multiple versions of the header files, dynamic-link libraries

Here Taoge the msvc2017, msvc2017_64 installation, mingw73_64 and android_x86.

Note msvc2017 x86 architecture Qt library, msvc2017_64 is x64 architecture.

If there msvc2013, msvc2015 also empathy.

Qt core path

Next, look at the path of the core focus, Qt to msvc2017_64 folder, for example

bin folder contains a dll exe program Qt provides a variety of tools, as well as a dynamic link library

Wherein the tool includes qmake.exe and windeployqt.exe, windeployqt.exe is the main tool we are discussing today.

动态链接库全部是两份dll,比如Qt5Cored.dll和Qt5Core.dll,文件名末尾带’d’表示debug版本的,另一个不带’d’的是release版本。

debug版本和release版本的主要区别:debug没有开编译器优化、携带了调试信息,release开了编译器优化O2,去掉了多余的信息

(图中还有pdb文件,是涛哥单独安装的,用来调试Qt源码,可以忽略)

和bin同级的,还有plugins文件夹,包含一些Qt用到的插件

比如imageformats文件夹中提供了jepg、gif、webp等图片格式的功能支持的插件,platforms文件夹则提供了平台插件,特别是

qwindows.dll这一个,在windows平台是必不可少的。

和bin同级的,另外一个文件夹是’qml’文件夹,包含Qml的各种功能模块。

和bin同级的其它文件夹,resources是WebEngine模块专用的,translations提供了

Qt内置的翻译文件,剩下的和发布无关,就不多说了。

HelloDeploy

这里新建一个简单的Hello World程序,名字就叫”HelloDeploy”。

同时为了说明问题,涛哥添加一些常用的模块。

在pro文件中,QT += 那一行该写的都写上:

在main.cpp中包含一下各个模块的头文件,再分别创建一个对象实例,调用一些简单的函数:

这样一个多模块依赖的程序就写好了。

Window编译和发布

Window 编译

这里要特别注意,编译器的选择, 以及编译用的是debug模式还是release模式。

涛哥这里是msvc2017_x64版本

一般发布用release模式。

编译完成后,默认在build-xxxx-release/release/文件夹中会生成我们的exe程序。

我们将这个exe复制出来,新建一个release文件夹,放进去

这时候可以尝试双击运行它,会提示缺少dll

Window 发布

发布程序,其实就是把exe程序依赖的dll和相关资源都放在一起,保证双击运行即可。

我们前面提过的windeployqt.exe,是Qt提供的命令行工具,能帮助我们自动把需要的dll或资源复制过来。

  1. 我们先打开一个命令行

可以从开始菜单找到Qt提供的命令行

注意选对版本。这种命令行在启动时已经设置好了QT的环境变量,可以直接输入windeployqt.exe

也可以用普通的命令行,使用windeployqt.exe时带上绝对路径即可。

涛哥一般用普通的命令行,因为绝对路径不易出错。

  1. cd到release目录

这里说一个windows启动命令行的小技巧:在release文件夹中,按住键盘shift键,然后按鼠标右键,弹出的右键菜单,

会比普通的右键菜单多一个“在此处打开命令窗口”,点击就能在release文件夹打开命令行窗口。

如果没有这个功能,就得手动输入cd指令,进入release路径。

  1. 执行windeployqt命令

这里通过绝对路径来使用windeployqt:

d:\qt\Online\5.12.4\msvc2017_64\bin\windeployqt.exe HelloDeploy.exe

HelloDeploy这个程序还用到了Qml,用到Qml的程序,要给windeployqt加上qmldir参数,写上相应的msvc2017_64\qml文件夹

(没用到qml的程序,不要加这一步)

d:\qt\Online\5.12.4\msvc2017_64\bin\windeployqt.exe HelloDeploy.exe –qmldir d:\qt\Online\5.12.4\msvc2017_64\qml

写好windeployqt命令后按回车执行

正确执行后,release文件夹下,多了很多dll,以及一些文件夹。

这时候我们双击运行HelloDeploy.exe, 就可以正常启动了。

将整个文件夹压缩或拷贝到其它没有Qt环境的电脑上,也是可以启动的。

只要dll齐备了,制作安装包也不是问题。(后续有时间,我再写安装包制作的教程)

VS运行时库

如果是VS编译的程序,需要将QT路径下对应的vcredist_xxx.exe带上。

如果其它电脑上有vs运行时则可以直接运行,如果没有,就需要运行一下vs运行时安装包。

经常玩一些单机游戏的同学应该都知道这个问题。

有的电脑环境特殊,可能运行时库无法安装上去,这时候需要一些变通的方法,

直接将运行时库安装包里面的dll复制出来即可。

怎么取出来?在能安装的电脑上装一遍,然后就有dll了,名字是vcruntime140.dll、

msvcp140.dll之类的,这里就不展开说了。

常见的错误处理

一般使用windeployqt,大部分库都能自动拷贝,不需要手动处理,

只有极少数情况下,windeployqt跑完,会缺失一些库,还要手动处理一下。

遇到这种情况,用依赖检查工具Dependencies即可快速定位问题。

Dependencies下载链接: https://github.com/lucasg/Dependencies

下面列举一些常见的错误信息

应用程序无法正常启动

最容易出现这种错误的情况是,程序是64位编译出来的,而同级目录下的dll是32位的,

或者同级目录下没有dll,但是环境变量中指向了32位的dll。(所以涛哥没有设置环境变量)

32位和64位倒过来也是。

如果dll版本是匹配的,还有可能出现的情况是缺少第三方库。

这里说一个检查依赖的方法:

将HelloDeploy.exe重命名为HelloDeploy.dll,然后用Dependencies打开,就可以查看少哪些库

如上图,红色问号的表示缺少的库。

找齐了依赖的库,再把程序的扩展名改回exe即可。

启动失败 - no Qt platform plugin

这种情况,是QT路径下的 plugins/platforms/qwindows.dll文件没有复制过来。

注意这个dll文件直接复制到exe同级是不起作用的,要放在exe程序同级的platforms文件夹里,或者同级

的plugins/platforms文件夹里

OpenGL Context 创建失败

这种情况,一般是OpenGL相关的库没有复制过来,补上就好了

整理

我们看到,exe同级目录下,windeployqt将一堆的文件夹放在了那里,有些混乱。

涛哥观察并验证了一下,其实可以做个简单的整理。

Qt开头的文件夹都是qml的模块,剩下的文件夹除了translations都是Qt的插件,

所以新建两个文件夹qml和plugins, 分别把qml模块和插件归入其中。

这样的结构,和QT安装路径下的结构是相似的。

这也正是Qt支持的插件加载路径、qml模块加载路径。

同级的dll则是windows系统默认的动态库加载规则,不方便修改

可以参考msdn:

https://docs.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order

简单裁剪

如果你熟悉Qt的各个模块,可以进行一些裁剪。以下都是些个人经验。

不熟悉请慎重!

不熟悉请慎重!

不熟悉请慎重!

(当然静态编译也是一种裁剪的途径)

删减dll

首先可以把单元测试的dll去掉

Qt5Test.dll

Qt5QuickTest.dll

如果没用到windows扩展,Qt5WinExtras.dll也可以去掉

其次,如果你不需要内置的翻译文件,translations文件夹也可以删掉

删减plugins

再来看一下plugins:

其中platforms是必不可少的,剩下的HelloDeploy都没用到,可以去掉。

常见程序会用的包括:

imageformats 图片格式支持

iconengines 小图标功能

sqldrivers 数据库驱动,这个保留用到的数据库足够了

其他的看情况删减。

删减qml

最后看一下Qml文件夹,如果程序完全没用qml,直接删掉就好了。

按windeployqt给HelloDeploy提供的这些,逐个文件夹来说:

  • Qt/labs 一般不推荐Qml中引入labs中的实验品,但是有些情况下功能缺失,只能引入。

如果Qml中使用了Quick.Dialog(不是labs.Dialog),它本身还是依赖的labs中的东西,一般是folderlistmodel和settings,

这时候还是不要动labs了,就按照windeployqt给的放着。

  • Qt/WebSockets Qml的Websocket功能,用了就放着,没用可以删掉。

  • QtGraphicalEffects Qml的一些ShaderEffect特效,用了就放着,没用到可以删掉

  • QtMultimedia Qml的多媒体模块,用了就放着,没用到可以删掉

  • QtQml/Models.2 数据Model, 经常用。

  • QtQuick 这里面大部分都是Qml中常用的,QtQuick/Extras可以按情况删掉

  • QtQuick.2 常用的

  • QtTest 单元测试,删掉吧

  • QtWinExtras Windows扩展,没用到可以去掉

 

============== End

 

Guess you like

Origin www.cnblogs.com/lsgxeva/p/12298087.html