文章目录
- 一、实现一个图片查看软件
一、实现一个图片查看软件
需要实现的功能:
- 打开目录选择图片
- 显示图片的名字
- 显示图片
在以上功能的基础上进行优化,需要解决如下问题:
如何记住上次打开的路径?
将路径保存到配置文件中,当打开图片前,会首先读取配置文件
如何指定默认的路径为文档/图片?
QStandardPaths::PicturesLocation图片如何自适应显示?
缩放到label的大小pix->scaled(ui->label_image->size(), Qt::KeepAspectRatio); //图像的缩放ui->label_image->setScaledContents(true); //QLabel会缩放其内部的图像或内容,以适应标签的尺寸
用到的Qt控件:
- QLabel:用于显式文本与图片
- QLineEdit:用于输入和编辑当行文本
- QPushButoon:按钮
用到的特殊类:
- QFileDialog:文件选择对话框,用于用户选择文件或目录,也可用来打开或保存文件
首先,在Qt Designer中利用空间与布局进行设计
ch1_7.pro文件:
QT += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += c++17# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0SOURCES += \main.cpp \widget.cppHEADERS += \widget.hFORMS += \widget.ui# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
widget.h:
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>QT_BEGIN_NAMESPACE
namespace Ui {
class Widget;
}
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();void open1();void open2();void open3();private slots:void on_btnOpen_clicked();private:Ui::Widget *ui;
};
#endif // WIDGET_H
widget.cpp:
#include "widget.h"
#include "ui_widget.h"
#include <QFileDialog>
#include <QSettings>
#include <QStandardPaths>
#include <memory>
#include <QDebug>Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);ui->label_image->clear();
}Widget::~Widget()
{delete ui;
}//未优化的图片查看代码
void Widget::open1()
{//选择文件QString fileName = QFileDialog::getOpenFileName(this, "请选择图片", "C:/Users/85733/Desktop/", "图片(*.png *.jpg);;");if(fileName.isEmpty()){return;}//设置图片名称ui->lineEdit_path->setText(fileName);//显示图片ui->label_image->setPixmap(QPixmap(fileName));
}//优化的图片查看代码
void Widget::open2()
{//获取配置文件路径,这里需要手动创建/config/setting.ini配置文件QString config_path = qApp->applicationDirPath() + "/config/setting.ini";//打开配置文件并读取QSettings *pSetIni = new QSettings(config_path, QSettings::IniFormat);QString lastPath = pSetIni->value("/LastPath/path").toString();//如果为空则指定默认路径为图片if(lastPath.isEmpty()){lastPath = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);}//选择文件QString fileName = QFileDialog::getOpenFileName(this, "请选择图片", lastPath, "图片(*.png *.jpg);;");if(fileName.isEmpty()){return;}//设置图片名称ui->lineEdit_path->setText(fileName);//显示图片QPixmap *pix = new QPixmap(fileName);pix->scaled(ui->label_image->size(), Qt::KeepAspectRatio); //图像的缩放ui->label_image->setScaledContents(true); //QLabel会缩放其内部的图像或内容,以适应标签的尺寸ui->label_image->setPixmap(*pix);//记住上次打开的路径,保存到配置文件中-5int end = fileName.lastIndexOf("/");QString _path = fileName.left(end);pSetIni->setValue("/LastPath/path", _path);delete pix;pix = nullptr;delete pSetIni;pSetIni = nullptr;
}//使用智能指针进行优化
void Widget::open3()
{//获取配置文件路径,这里需要手动创建/config/setting.ini配置文件QString config_path = qApp->applicationDirPath() + "/config/setting.ini";//打开配置文件并读取std::unique_ptr<QSettings> pSetIni(new QSettings(config_path, QSettings::IniFormat));// QSettings *pSetIni = new QSettings(config_path, QSettings::IniFormat);QString lastPath = pSetIni->value("/LastPath/path").toString();//如果为空则指定默认路径为图片if(lastPath.isEmpty()){lastPath = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);}//选择文件QString fileName = QFileDialog::getOpenFileName(this, "请选择图片", lastPath, "图片(*.png *.jpg);;");if(fileName.isEmpty()){return;}//设置图片名称ui->lineEdit_path->setText(fileName);//显示图片std::unique_ptr<QPixmap> pix(new QPixmap(fileName));// QPixmap *pix = new QPixmap(fileName);pix->scaled(ui->label_image->size(), Qt::KeepAspectRatio); //图像的缩放ui->label_image->setScaledContents(true); //QLabel会缩放其内部的图像或内容,以适应标签的尺寸ui->label_image->setPixmap(*pix);//记住上次打开的路径,保存到配置文件中-5int end = fileName.lastIndexOf("/");QString _path = fileName.left(end);pSetIni->setValue("/LastPath/path", _path);// delete pix;// pix = nullptr;// delete pSetIni;// pSetIni = nullptr;
}void Widget::on_btnOpen_clicked()
{// open1();// open2();open3();
}
main.cpp:
#include "widget.h"
#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);Widget w;w.show();return a.exec();
}
运行结果: