QT配置文件.ini文件的读写操作

QT配置文件.ini文件的读写操作

  • 头文件.h
  • ini文件的写操作
  • ini文件的读操作

头文件.h

#include   //读写ini文件

ini文件(即Initialization file):这种类型的文件中通常存放的是一个程序的初始化信息。ini文件由若干个节(Section)组成,每个Section由若干键(Key)组成,每个Key可以赋相应的值。

ini文件的写操作

// xxx.h
class xxx : public QMainWindow
{
   private:
     const QString iniFileName = "center_paras.ini";// 定义 .ini文件名(可以加上路径)
};

.ini文件打开(新建),并写入

//  xxx.cpp
	QSettings* settings = new QSettings(iniFileName, QSettings::IniFormat);		
	//保存xxx
	QString strKv = QString::number(ui.sbKVVoltage->value()) + "Kv/",
		strFocusCurrent = "FocusCurrent",
		strXCurrent = "XCurrent",
		strYCurrent = "YCurrent";
	settings->setValue(strKv + strFocusCurrent, ui.sbFocusCurrent->value());
	settings->setValue(strKv + strXCurrent, ui.sbXCurrent->value());
	settings->setValue(strKv + strYCurrent, ui.sbYCurrent->value());

	// 保存xxx
	int KvSetValue = (int)ui.sbKVVoltage->value();
	if (KvSetValue == 40)
	{
		settings->setValue("Fliment/fRatio", ui.sbLightCurrent->value());
	}

ini文件的读操作

     // xxx.cpp  
     // 读取.ini配置文件
	QSettings* settings = new QSettings(iniFileName, QSettings::IniFormat);
	QString strKv = QString::number(ui.sbKVVoltage->value()) + "Kv/",
		strFocusCurrent = "FocusCurrent",
		strXCurrent = "XCurrent",
		strYCurrent = "YCurrent",
		strFlimentRatio = "Fliment/fRatio";
		
	double currtFocus, currtX, currtY, currtFliment;
	
	currtFocus = settings->value(strKv + strFocusCurrent).toDouble();
	currtX = settings->value(strKv + strXCurrent).toDouble();
	currtY = settings->value(strKv + strYCurrent).toDouble();
	currtFliment = settings->value(strFlimentRatio).toDouble();

你可能感兴趣的:(Qt,C++,Qt,ini,配置文件,C++)