組件腳本
對于每個組件,您可以指定一個腳本,來準備要由安裝程式執行的操作,腳本格式必須與QJSEngine兼容,
構造
腳本必須包含安裝程式在加載腳本時創建的Component物件, 因此,腳本必須至少包含Component()函式,該函式執行初始化,例如將頁面放置在正確的位置或連接信號和槽,
以下代碼片段將ErrorPage頁面(這是從errorpage.ui加載的用戶界面檔案的類名),放置在準備安裝頁面的前面,并將其完整性設定為false,
function Component()
{
// Add a user interface file called ErrorPage, which should not be complete
installer.addWizardPage( component, "ErrorPage", QInstaller.ReadyForInstallation );
component.userInterface( "ErrorPage" ).complete = false;
}
有關更多資訊,請參見installer::addWizardPage()和 component::userInterface()的檔案,
安裝鉤子
您可以在腳本中添加以下鉤子方法:
| 方法 | 描述 |
|---|---|
| Component.prototype.retranslateUi | 當安裝程式的語言更改時呼叫, |
| Component.prototype.createOperations | 見component::createOperations(), |
| Component.prototype.createOperationsForArchive | 見component::createOperationsForArchive(). |
| Component.prototype.createOperationsForPath | 見component::createOperationsForPath(). |
全域變數
安裝程式將以下符號放入腳本空間:
| 符號 | 描述 |
|---|---|
| installer | 參考組件的QInstaller |
| component | 參考組件的Component |
訊息框
您可以使用以下靜態成員函式從腳本中顯示QMessageBox:
> QMessageBox::critical()
> QMessageBox::information()
> QMessageBox::question()
> QMessageBox::warning()
為了方便起見,可以通過QMessageBox.Ok,QMessageBox.Open等使QMessageBox::StandardButton可用,
向組件添加操作
例如,在拷貝檔案或更新檔案內容時,你可能需要在提取內容后添加自定義操作,您可以在腳本中使用component::addOperation(),來創建并添加更新操作到安裝中,如果要運行要求管理員權限的操作,請改用component::addElevatedOperation(),
操作需要在實際安裝步驟之前添加,覆寫component::createOperations(),以注冊組件的自定義操作,
每個操作都有一個唯一的鍵,用于識別的,且最多可以包含五個引數,在引數值中,可以使用通過installer::setValue()設定的變數值,有關更多資訊,請參見預定義變數章節,
有關所有可用操作的總結,請參見操作章節,
注冊自定義操作
您可以在安裝程式中注冊自定義安裝操作,通過派生KDUpdater::UpdateOperation類, 以下代碼顯示了必須實作的方法:
#include <UpdateOperation>
class CustomOperation : public KDUpdater::UpdateOperation
{
public:
CustomOperation()
{
setName( "CustomOperation" );
}
void backup()
{
// do whatever is needed to restore the state in undoOperation()
}
bool performOperation()
{
const QStringList args = arguments();
// do whatever is needed to do for the given arguments
bool success = ...;
return success;
}
void undoOperation()
{
// restore the previous state, as saved in backup()
}
bool testOperation()
{
// currently unused
return true;
}
CustomOperation* clone() const
{
return new CustomOperation;
}
QDomDocument toXml()
{
// automatically adds the operation's arguments and everything set via setValue
QDomDocument doc = KDUpdater::UpdateOperation::toXml();
// if you need any information to undo the operation you did,
// add them to the doc here
return doc;
}
bool fromXml( const QDomDocument& doc )
{
// automatically loads the operation's arguments and everything set via setValue
if( !KDUpdater::UpdateOperation::fromXml( doc ) )
return false;
// if you need any information to undo the operation you did,
// read them from the doc here
return true;
}
};
最后,您需要注冊您的自定義操作類,如下所示:
#include <UpdateOperationFactory>
KDUpdater::UpdateOperationFactory::instance().registerUpdateOperation< CustomOperation >( "CustomOperation" );
現在,您可以在安裝程式中使用您的操作了,方式和預定義操作相同,
預定義變數
您可以在腳本中使用以下預定義的變數來方便檔案夾訪問:
| 符號 | 描述 |
|---|---|
| ProductName | 要安裝的產品的名稱,如config.xml中所定義, |
| ProductVersion | 要安裝的產品的版本號,如config.xml中所定義, |
| Title | 安裝程式的標題,如config.xml中所定義, |
| Publisher | 安裝程式的發布者,如config.xml中所定義, |
| Url | 產品網址,如config.xml中定義, |
| StartMenuDir | 開始選單組,如config.xml中所定義, 僅在Windows上可用, |
| TargetDir | 用戶選擇的安裝目標檔案夾, |
| DesktopDir | 包含用戶桌面的檔案夾名稱,僅在Windows上可用, |
| os | 當前平臺:"x11", "win", or "mac",變數已啟用:請改用systemInfo, |
| RootDir | 檔案系統的根目錄, |
| HomeDir | 當前用戶的主目錄, |
| ApplicationsDir | 應用程式檔案夾, 例如,Windows上的C:\Program Files,Linux上的/opt和macOS上的/Applications, 另請參閱表格,列出了Windows上應用程式目錄示例, |
| ApplicationsDirX86 | 32位程式的應用程式檔案夾, 這在Windows上很有用,在其他平臺上與ApplicationsDir相同,例如,Windows上的C:\Program Files (x86), 另請參閱表格,列出了Windows上應用程式目錄示例, |
| ApplicationsDirX64 | 64位程式的應用程式檔案夾, 這在Windows上很有用,在其他平臺上與ApplicationsDir相同,例如,Windows上的C:\Program Files, 另請參閱表格,列出了Windows上應用程式目錄示例, |
| InstallerDirPath | 包含安裝程式可執行檔案的目錄, |
| InstallerFilePath | 安裝程式可執行檔案的檔案路徑, |
| UserStartMenuProgramsPath | 包含當前用戶開始選單中各子項的檔案夾的路徑,例如,C:\Users\USERNAME\AppData\Roaming\Microsoft\Windows\Start Menu\Programs,僅在Windows上可用, |
| AllUsersStartMenuProgramsPath | 包含所有用戶開始選單中各子項的檔案夾的路徑,例如,C:\ProgramData\Microsoft\Windows\Start Menu\Programs,僅在Windows上可用, |
可以通過呼叫installer::value()來決議變數, 如果嵌入在“ @”中,它們也可以是作為的字串的一部分,并作為引數傳遞給安裝操作:
if (installer.value("os") === "win") {
component.addOperation("CreateShortcut", "@TargetDir@/MyApp.exe", "@StartMenuDir@/MyApp.lnk");
}
例如,Windows上的應用程式目錄:
| 作業系統 (Windows) | Qt Installer Framework | 變數 | 示例路徑 |
|---|---|---|---|
| 32bit | 32bit | ApplicationsDir | C:\Program Files |
| ApplicationsDirX86 | C:\Program Files | ||
| ApplicationsDirX64 | C:\Program Files | ||
| 64bit | 32bit | ApplicationsDir | C:\Program Files (x86) |
| ApplicationsDirX86 | C:\Program Files (x86) | ||
| ApplicationsDirX64 | C:\Program Files | ||
| 64bit | ApplicationsDir | C:\Program Files | |
| ApplicationsDirX86 | C:\Program Files (x86) | ||
| ApplicationsDirX64 | C:\Program Files |
原創造福大家,共享改變世界
獻出一片愛心,溫暖作者心靈
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/5178.html
標籤:其他
