QT自定义安装包

  有时候我们需要把整个软件需要的东西打包在一个文件内,这样就可以一键安装需要的东西,方便用户操作,本文用的QT版本是qt6.2。

1 解压压缩包

    1.1 zlib

    先下载zlib,链接如下:

http://www.zlib.net/

图片

    下载后编译一下

图片

    1.2 quazip

    下载quazip,链接如下

https://sourceforge.net/projects/quazip/files/quazip/0.7.3/

    下载后,点击下面的pro文件开始编译

图片

    1.3 使用方法

    主要是将压缩包放到资源文件中,然后程序解压包一个输出目录,解压的过程放到一个线程中去。

    下面是主要解压方法。

bool AUnZipThread::UnZipFile(QString infile, QString outpath)
{
    QuaZip zipInFile(infile);
    if (!zipInFile.open(QuaZip::mdUnzip)) {
        qCritical()<<"解压失败";
        return false;
    }

    //
    bool ret = zipInFile.goToFirstFile();
    while(ret) {
        ret = SubUnZipFile(zipInFile.getZipName(), zipInFile.getCurrentFileName(), outpath);
        if (ret != true) {
            qCritical()<<"解压失败";
            return false;
        }
        ret = zipInFile.goToNextFile();
    }

    //
    return true;
}

bool AUnZipThread::SubUnZipFile(QString infile, QString infilepath, QString outpath)
{
    QuaZipFile inZipFile(infile, infilepath);
    if (!inZipFile.open(QIODevice::ReadOnly)) {
        return false;
    }

    //
    QByteArray data = inZipFile.readAll();
    QString outPath = outpath +'/'+ infilepath;
    if (outPath.endsWith("/") == true) {
        QDir appDir(outPath);
        if (appDir.exists() != true) {
            appDir.mkpath(outPath);
        }
        return(true);
    }

    //
    QFile   outfile(outPath);
    qDebug()<<"正在提取文件"<<outPath;
    if (!outfile.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
        return false;
    }
    outfile.write(data);
    inZipFile.close();
    outfile.close();

    //
    return true;
}

2 安装驱动

    安装驱动本质就是调用其他程序,主要代码如下。‍

    QString program = DriveFile;
    QStringList arguments;
    QProcess myProcess;
    myProcess.start(program,arguments);
    myProcess.waitForFinished();//等待其他程序结束

3 其他功能

    3.1 创建桌面和开始菜单快捷方式

    主要代码如下:

    //建立桌面快捷方式
    QString strAppPath = "C:/Windows/System32/notepad.exe";
    QString strDesktopLink = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation) + "/";
    strDesktopLink += "notepad.lnk";
    QFile fApp(strAppPath);
    fApp.link(strDesktopLink);

    //建立开始菜单快捷方式
    QString strMenuLink = QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation) + "/";
    strMenuLink += "notepad/";
    QDir pathDir;
    pathDir.mkpath(strMenuLink);
    strMenuLink += "notepad.lnk";
    fApp.link(strMenuLink);

    3.2 添加环境变量

    QSettings setting("HKEY_CURRENT_USER\\Environment", QSettings::NativeFormat);
    if (setting.value("Path", "").toString().indexOf(OutPath) < 0) {
        setting.setValue("Path", setting.value("Path", "").toString()+OutPath+";");
    }

    3.3 静态编译

    Qt需要静态编译,不然生成的exe还是需要一堆dll,静态编译可以参考下面这篇文章。

https://blog.csdn.net/zhangpeterx/article/details/86529231

4 界面展示

图片

图片

图片

图片

    测试源码放到csdn上面了,有需要的同学可以下载。

https://download.csdn.net/download/qq_40732350/64669241

猜你喜欢

转载自blog.csdn.net/qq_40732350/article/details/122041332