QT 读ini配置文件

9 篇文章 1 订阅

读取文件

#include "read.h"

total_region total;
A1_region A1;
A2_region A2;
A3_region A3;
A1_Small_area small_area;


string outline_image_path;

CString ini_path = "D://a.ini";


//QString转为const wchar_t*
inline const WCHAR *QStoWCHAR(const QString& qs)
{
	return (const WCHAR *)qs.utf16();
}

// TCHAR * 转为char *
char* TCHARToChar(TCHAR* pTchar)
{
	char* pChar = nullptr;
	int nLen = wcslen(pTchar) + 1;
	pChar = new char[nLen * 2];
	WideCharToMultiByte(CP_ACP, 0, pTchar, nLen, pChar, 2 * nLen, NULL, NULL);
	return pChar;
}

//TCHAR 转换 string
string TCHAR2STRING(TCHAR * STR)
{
	 int iLen = WideCharToMultiByte(CP_ACP, 0,STR, -1, NULL, 0, NULL, NULL);

	 char* chRtn = new char[iLen * sizeof(char)];

	 WideCharToMultiByte(CP_ACP, 0, STR, -1, chRtn, iLen, NULL, NULL);

	string str(chRtn);

	return str;

}

//读取整型ini数据
int getInt(CString lpKeyName, INT nDefault)
{
	int value = GetPrivateProfileInt(TEXT("Detection"), lpKeyName, nDefault, ini_path);
	return value;
}


//读取浮点型ini数据
double getDouble(CString lpKeyName, CString nDefault)
{
	//读取字符型ini数据
	TCHAR value[20];
	GetPrivateProfileString(TEXT("Detection"), lpKeyName, nDefault, value, sizeof(value), ini_path);
	//转为double型
	return strtod(TCHARToChar(value), NULL);
}


void read_ini()
{
	total.model_flag = getInt("model_flag", 0);
	total.filter_flag = getInt("model_flag", 0);

	total.Zoom_factor_x = getDouble("Zoom_factor_x", "1");;
	total.Zoom_factor_y = getDouble("Zoom_factor_y", "1");;


	TCHAR file_path[200];
	GetPrivateProfileString(TEXT("Detection"), CString("configuration_file_path "), CString(""), file_path, sizeof(file_path), ini_path);;
    //修改char[]的值
	strcpy_s(configuration_file_path, TCHARToChar(file_path));
	GetPrivateProfileString(TEXT("Detection"), CString("outline_image_path"), CString(""), file_path, sizeof(file_path), ini_path);
    //修改字符串
	outline_image_path = TCHAR2STRING(file_path);


	#ifdef DEBUG
		cout << configuration_file_path << " * " << outline_image_path << endl;
	#endif
}

参考链接:

1. TCHAR 转换 string

https://blog.csdn.net/wzy198852/article/details/19990567

2. TCHAR与CHAR互转

https://blog.csdn.net/pjf_1806339272/article/details/105967020

3. QString转换为LPCTSTR

https://xbuba.com/questions/51027141

注:二进制文件中不要保存string类型,读取的时候会出现都不完全

注意:.ini文件的格式必须是utf-8

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值