qt自定义软件安装包

4 篇文章 3 订阅

将程序压缩为zip格式后添加到资源文件,安装时使用quazip从资源文件解压到指定目录,并且将解压放在子线程,避免UI卡主。在做安装包时要使用静态编译,要不然安装包还是需要一大堆dll文件。目录结构如下:

主要代码:

#ifndef UNCOMPRESSTHREAD_H
#define UNCOMPRESSTHREAD_H

#include <QObject>
#include <QThread>

class UncompressThread : public QObject
{
    Q_OBJECT
public:
    explicit UncompressThread(QObject *parent = 0);
    ~UncompressThread();

private:
    bool SubFunExtractFile(QString strInFile, QString strOutFile);
    bool SubFunExtractDir(QString strInFile, QString strOutPath);
    bool SubFubExtractZipFile(QString strInFile, QString strInFilePath, QString strOutPath);
    void setRegValue(QString strPath, QString strItemName, QString strItemData);
signals:
    void WorkerThreadFinishSig();
    void SendUncompressInfomationSig(QString mes);
    void SendCurrentFileSig(QString file);
    void SendProgressValueSig(int value);
public slots:
    void uncompressFile(QString file,QString path);
private:
    QThread mThread;
};

#endif // UNCOMPRESSTHREAD_H
#include "uncompressthread.h"
#include "./quazip/JlCompress.h"
#include <QDir>
#include <QStandardPaths>
#include <QDebug>
#include <QSettings>
#include <QCoreApplication>


UncompressThread::UncompressThread(QObject *parent) : QObject(parent)
{
    this->moveToThread(&mThread);
    mThread.start();
}

UncompressThread::~UncompressThread()
{
    mThread.quit();
    mThread.wait();
}

void UncompressThread::uncompressFile(QString file,QString path)
{
    QString m_strAppPath =path;
    //QString strInFileName /*= "D:\\QtCore\\Uncompress\\release\\Qt5.7-static"*/;

    QStringList OutFileList;
    OutFileList<< file;
    qDebug()<<OutFileList;

    for(int i = 0;i < OutFileList.count();i++)
    {

        //SubFunExtractFile(strInFileName, OutFileList.at(i));


        bool bRet = SubFunExtractDir(OutFileList.at(i), m_strAppPath);
        if (bRet != true)
        {
            SendUncompressInfomationSig(QStringLiteral("提取文件错误."));
        }
        SendUncompressInfomationSig(QStringLiteral("提取文件完成."));
    }

    emit WorkerThreadFinishSig();
}


bool UncompressThread::SubFunExtractFile(QString strInFile, QString strOutFile)
{
    QFile infile(strInFile);
    if (!infile.open(QIODevice::ReadOnly))
    {
        return(false);
    }
    QByteArray data = infile.readAll();
    QFile outfile(strOutFile);
    if (!outfile.open(QIODevice::WriteOnly | QIODevice::Append))
    {
        return(false);
    }
    outfile.write(data);
    infile.close();
    outfile.close();
    return(true);
}

bool UncompressThread::SubFunExtractDir(QString strInFile, QString strOutPath)
{
    QuaZip zipInFile(strInFile);
    if (!zipInFile.open(QuaZip::mdUnzip))
    {
        qDebug()<<"zipInFile open failed";
        return(false);
    }

    int nFileCount = zipInFile.getEntriesCount();


    int nFileProgress = 0;
    bool hasFile = zipInFile.goToFirstFile();
    while(hasFile)
    {
        bool bRet = SubFubExtractZipFile(zipInFile.getZipName(), zipInFile.getCurrentFileName(), strOutPath);
        if (bRet != true)
        {
            qDebug()<<"SubFubExtractZipFile failed";
            return(false);
        }
        nFileProgress++;
        emit SendProgressValueSig(100*nFileProgress/nFileCount);
        bRet = zipInFile.goToNextFile();
        if (bRet != true)
            break;
    }
    return(true);
}

bool UncompressThread::SubFubExtractZipFile(QString strInFile, QString strInFilePath, QString strOutPath)
{
    QuaZipFile infile(strInFile, strInFilePath);
    if (!infile.open(QIODevice::ReadOnly))
    {
        return(false);
    }
    QByteArray data = infile.readAll();
    QString outPath = strOutPath +'\\'+ strInFilePath;
    if (outPath.endsWith("/") == true)
    {
        QDir appDir(outPath);
        if (appDir.exists() != true)
        {
            appDir.mkpath(outPath);
        }
        return(true);
    }
    outPath.remove('\\');

    QFile outfile(outPath);
    QString file = outPath.remove(QStandardPaths::standardLocations(QStandardPaths::ConfigLocation)[0]);
    emit SendCurrentFileSig(file);
    SendUncompressInfomationSig(QStringLiteral("正在提取文件")+file);
    if (!outfile.open(QIODevice::WriteOnly | QIODevice::Truncate))
    {
        return(false);
    }
    outfile.write(data);
    infile.close();
    outfile.close();
    return(true);
}

void UncompressThread::setRegValue(QString strPath, QString strItemName, QString strItemData)
{
    QSettings reg(strPath, QSettings::NativeFormat);

    QStringList regValue = reg.value(strItemName).toString().split(";");


    if(regValue.contains(strItemData))
    {
        return;
    }

    if(strItemName == "Path")
    {
        reg.setValue(strItemName, reg.value(strItemName).toString()+";"+strItemData);
    }
    else
    {
        reg.setValue(strItemName, strItemData);
    }
}

 

Qt是一款跨平台的桌面应用程序开发框架,它提供了丰富的功能和严密的文档支持。在Qt中生成exe安装包的过程相对简单。下面我来介绍一下具体的步骤。 首先,你需要使用Qt Creator来创建一个Qt项目。在创建项目时,你可以选择不同的模板来满足你的需求。如果你想生成一个可以安装的exe文件,可以选择"Qt Widgets Application"模板。 接下来,你需要在项目中添加你的代码Qt提供了强大的API库,可以用来创建各种用户界面和实现各种功能。你可以根据自己的需求来编写代码。 完成代码编写后,你需要进行构建和调试。在Qt Creator中,你可以使用带有构建按钮的工具栏来构建项目。如果存在编译错误,你需要修复它们以确保项目能够成功构建。 当项目构建成功后,你就可以开始生成exe安装包了。在Qt Creator的菜单栏中,选择"Build"选项下的"Create Installer"。接着,你会看到一个对话框,可以在其中选择自定义安装包的属性,例如安装路径、附带的文件和图标等等。完成设置后,点击"Create"按钮,Qt Creator会自动生成exe安装包。 最后,你可以将生成的exe安装包分享给其他人。他们可以运行安装包来安装你开发的应用程序。 总结来说,Qt提供了一种简单的方式来生成exe安装包。通过使用Qt Creator,你可以创建一个Qt项目,并编写代码。然后,利用Qt Creator的生成安装包功能,你可以将项目打包成一个exe安装包,以方便其他人使用。希望这个回答对你有帮助。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值