Qt Installer Framework打包一个简易的Linux程序,实现自定义选择安装路径,并在打包卸载时调用shell脚本等。
先下载网上下载Qt Installer Framework,本文采用的是4.1.4版本
打包目录结构如下:
----config
|__ config.xml
|__ controller.qs
----packages
|____ TEST ————文件夹名称为项目名称
|____ data ————需打包的文件在此目录下
|____ lib
|____ Licenses
|____ script
|__ 其他
|____ meta
|__ installscript.qs
|__ package.xml
|__ targetwidget.ui
|__ zh.qm
|__ zh.ts
----production.sh
文件配置在官网案例中都有,下面是主要文件配置:
config.xml
<Installer>
<Name>TESTName>
<Version>1.0.0.0Version>
<WizardShowPageList>falseWizardShowPageList>
<Title>TESTTitle>
<Publisher>XXXPublisher>
<StartMenuDir>TESTStartMenuDir>
<TargetDir>@HomeDir@/TESTTargetDir>
<ControlScript>controller.qsControlScript>
Installer>
controller.qs
function Controller()
{
// 判断是否是卸载程序
if(installer.isUninstaller()){
installer.uninstallationStarted.connect(this, this.uninstallationStarted);
installer.uninstallationFinished.connect(this, this.uninstallationFinished);
}
// 判断是否是安装程序
if(installer.isInstaller()){
installer.installationStarted.connect(this,this.installationStarted);
}
}
// 卸载开始前需要执行的操作
Controller.prototype.uninstallationStarted = function() {
var dir = installer.value("TargetDir");
installer.execute(dir + "/script/uninstall.sh"); // 可以自定义一些卸载的shell脚本
}
// 当卸载完成时,触发
Controller.prototype.uninstallationFinished = function() {
}
// 开始安装前执行的操作
Controller.prototype.installationStarted = function() {
//installer.execute("touch","/home/arteryflow/aa/bb.txt");
}
meta/installscript.qs
var targetDirectoryPage = "/home/xxx/TEST";
function Component()
{
// constructor
component.loaded.connect(this, Component.prototype.installerLoaded);
if (!installer.addWizardPage(component, "Page", QInstaller.TargetDirectory))
console.log("Could not add the dynamic page.");
}
Component.prototype.isDefault = function()
{
// select the component by default
return true;
}
Component.prototype.createOperations = function()
{
try {
// call the base create operations function
component.createOperations();
//添加桌面快捷方式
var exec="Exec=sh "+"@TargetDir@/TEST.sh\n";
var icon="Icon="+"@TargetDir@/logo.png\n";
var type="Type=Application\n";
var name="Name=TEST\n";
var version="Version=\n";
var categories="Categories=Application;Development\n";
var terminal="Terminal=false\n";
var desktop="/usr/share/applications/TEST.desktop";
var comment=name+exec+icon+version+terminal+type+categories;
// 以管理员身份进行创建
component.addElevatedOperation("CreateDesktopEntry",desktop,comment);
} catch (e) {
console.log(e);
}
}
Component.prototype.loaded = function ()
{
var page = gui.pageByObjectName("DynamicPage");
if (page != null) {
console.log("Connecting the dynamic page entered signal.");
page.entered.connect(Component.prototype.dynamicPageEntered);
}
}
Component.prototype.dynamicPageEntered = function ()
{
var pageWidget = gui.pageWidgetByObjectName("DynamicPage");
if (pageWidget != null) {
console.log("Setting the widgets label text.")
pageWidget.m_pageLabel.text = "This is a dynamically created page.";
}
}
// 实用函数,类似于 QString QDir::toNativeSeparators()
var Dir = new function () {
this.toNativeSparator = function (path) {
if (installer.value("os") == "win")
return path.replace(/\//g, '\\');
return path;
}
};
// 加载组件后立即调用
Component.prototype.installerLoaded = function()
{
installer.setDefaultPageVisible(QInstaller.TargetDirectory, false);
installer.setDefaultPageVisible(QInstaller.ComponentSelection, false);
installer.setDefaultPageVisible(QInstaller.LicenseCheck, false);
installer.addWizardPage(component, "TargetWidget", QInstaller.TargetDirectory);
targetDirectoryPage = gui.pageWidgetByObjectName("DynamicTargetWidget");
targetDirectoryPage.windowTitle = qsTranslate("installscript", "选择安装目录");
targetDirectoryPage.description.setText( qsTranslate("installscript", "请选择程序的安装位置:"));
targetDirectoryPage.targetDirectory.textChanged.connect(this, this.targetDirectoryChanged);
targetDirectoryPage.targetDirectory.setText(Dir.toNativeSparator(installer.value("TargetDir")));
targetDirectoryPage.targetChooser.released.connect(this, this.targetChooserClicked);
gui.pageById(QInstaller.ComponentSelection).entered.connect(this, this.componentSelectionPageEntered);
}
// 当点击选择安装位置按钮时调用
Component.prototype.targetChooserClicked = function()
{
var dir = QFileDialog.getExistingDirectory("", targetDirectoryPage.targetDirectory.text);
if (dir != "") {
targetDirectoryPage.targetDirectory.setText(Dir.toNativeSparator(dir));
}
}
// 当安装位置发生改变时调用
Component.prototype.targetDirectoryChanged = function()
{
var dir = targetDirectoryPage.targetDirectory.text;
if (installer.fileExists(dir) && installer.fileExists(dir + "/TEST")) {
targetDirectoryPage.warning.setText( qsTranslate("installscript", "检测到程序已安装,继续将会被覆盖。
"));
} else {
targetDirectoryPage.warning.setText("");
}
installer.setValue("TargetDir", dir);
}
// 当进入【选择组件】页面时调用
Component.prototype.componentSelectionPageEntered = function()
{
var dir = installer.value("TargetDir");
if (installer.fileExists(dir) && installer.fileExists(dir + "/maintenancetool.exe")) {
installer.execute(dir + "/maintenancetool.exe", "--script=" + dir + "/script/uninstallscript.qs");
}
}
meta/package.xml
<Package>
<ReleaseDate>2023-07-19ReleaseDate>
<Version>1.1.0.0Version>
<Default>trueDefault>
<Script>installscript.qsScript>
<UserInterfaces>
<UserInterface>targetwidget.uiUserInterface>
UserInterfaces>
<Translations>
<Translation>zh.qmTranslation>
Translations>
Package>
production.sh
#打包指令
/home/xxx/QtIFW-4.4.1/bin/binarycreator -c config/config.xml -p packages TEST_Install --verbose --af tar.gz
打包成功后,在同级目录下生成安装包