如图1所⽰,我们需要在QT界⾯中实现⼿动读取参数存放的位置,那么我们该如何做呢?
⽅法:读取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\ configIniWrite.endGroup();
configIniWrite.beginGroup(\"robot_pose_file\");
configIniWrite.setValue(\"robot_pose_file_path\ 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\ }}
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\ }}
由于ini⽂件不可在星球中上传,此处⽤txt形式的截图作为附件,见图2.
到此这篇关于QT中如何读写ini配置⽂件的⽂章就介绍到这了,更多相关QT读写ini内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!
因篇幅问题不能全部显示,请点此查看更多更全内容