// 1. QMetaObject::connectSlotsByName
void Widget::on_btnOpen_clicked()
{
QString conf_path = qApp -> applicationDirPath() + "/conf/settings.ini";
std::unique_ptr<QSettings> iniSet(new QSettings(conf_path, QSettings::IniFormat)); //2. 操作ini
QString lastPath = iniSet -> value("/LastPath/path/").toString(); //读ini
if(lastPath.isEmpty())
{
lastPath = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation); //3. 标准设备,默认路径
}
QString fileName = QFileDialog::getOpenFileName(this, "请选择", lastPath, "图片(*.png *.img *.gif);(*.*);");
if(fileName.isEmpty() || fileName.isNull()) return;
int end =fileName.lastIndexOf("/");
QString _path = fileName.left(end);
iniSet -> setValue("/LastPath/path/", _path); //写ini, 保存上次路径
std::unique_ptr<QPixmap> pix(new QPixmap(fileName));
pix -> scaled( ui->label_img->size(), Qt::KeepAspectRatio); //4. 缩放|抗锯齿
ui -> label_img ->setScaledContents(true); //自适应显示
ui -> lineEdit_path -> setText(fileName);
ui -> label_img -> setPixmap( *pix );
}
标签:img,配置文件,fileName,ui,lastPath,path,QString
From: https://www.cnblogs.com/cs217113/p/18182923