ini文件在windows系统中可以存储需要持久保存的配置信息,QT界面中如何实现手动读取参数存放的位置,感兴趣的小伙伴们可以参考一下
ini文件在windows系统中可以存储需要持久保存的配置信息,QT界面中如何实现手动读取参数存放的位置,感兴趣的小伙伴们可以参考一下
ini文件在windows系统中可以存储需要持久保存的配置信息,QT界面中如何实现手动读取参数存放的位置,感兴趣的小伙伴们可以参考一下
如图1所示,我们需要在QT界面中实现手动读取参数存放的位置,那么我们该如何做呢?

方法:读取ini格式的配置文件,实现路径的写入与读取。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
//初始化一个.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();} |
|
1
2
3
4
5
6
7
8
9
10
11
|
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:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
//设置相机标定文件路径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:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
//设置手眼标定时的机械臂运动轨迹路径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内容请搜索米米素材网以前的文章或继续浏览下面的相关文章希望大家以后多多支持米米素材网!
原文链接:https://blog.csdn.net/Yong_Qi2015/article/details/87916091
发表评论