• Qt-qrc资源文件-rcc打包-程序调用-ZIP压缩和解压-安装程序制作参考



    将程序的资源文件打包在程序的应用程序内,能够使程序保持更大的稳定性,本文围绕qrc资源文件的编辑,rcc文件打包,最后示范如何在exe或者dll上调用。

    1.qrc文件编辑

    qrc文件本身内容是个xml文件,如下所示:

    <RCC>
        <qresource prefix="/QtInstallVS2017">
            <file>Resource/Install20220821.zipfile>
        qresource>
    RCC>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    实际上,它有许多规则,如何简单使用?可以用qrceditor.exe这个软件来编辑。下载路径。

    2.将qrc文件转位rcc

    将Qt Bin所在路径临时添加入系统PATH目录,脚本如下所示:

    Set PATH=%PATH%;E:\OpenSource\QT\vs2017Qt\Qt5_9_2_VS2017_Static_64\bin
    
    • 1

    将qrc文件转为二进制文件,代码如下所示:

    rcc -binary QtInstallVS2017A.qrc -o QtInstallVS2017A.rcc
    rcc -binary QtInstallVS2017.qrc -o QtInstallVS2017.rcc
    
    • 1
    • 2

    3.资源使用

    打包之后的资源如何使用,需要在程序中注册。如果与项目默认文件名一致,会打包到exe文件,且不需要注册。

    bool rlt=QResource::registerResource("QtInstallVS2017A.rcc");
    if (rlt) {
    		QFile file(":/File/Install20220821.zip"); //":/前缀/文件名"
    		if (file.open(QIODevice::ReadOnly)) {
    			int k = 0;
    		}
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    注意:资源文件放在项目所在目录,而不是解决方案所在目录。

    4.ZIP压缩和解压

    在Qt中使用内置的压缩和解压模块,需要在模块中加载gui-private,如下图所示:
    在这里插入图片描述

    4.1.解压

    然后非常简单就能解压文件,如下所示:

    #include "QtGui/private/qzipreader_p.h"
    #include "QtGui/private/qzipwriter_p.h"
    
    //解压文件到文件夹
    QZipReader reader(srcZipFilename);
    reader.extractAll(dstFolder);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    4.2.压缩

    将文件夹压缩为zip文件。代码如下所示:

    #include "QtGui/private/qzipreader_p.h"
    #include "QtGui/private/qzipwriter_p.h"
    #include 
    #include 
    #include 
    #include 
    #include 
    
    static bool QZipWriterEx(QZipWriter *writer, QString tmpPath, QString basePath)
    {
    	QDir dir(tmpPath);
    	QFileInfoList fil = dir.entryInfoList();
    	foreach(QFileInfo info, fil)
    	{
    		if (info.fileName() == "." || info.fileName() == "..")
    			continue;
    		if (info.isFile())
    		{
    			QFile upfile(info.filePath());
    			upfile.open(QIODevice::ReadOnly);
    			QString fileName = info.filePath().mid(basePath.size() + 1, info.filePath().size());
    			writer->addFile(fileName, upfile.readAll());
    			//qDebug() << fileName << tmpPath << basePath;
    			upfile.close();
    		}
    		else if (info.isDir())
    		{
    			QZipWriterEx(writer, info.filePath(), basePath);
    		}
    	}
    	return true;
    }
    
    //SavePath:压缩到的文件名;dirPath:需要压缩的目录。
    static bool zipDir(const QString& srcFolder, QString dstZipFilename)
    {
    	bool  ret;
    	QZipWriter *writer = new QZipWriter(dstZipFilename);
    	if (QZipWriterEx(writer, srcFolder, srcFolder))//dirPath为了方便回调,所以传了两次。
    		ret = true;
    	else
    		ret = false;
    
    	writer->close();
    	delete writer;
    	return  ret;
    }
    
    static void Test() {
    	QString srcFolder = QString::fromLocal8Bit("C://Users//ajz//Desktop//新建文件夹 (2)");
    	QString dstZipFile = QString::fromLocal8Bit("C://Users//ajz//Desktop//test.zip");
    	zipDir(srcFolder, dstZipFile);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    5.错误

    A.解决fatal error C1060: 编译器的堆空间不足(详解)
    常见的是由于定义了大量静态全局数组,编译时导致编译器占用内存超出该编译器程序可访问范围。打开xxx.vcxproj工程文件,搜索“Globals”在文件中找到

    <PreferredToolArchitecture>x64PreferredToolArchitecture>
    
    • 1

    这句话的意思就是,指定VS使用64位编译器cl.exe来进行编译,这样编译时,可访问内存将达到4GB以上,便不会报错了。

    6.作者答疑

    如有疑问,敬请留言。

  • 相关阅读:
    【Springboot】基于注解式开发Springboot-Vue3整合Mybatis-plus实现分页查询
    python3如何安装各类库的小总结
    【排序算法】详解直接插入排序和希尔排序原理及其性能分析
    Zemax操作37--更换玻璃和非球面
    Dubbo前后端分离监控中心搭建
    微信小程序使用 npm 包,举例图文详解
    Mysql中获取所有表名以及表名带时间字符串使用BetweenAnd筛选区间范围
    svg图片代码data:image/svg+xml转png图片方法
    【AI相关】模型相关技术名词
    MySQL 8.0.18 重新初始化
  • 原文地址:https://blog.csdn.net/m0_67316550/article/details/126448116