Qt 製作更新檔 Qt Installer Framework

前言

如果按照 Qt Installer Framework 正常的方式製作安裝檔,使用 startmenu的範本,安裝後正常使用沒有問題,但程式後續需要更新升級,就必須要從控制台把程式完整移除後才能重新安裝,否則點選安裝檔時會顯示

The directory you selected already exists and contains an installation. Choose a different target for installation.

要避免這種狀況發生,可以做一個範本在安裝時會提醒使用者覆蓋掉原本路徑的檔案,且自動幫使用者做移除程式的動作

這個動作不算是製作更新檔,算是製作一個會幫使用者省掉一些移除步驟的安裝檔

流程

1. 需要先手動到控制台移除舊的程式及 C:/ 底下的目錄,因為舊的目錄下範本是沒有更新的,只有第一次需要移除,之後產生的安裝檔會自動移除

2. 使用預設的 startmenu範本,更新 config 下 config.xml,更新標籤
<Name> </Name>
<Version> </Version>
<Title> </Title>

3. 用 release 的方式 build 出.exe 檔,使用 windeployqt 語法 build 出缺少的檔案,放到 data 資料夾下

以上的這些步驟都跟 Qt 製作安裝檔 那篇文章相同,以下是需要修改的地方

4. 開啟 meta 資料夾下的 package.xml
要新增加 Required 跟 UserInterfaces 兩個標籤
targetwidget.ui 是後面需要增加的檔案
<code class="prettyprint">
  <Required>true</Required>
    <Script>installscript.qs</Script>
<UserInterfaces>
        <UserInterface>targetwidget.ui</UserInterface>
    </UserInterfaces>
</code>

5. 更新 installscripts.qs
刪掉所有語法,增加以下 function,註釋直接加在語法裡
xxx 為程式名
<code class="prettyprint">
  <Required>true</Required>
    <Script>installscript.qs</Script>
<UserInterfaces>
        <UserInterface>targetwidget.ui</UserInterface>
    </UserInterfaces>
</code>

紅字顯示

6. 新增 targetwidget.ui 可以用記事本直接新增,記得存檔要改成所有檔案不存成.txt
 
installer Framework examples 下,Dynamic 範本下也有一個一樣名稱的檔案,這個檔案是用舊的 targetwidget.ui 檔案稍做修改,增加下圖黃色差異,為了要顯示不同的 Layout 提醒使用者



可以直接複製貼上

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>TargetWidget</class>
 <widget class="QWidget" name="TargetWidget">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>491</width>
    <height>190</height>
   </rect>
  </property>
  <property name="sizePolicy">
   <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
    <horstretch>0</horstretch>
    <verstretch>0</verstretch>
   </sizepolicy>
  </property>
  <property name="minimumSize">
   <size>
    <width>491</width>
    <height>190</height>
   </size>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout">
   <item>
    <widget class="QLabel" name="label_2">
     <property name="text">
      <string>Please specify the folder where Dynamic Page Installer Example will be installed.</string>
     </property>
    </widget>
   </item>
   <item>
    <layout class="QHBoxLayout" name="horizontalLayout">
     <item>
      <widget class="QLineEdit" name="targetDirectory">
       <property name="readOnly">
        <bool>true</bool>
       </property>
      </widget>
     </item>
     <item>
      <widget class="QToolButton" name="targetChooser">
       <property name="sizePolicy">
        <sizepolicy hsizetype="Fixed" vsizetype="Preferred">
         <horstretch>0</horstretch>
         <verstretch>0</verstretch>
        </sizepolicy>
       </property>
       <property name="minimumSize">
        <size>
         <width>0</width>
         <height>0</height>
        </size>
       </property>
       <property name="text">
        <string>...</string>
       </property>
      </widget>
     </item>
    </layout>
   </item>
   <item>
    <spacer name="verticalSpacer">
     <property name="orientation">
      <enum>Qt::Vertical</enum>
     </property>
     <property name="sizeHint" stdset="0">
      <size>
       <width>20</width>
       <height>122</height>
      </size>
     </property>
    </spacer>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>

7. 到 data 資料夾下新增一個 scripts 資料夾,這個資料夾在執行 windeployqt 是不會產生的要手動去加,主要是去執行這段語法

舊的程式需要先移除也是因為沒有這個資料夾下的檔案


新增一個 auto_uninstall.qs 檔案,一樣可以使用記事本 記得要存成所有檔案,結尾 .qs

function Controller()
{
    gui.clickButton(buttons.NextButton);
    gui.clickButton(buttons.NextButton);

    installer.uninstallationFinished.connect(this, this.uninstallationFinished);
}

Controller.prototype.uninstallationFinished = function()
{
    gui.clickButton(buttons.NextButton);
}

Controller.prototype.FinishedPageCallback = function()
{
    gui.clickButton(buttons.FinishButton);
}

8. 到這一步就完成了所有動作,最後執行 readme 語法 build 出 installer


9. 有一點要提醒,舊的installer要先刪除,否則執行會失敗


10. 最後自動執行刪除的那個步驟是在按 Next 的按件後會馬上執行,要仔細去看才會發現.
按照步驟安裝完後即更新完成

留言

熱門文章

TourCard 開通全紀錄 台胞證 支付寶

svn 刪除使者資訊 & 查看使用者帳號密碼

[ 教學 ] 如何使用 Sourcetree 對 Github 進行版本控制 #Mac OS 版本控制