QT中怎么读写ini配置文件

发布时间:2021-11-26 14:47:10 作者:iii
来源:亿速云 阅读:420

这篇文章主要介绍“QT中怎么读写ini配置文件”,在日常操作中,相信很多人在QT中怎么读写ini配置文件问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”QT中怎么读写ini配置文件”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

如图1所示,我们需要在QT界面中实现手动读取参数存放的位置,那么我们该如何做呢?

QT中怎么读写ini配置文件

方法:读取ini格式的配置文件,实现路径的写入与读取。

第一步:界面构造函数中,初始化一个Config.ini文件

//初始化一个.ini配置文件
 //qApp是QT系统自带的,可以直接使用
QString iniFilePath=qApp->applicationDirPath()+"/Config.ini";
//如果不存在Config.ini,便生成一个Config.ini。如果已经存在了,则略过。
if(!QFile::exists(iniFilePath))
{
     QSettings configIniWrite(iniFilePath,QSettings::IniFormat);
     configIniWrite.beginGroup("calib_data_path");
     configIniWrite.setValue("calib_data_path","FA0180090134.xml");
     configIniWrite.endGroup();
     configIniWrite.beginGroup("robot_pose_file");
     configIniWrite.setValue("robot_pose_file_path","robot_pose_file.txt");
     configIniWrite.endGroup();
}

第二步:定义一个保存Config文件的函数

void saveConfig(const QString& group,const QString& name, const QVariant& var)
{
     QString iniFilePath = qApp->applicationDirPath() + "/Config.ini";
     if (QFile::exists(iniFilePath))
     {
	   QSettings configIniWrite(iniFilePath,QSettings::IniFormat);
	   configIniWrite.beginGroup(group);
	   configIniWrite.setValue(name,var);
           configIniWrite.endGroup();
     }
}

第三步:设置路径

Demo1:

  //设置相机标定文件路径
void CalibrationForm::btnLoadCamParaPath_clicked()
{
	QFileDialog dialog(this,tr("Select calib data file"));
	dialog.setAcceptMode(QFileDialog::AcceptOpen);
	dialog.setFileMode(QFileDialog::ExistingFile);
	static bool firstDialog = true;
	if (firstDialog)
	{
		firstDialog = false;
		const QStringList fileLocations = 
                QStandardPaths::standardLocations(QStandardPaths::DesktopLocation);
		dialog.setDirectory(fileLocations.isEmpty() ?          
                QDir::currentPath():fileLocations.last());
	}
	dialog.setNameFilter(tr("FA0180090134(*.xml)"));
	if (dialog.exec()==QDialog::Accepted)
	{
		//获得文件夹路径+文件名
		_campara_path = dialog.selectedFiles().first();
		ui->lineEditCamParaPath->setText(_campara_path); //此处是在lineEdit窗口显示路径名+文件名
		saveConfig("calib_data_path","calib_data_path",_campara_path);
	}
}

demo2:

 //设置手眼标定时的机械臂运动轨迹路径
void CalibrationForm::btnLoadRobotPara_clicked()
{
	QFileDialog dialog(this,tr("Select robot pose file"));
	dialog.setAcceptMode(QFileDialog::AcceptOpen);
	dialog.setFileMode(QFileDialog::ExistingFile);
	static bool first_Dialog = true;
	if (first_Dialog)
	{
		first_Dialog = false;
		const QStringList fileLocations = 
QStandardPaths::standardLocations(QStandardPaths::DesktopLocation);
		dialog.setDirectory(fileLocations.isEmpty()?
QDir::currentPath():fileLocations.last());
	}
	dialog.setNameFilter(tr("robot_pose_file(*.txt)"));
	if (dialog.exec()==QDialog::Accepted)
	{
		_robot_pose_path = dialog.selectedFiles().first();
		ui->lineEditRobotPath->setText(_robot_pose_path);
		saveConfig("robot_pose_file","robot_pose_file_path",_robot_pose_path);
	}
}

由于ini文件不可在星球中上传,此处用txt形式的截图作为附件,见图2.

QT中怎么读写ini配置文件

到此,关于“QT中怎么读写ini配置文件”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. VC中读写INI文件
  2. QT xml 读写

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

qt ini

上一篇:MDK软件如何安装

下一篇:C#如何实现基于Socket套接字的网络通信封装

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》