控制腳本
對于每個安裝程式,您可以指定一個控制腳本,用來與安裝程式的部分UI或功能進行互動,控制腳本可以在向導中添加和洗掉頁面,更改現有頁面,進行附加檢查以及通過模擬用戶單擊來與UI互動,例如,這允許安裝時無需人的參與,
腳本格式必須與QJSEngine兼容,
本節介紹為實作這種控制腳本而呼叫的函式功能,它還概述了安裝程式頁面以及每個頁面上可用的小部件,例如按鈕,單選按鈕和行編輯,
撰寫控制腳本
最小的有效腳本至少需要包含一個建構式,如下所示:
function Controller()
{
}
以下示例顯示了一個更高級的腳本,該腳本使用gui JavaScript全域物件方法,用于在簡介頁面上設定新頁面標題和歡迎訊息,并在目標檔案夾頁面上自動單擊下一步按鈕:
function Controller()
{
}
Controller.prototype.IntroductionPageCallback = function()
{
var widget = gui.currentPageWidget(); // get the current wizard page
if (widget != null) {
widget.title = "New title."; // set the page title
widget.MessageLabel.setText("New Message."); // set the welcome text
}
}
Controller.prototype.TargetDirectoryPageCallback = function()
{
gui.clickButton(buttons.NextButton); // automatically click the Next button
}
有關可在控制腳本中使用的JavaScript全域物件的更多資訊,請參見Scripting API章節,
預定義的安裝程式頁面
QInstaller JavaScript物件提供對以下預定義安裝程式頁面的訪問:
> 介紹
> 目標檔案夾
> 組件選擇
> 許可證檢查
> 開始選單選擇
> 準備安裝
> 執行安裝
> 安裝完成
buttons JavaScript物件提供了一組可在安裝程式頁面上使用的按鈕,
以下各節描述了可與安裝程式頁面互動的用戶可實作函式,以及每個頁面上可用的小部件,
簡介頁面
實作Controller.prototype.IntroductionPageCallback()函式,用來與簡介頁面上的部件進行互動,
向導按鈕:
> 下一步按鈕
> 取消按鈕
| 部件 | 簡要描述 |
|---|---|
| ErrorLabel | 顯示錯誤資訊 |
| MessageLabel | 顯示訊息,默認顯示"歡迎來到 |
| InformationLabel | 顯示進度資訊, |
| 單選按鈕 | 簡要描述 |
|---|---|
| PackageManagerRadioButton | 包管理器單選按鈕,作為維護工具運行時,在頁面上顯示, |
| UpdaterRadioButton | 更新程式單選按鈕,作為維護工具運行時,在頁面上顯示, |
| UninstallerRadioButton | 卸載程式單選按鈕,作為維護工具運行時,在頁面上顯示, |
| 進度條 | 簡要描述 |
|---|---|
| InformationProgressBar | 進度條,獲取遠程包時顯示, |
| Qt核心功能 | 簡要描述 |
|---|---|
| packageManagerCoreTypeChanged() | 如果要在維護工具的型別更改時收到通知,請連接到此信號, 注意:僅當用戶啟動二進制檔案,即所謂的維護工具(安裝后),并在單選按鈕之間切換時,才發出此信號, |
示例代碼:
function Controller()
{
var widget = gui.pageById(QInstaller.Introduction); // get the introduction wizard page
if (widget != null)
widget.packageManagerCoreTypeChanged.connect(onPackageManagerCoreTypeChanged);
}
onPackageManagerCoreTypeChanged = function()
{
console.log("Is Updater: " + installer.isUpdater());
console.log("Is Uninstaller: " + installer.isUninstaller());
console.log("Is Package Manager: " + installer.isPackageManager());
}
許可協議頁面
實作Controller.prototype.LicenseAgreementPageCallback()函式,用來與許可協議頁面上的部件進行互動,
向導按鈕:
> 下一步按鈕
> 取消按鈕
> 回傳按鈕
| 部件 | 簡要描述 |
|---|---|
| LicenseListWidget | 列出可用許可證 |
| LicenseTextBrowser | 顯示被選擇許可證內容 |
| AcceptLicenseLabel | 在接受許可證單選按鈕旁邊顯示的文本 |
| RejectLicenseLabel | 在拒絕許可證單選按鈕旁邊顯示的文本 |
| 單選按鈕 | 簡要描述 |
|---|---|
| AcceptLicenseRadioButton | 接收許可協議 |
| RejectLicenseRadioButton | 拒絕許可協議,默認選中, |
目標檔案夾頁面
實作Controller.prototype.TargetDirectoryPageCallback()函式,用來與目標檔案夾選擇頁面上的部件進行互動,
向導按鈕:
> 下一步按鈕
> 取消按鈕
> 回傳按鈕
| 部件 | 簡要描述 |
|---|---|
| MessageLabel | 顯示訊息 |
| TargetDirectoryLineEdit | 顯示安裝目標檔案夾值 |
| WarningLabel | 顯示警告 |
組件選擇頁面
實作Controller.prototype.ComponentSelectionPageCallback()函式,用來與組件選擇頁面上的部件進行互動,
向導按鈕:
> 下一步按鈕
> 取消按鈕
> 回傳按鈕
| 方法 | 簡要描述 |
|---|---|
| selectAll() | 盡可能選擇所有可用包 |
| deselectAll() | 盡可能反選所有可用包 |
| selectDefault() | 將可用包的選擇狀態重置為初始狀態 |
| selectComponent(id) | 選擇具有id(string)的包 |
| deselectComponent(id) | 反選具有id(string)的包 |
| 按鈕 | 簡要描述 |
|---|---|
| SelectAllComponentsButton | 盡可能選擇所有可用包 |
| DeselectAllComponentsButton | 盡可能反選所有可用包 |
| SelectDefaultComponentsButton | 將可用包的選擇狀態重置為初始狀態 |
| ResetComponentsButton | 重置為已安裝組件狀態 |
| FetchCategoryButton | 從類別中獲取組件 |
| 部件 | 簡要描述 |
|---|---|
| CategoryGroupBox | 包含用于選擇存盤庫類別的復選框 |
Installer Framework 3.1引入了存盤庫類別,作為一項新功能, 使用包含存盤庫類別的安裝程式時,可以按其顯示名稱選擇類別,獲取其內容,然后選擇包含的組件進行安裝,
您可以從類別中獲取組件,如下所示:
Controller.prototype.ComponentSelectionPageCallback = function()
{
var page = gui.pageWidgetByObjectName("ComponentSelectionPage");
// if CategoryGroupBox is visible, check one of the checkboxes
// and click fetch button before selecting any components
var groupBox = gui.findChild(page, "CategoryGroupBox");
if (groupBox) {
console.log("groupBox found");
// findChild second argument is the display name of the checkbox
var checkBox = gui.findChild(page, "Archive");
if (checkBox) {
console.log("checkBox found");
console.log("checkBox name: " + checkBox.text);
if (checkBox.checked == false) {
checkBox.click();
var fetchButton = gui.findChild(page, "FetchCategoryButton");
if (fetchButton) {
console.log("fetchButton found");
fetchButton.click();
} else {
console.log("fetchButton NOT found");
}
}
} else {
console.log("checkBox NOT found");
}
} else {
console.log("groupBox NOT found");
}
// you can now select components from the fetched category
}
開始選單檔案夾頁面
實作Controller.prototype.StartMenuDirectoryPageCallback()函式,用來與準備安裝頁面上的部件進行互動,
向導按鈕:
> 下一步按鈕
> 取消按鈕
> 回傳按鈕
| 部件 | 簡要描述 |
|---|---|
| StartMenuPathLineEdit | 顯示用于創建程式快捷方式的檔案夾, |
準備安裝頁面
實作Controller.prototype.ReadyForInstallationPageCallback()函式,用來與準備安裝頁面上的部件進行互動,
向導按鈕:
> 提交按鈕
> 取消按鈕
> 回傳按鈕
| 部件 | 簡要描述 |
|---|---|
| MessageLabel | 顯示訊息, |
| TaskDetailsBrowser | 顯示有關安裝的一些更詳細的資訊 |
執行安裝頁面
實作Controller.prototype.PerformInstallationPageCallback()函式,用來與執行安裝頁面上的部件進行互動,
向導按鈕:
> 提交按鈕
> 取消按鈕
完成頁面
實作Controller.prototype.FinishedPageCallback()函式,用來與安裝完成頁面上的部件進行互動,
向導按鈕:
> 提交按鈕
> 取消按鈕
> 完成按鈕
| 部件 | 簡要描述 |
|---|---|
| MessageLabel | 顯示訊息, |
| RunItCheckBox | 文本欄位,通知用戶可以在安裝完成后啟動應用程式, |
自定義頁面
自定義頁面被注冊為Dynamic${ObjectName},其中${ObjectName}是在UI檔案中設定的物件名稱, 因此,將呼叫Dynamic${ObjectName}Callback()函式,通過部件的物件名稱(從UI檔案)可以實作對部件的訪問,
示例代碼:
function Component()
{
// add page with widget \c SomePageWidget before the target directory page
installer.addWizardPage(component, "SomePageWidget", QInstaller.TargetDirectory)
}
Component.prototype.DynamicSomePageWidgetCallback = function()
{
var page = gui.pageWidgetByObjectName("DynamicSomePageWidget");
page.myButton.click, //direct child of the UI file's widget
page.someFancyWidget.subWidget.setText("foobar") // nested widget
}
訊息框
在執行安裝程式時,例如,程式可能會顯示一些有關發生錯誤的訊息框, 在用戶系統上運行程式時,這沒問題,但這可能會破壞自動化測驗套件, 為解決此問題,Qt Installer框架顯示的所有訊息框,均可通過特定識別符號進行訪問,
| 識別符號 | 可選值 | 描述 |
|---|---|---|
| OverwriteTargetDirectory | Yes, No | 確認使用已有檔案夾作為安裝目標檔案夾 |
| installationError | OK, Retry, Ignore | 執行安裝時發生致命錯誤, |
| installationErrorWithRetry | Retry, Ignore, Cancel | 執行安裝時發生錯誤, 最終可以選擇重試以再次運行, |
| AuthorizationError | Abort, OK | 無法獲取提升的權限, |
| OperationDoesNotExistError | Abort, Ignore | 嘗試執行操作時發生錯誤,但該操作不存在, |
| isAutoDependOnError | OK | 呼叫包腳本時發生錯誤, 無法評估該包是否對其他包具有自動依賴性, |
| isDefaultError | OK | 呼叫包腳本時發生錯誤, 無法評估該包是否默認安裝, |
| DownloadError | Retry, Cancel | 從遠程存盤庫下載存檔哈希時發生錯誤, 用戶可以選擇重試, |
| archiveDownloadError | Retry, Cancel | 從遠程存盤庫下載檔案時發生錯誤,用戶可以選擇重試, |
| WriteError | OK | 寫入維護工具時發生錯誤, |
| ElevationError | OK | 無法獲取提升的權限, |
| unknown | OK | 刪除某些包時發生未知錯誤, |
| Error | OK | 通用錯誤, |
| stopProcessesForUpdates | Retry, Ignore, Cancel | 更新包時發生錯誤, 必須先退出一些正在運行的應用程式或行程,然后才能執行更新,用戶可以選擇“重試”以在停止后在此運行, |
| Installer_Needs_To_Be_Local_Error | OK | 安裝程式是從網路位置啟動的,但是不支持通過網路進行安裝, |
| TargetDirectoryInUse | No | 安裝的目標檔案夾已經包含其他安裝, |
| WrongTargetDirectory | OK | 安裝的目標檔案夾是檔案或符號鏈接, |
| AlreadyRunning | OK | 另一個應用程式實體已在運行, |
示例代碼:
function Controller()
{
installer.autoRejectMessageBoxes;
installer.setMessageBoxAutomaticAnswer("OverwriteTargetDirectory", QMessageBox.Yes);
installer.setMessageBoxAutomaticAnswer("stopProcessesForUpdates", QMessageBox.Ignore);
}
原創造福大家,共享改變世界
獻出一片愛心,溫暖作者心靈
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/5177.html
標籤:其他
