VS+Qt 如何设置软件版本版权,获取软件版本并显示版本号?

VS+Qt 如何设置软件版本版权,获取软件版本并显示版本号?


前言

VS版本:VS2019
QT版本:Qt5.12.3(msvc2017_64)


一、效果展示

1.增加版本信息前 VS 增加版本信息后👇👇👇
在这里插入图片描述
2.获取版本信息前 VS 获取版本信息后👇👇👇
在这里插入图片描述

二、版本、版权信息设置

  • 在VS中,右键项目名称->添加->资源;
    在这里插入图片描述
  • 在弹出的窗口中,选择Version->新建;
    在这里插入图片描述
  • 在弹出的版本资源管理器中可以对版本号、公司名称、版权、软件名称等信息进行设置;
    在这里插入图片描述
  • 保存后退出资源管理器,重新生成可执行文件,可执行文件(.exe)即可显示版本、版权等信息。
    在这里插入图片描述

三、版本信息获取与显示

  • 需要增加版本资源获取文件VersionUpdate.h

VersionUpdate.h文件

#pragma comment(lib, "version.lib")

#include <QObject>
#include <windows.h>
#include <tchar.h>

class VersionUpdate
{
public:
	static QString VersionUpdate::GetVersion()
	{
		int nMaxPathName = 4096; // Max length of file name/path
		TCHAR* pBuffer;
		UINT nItemLength;
		void* pData, * lpBuffer;
		QString sVersion;
		DWORD dwInfoSize, dwHandle;
		VS_FIXEDFILEINFO* pFileInfo;

		// Get the file path and name
		pBuffer = new TCHAR[nMaxPathName];
		GetModuleFileName(NULL, pBuffer, nMaxPathName - 1);

		// Get File Version Info size
		dwInfoSize = GetFileVersionInfoSize(pBuffer, &dwHandle);
		if (dwInfoSize > 0)
		{
			pData = new TCHAR[dwInfoSize];
			if (GetFileVersionInfo(pBuffer, dwHandle, dwInfoSize, pData))
				if (VerQueryValue(pData, _T("\\"), &lpBuffer, &nItemLength))
				{
					pFileInfo = (VS_FIXEDFILEINFO*)lpBuffer;
					sVersion = QString("%1.%2.%3.%4")
						.arg(pFileInfo->dwProductVersionMS >> 16)
						.arg(pFileInfo->dwProductVersionMS & 0xFFFF)
						.arg(pFileInfo->dwProductVersionLS >> 16)
						.arg(pFileInfo->dwProductVersionLS & 0xFFFF);
					// Calculate the product version as a number, you can delete the next statement if you don't need it.
					DWORD dwProductVersion = (pFileInfo->dwProductVersionMS >> 16) * 1000 +
						(pFileInfo->dwProductVersionMS & 0xFFFF) * 100 +
						(pFileInfo->dwProductVersionLS >> 16) * 10 +
						(pFileInfo->dwProductVersionLS & 0xFFFF) * 1;

				}
			// Delete the data buffer
			delete[] pData;
		}
		// Get rid of the allocated string buffer
		delete[] pBuffer;
		return sVersion;
	}
private:
};
  • main.cpp中加入这几句代码;
#include "ApplicationVersionUpdate.h"
#include <QtWidgets/QApplication>

//版本号获取
#include "VersionUpdate.h"
#define  AppTitle (QString)("ApplicationVersionUpdate_V"+VersionUpdate::GetVersion()) 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    ApplicationVersionUpdate w;
    //设置状态栏显示内容
    w.setWindowTitle(AppTitle);   
    w.show();
    return a.exec();
}
  • 运行项目,窗框状态栏则显示配置的版本信息。
    在这里插入图片描述

四、工程源码

  • 7
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jack1009HF

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值