Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to read and write ini configuration file in QT

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to read and write ini configuration files in QT". In daily operation, I believe many people have doubts about how to read and write ini configuration files in QT. Xiaobian consulted all kinds of information and sorted out simple and easy operation methods. I hope to help you answer the doubts about "how to read and write ini configuration files in QT"! Next, please follow the small series to learn together!

As shown in Figure 1, we need to manually read the parameter storage location in the QT interface, so how do we do it?

Method: Read the configuration file in ini format, and write and read the path.

Step 1: In the interface constructor, initialize a Config.ini file//initialize a.ini configuration file//qApp is a built-in QT system, you can directly use QString iniFilePath=qApp->applicationDirPath()+"/Config.ini";//if Config.ini does not exist, generate a Config.ini. If it already exists, skip it. 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"); configInitiWrite.endGroup();} Step 2: Define a function 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(); }} Step 3: Set the path

Demo1:

//Set Camera Calibration File Path 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) { //Get folder path + file name _campara_path = dialog.selectedFiles().first(); ui->lineEditCamParaPath->setText(_campara_path); //here is the path name + file name displayed in the lineEdit window saveConfig("calib_data_path","calib_data_path",_campara_path); }}

demo2:

//Set the path of the robot arm motion during hand-eye calibration 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); }}

Since ini files cannot be uploaded in the planet, here is a screenshot in txt form as an attachment, see Figure 2.

At this point, the study of "how to read and write ini configuration files in QT" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report