Qt Installer Framework翻译(7-4)

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Qt Installer Framework翻译(7-4)

codeForFamily   2020-01-31 我要评论

组件脚本

对于每个组件,您可以指定一个脚本,来准备要由安装程序执行的操作。脚本格式必须与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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们