一、保存路径到ini文件
① 代码示例
void MainWindow::SavePathSlot() { //为了能记住上次打开的路径 QSettings setting(QCoreApplication::applicationDirPath()+"/config/savePath.ini", QSettings::IniFormat); QString lastPath = setting.value("LastFilePath").toString(); QString path = QFileDialog::getExistingDirectory(this, "请选择文件路径...", lastPath); if (path.isEmpty()) return; //记录路径到QSetting中保存 setting.setValue("LastFilePath", path); if(path.isEmpty()) { return; } }
② 文件展示
二、程序启动,读取ini文件,将历史路径加载到QLineEdit中
① 解析ini文件 方法一
path = QCoreApplication::applicationDirPath()+"/config/savePath.ini"; QFile *file = new QFile(this); file->setFileName(path); if(!file->open(QIODevice::ReadOnly)) { return; } while (!file->atEnd()) { file->readLine(); QString str = file->readLine(); QStringList list = str.split("="); if(!list.isEmpty()) { ui->savePath_LineEdit->setText(list.at(1).trimmed()); } } file->close(); file = nullptr; delete file;