Qt installer framework

在網上發現一篇介紹 qt製作安裝包的程序

一、介紹

    使用Qt庫開發的應用程序,一般有兩種發佈方式:(1)靜態編譯發佈。這種方式使得程序在編譯的時候會將Qt核心庫全部編譯到一個可執行文件中。其優勢是簡單單一,所有的依賴庫都集中在一起,其缺點也很明顯,可執行程序體量較大,光Qt核心庫加起來就得十多兆。(2)製作安裝包發佈。這種方式的原理也簡單,就是將可執行程序和其依賴的庫文件一起打包壓縮,製作成安裝包發佈。製作安裝包的工具挺多,今天要說的是Qt官方的安裝包製作框架Qt installer framework.這個框架由Qt官方出品,廣泛應用於Qt library, Qt Creator等安裝包產品。網上搜了一下,相關教程貌似不多。

二、配置使用

    下載並安裝好Qt installer framework之後,需要創建目標文件夾。所有需要打包的文件都放到這個文件夾下。我的文件夾內容如下:

    然後我們需要安裝如下的目錄組織結構分別創建文件夾,注意的是,一定要安裝這樣的順序組織文件夾哦。最簡單的方法是從Qt installer framework安裝目錄下的examples裏面複製一個出來,在此基礎上進行修改:

    我創建的根文件夾名稱爲sc.在sc下面又創建了兩個子文件夾:config和packages。在config文件夾下創建一個config.xml文件,該文件用於定製安裝包的UI和行爲,其基本格式如下:

<?xml version="1.0"?>

<Installer>

    <Name>Some Application</Name>

    <Version>1.0.0</Version>

    <Title>Some Application Setup</Title>

    <Publisher>Your Company</Publisher>

    <ProductUrl>http://www.your-fantastic-company.com</ProductUrl>

    <InstallerWindowIcon>installericon</InstallerWindowIcon>

    <InstallerApplicationIcon>installericon</InstallerApplicationIcon>

    <Logo>logo.png</Logo>

    <Watermark>watermark.png</Watermark>

    <RunProgram></RunProgram>

    <RunProgramArguments></RunProgramArguments>

    <RunProgramDescription></RunProgramDescription>

    <StartMenuDir>Some Application Entry Dir</StartMenuDir>

    <UninstallerName>SDKMaintenanceTool</UninstallerName>

    <AllowNonAsciiCharacters>true</AllowNonAsciiCharacters>

    <Background>background.png</Background>



    <TargetDir>@homeDir@/testinstall</TargetDir>

    <AdminTargetDir>@rootDir@/testinstall</AdminTargetDir>

    <RemoteRepositories>

        <Repository>

            <Url>http://www.your-repo-location/packages/</Url>

        </Repository>

    </RemoteRepositories>

</Installer>

    其中,name和version子元素是必需的,其他子元素則是可選的,並且順序可以任意。關於這些配置項的含義,在官方網站的鏈接上可以查看

    在上面的圖中我們可以看到,我們創建名爲org.hust.simulator的文件夾。事實上,Qt installer framework引入了組件的概念。即每一個獨立模塊可以單獨放在一個組件component中,對於大型程序這樣清晰的結構有利於工程組織,降低複雜度。例子中比較簡單,只使用了一個組件,根據需要可以創建多個組件:

1

2

3

4

5

6

7

8

9

10

11

12

13

packages

    - com.vendor.root

        - data

        - meta

    - com.vendor.root.component1

        - data

        - meta

    - com.vendor.root.component1.subcomponent1

        - data

        - meta

    - com.vendor.root.component2

        - data

        - meta

   

  可以注意到,在packages文件夾下每個組件都需要創建兩個子文件夾meta和data。meta目錄下用於存放一些配置文件,用於指定安裝和部署過程。在meta文件夾中至少需要一個package.xml及所有在該文件中引用到的資源文件,如腳本文件、界面資源、翻譯文件等。package.xml文件描述了一個組件的基本信息,其格式如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

<?xml version="1.0"?>

<Package>

    <DisplayName>QtGui</DisplayName>

    <Description>Qt gui libraries</Description>

    <Description xml:lang="de_de">Qt GUI Bibliotheken</Description>

    <Version>1.2.3</Version>

    <ReleaseDate>2009-04-23</ReleaseDate>

    <Name>com.vendor.root.component2</Name>

    <Dependencies>com.vendor.root.component1</Dependencies>

    <Virtual>false</Virtual>

    <Licenses>

        <License name="License Agreement" file="license.txt" />

    </Licenses>

    <Script>installscript.qs</Script>

    <UserInterfaces>

        <UserInterface>specialpage.ui</UserInterface>

        <UserInterface>errorpage.ui</UserInterface>

    </UserInterfaces>

    <Translations>

        <Translation>sv_se.qm</Translation>

        <Translation>de_de.qm</Translation>

    </Translations>

    <DownloadableArchives>component2.7z, component2a.7z</DownloadableArchives>

    <AutoDependOn>com.vendor.root.component3</AutoDependOn>

    <SortingPriority>123</SortingPriority>

    <UpdateText>This changed compared to the last release</UpdateText>

    <Default>false</Default>

    <ForcedInstallation>false</ForcedInstallation>

    <Essential>false</Essential>

    <Replaces>com.vendor.root.component2old</Replaces>

</Package>

  其中License節中指定了協議條款,file屬性指定了協議條款所在的文件。UserInterfaces和Translations節分別指定了界面資源文件和翻譯文件。Script節指定了腳本文件,在腳本文件中可以對安裝過程進行更爲詳細的定製。樣例中其他元素名稱及值都比較容易懂,對着修改並不困難。全部選項可以上官網查看

    data文件夾中則用於存放我們實際需要打包的程序文件(*.dll, *.exe等),Qt installer framework在製作過程中用archivegen會將這些文件壓縮成7zip格式。安裝過程中自動從壓縮包中提取出數據文件。最後我的文件組織結構如下:

     爲了方便,我將這個sc文件夾直接拖到了Qt installer framework的安裝目錄下,這樣就可以在命令行下直接使用binarycreator.exe這個工具了。準備工作完成,開始生成安裝包:

1

binarycreator.exe -c sc\config\config.xml -p sc\packages my_installer.exe -v

  命令運行完成,將在binarycreator.exe所在目錄下生成my_installer.exe文件。運行起來看看:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章