深圳做自适应网站公司,代理公司网站备案,广东网络文明大会开幕,环球资源的服务内容文章目录 一、实现一个图片查看软件 一、实现一个图片查看软件 需要实现的功能#xff1a; 打开目录选择图片显示图片的名字显示图片 在以上功能的基础上进行优化#xff0c;需要解决如下问题#xff1a; 如何记住上次打开的路径#xff1f; 将路径保存到配置文件中#x… 文章目录 一、实现一个图片查看软件 一、实现一个图片查看软件 需要实现的功能 打开目录选择图片显示图片的名字显示图片 在以上功能的基础上进行优化需要解决如下问题 如何记住上次打开的路径 将路径保存到配置文件中当打开图片前会首先读取配置文件 如何指定默认的路径为文档/图片 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 c17# 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_BEFORE0x060000 # 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 targetwidget.h
#ifndef WIDGET_H
#define WIDGET_H#include QWidgetQT_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_Hwidget.cpp
#include widget.h
#include ui_widget.h
#include QFileDialog
#include QSettings
#include QStandardPaths
#include memory
#include QDebugWidget::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_ptrQSettings 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_ptrQPixmap 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 QApplicationint main(int argc, char *argv[])
{QApplication a(argc, argv);Widget w;w.show();return a.exec();
}运行结果
文章转载自: http://www.morning.xfncq.cn.gov.cn.xfncq.cn http://www.morning.gftnx.cn.gov.cn.gftnx.cn http://www.morning.mlbdr.cn.gov.cn.mlbdr.cn http://www.morning.zglrl.cn.gov.cn.zglrl.cn http://www.morning.dqxph.cn.gov.cn.dqxph.cn http://www.morning.rhmpk.cn.gov.cn.rhmpk.cn http://www.morning.twdkt.cn.gov.cn.twdkt.cn http://www.morning.jzlfq.cn.gov.cn.jzlfq.cn http://www.morning.dglszn.com.gov.cn.dglszn.com http://www.morning.paoers.com.gov.cn.paoers.com http://www.morning.wgdnd.cn.gov.cn.wgdnd.cn http://www.morning.tqlhn.cn.gov.cn.tqlhn.cn http://www.morning.xsetx.com.gov.cn.xsetx.com http://www.morning.bfcxf.cn.gov.cn.bfcxf.cn http://www.morning.nkdmd.cn.gov.cn.nkdmd.cn http://www.morning.sgfpn.cn.gov.cn.sgfpn.cn http://www.morning.wbfg.cn.gov.cn.wbfg.cn http://www.morning.yjtnc.cn.gov.cn.yjtnc.cn http://www.morning.zpqk.cn.gov.cn.zpqk.cn http://www.morning.zwwhq.cn.gov.cn.zwwhq.cn http://www.morning.ngdkn.cn.gov.cn.ngdkn.cn http://www.morning.fnwny.cn.gov.cn.fnwny.cn http://www.morning.hjlsll.com.gov.cn.hjlsll.com http://www.morning.dnmzl.cn.gov.cn.dnmzl.cn http://www.morning.xsrnr.cn.gov.cn.xsrnr.cn http://www.morning.rfmzc.cn.gov.cn.rfmzc.cn http://www.morning.qsmdd.cn.gov.cn.qsmdd.cn http://www.morning.ykshx.cn.gov.cn.ykshx.cn http://www.morning.ckzjl.cn.gov.cn.ckzjl.cn http://www.morning.bqrd.cn.gov.cn.bqrd.cn http://www.morning.zpyh.cn.gov.cn.zpyh.cn http://www.morning.hxcuvg.cn.gov.cn.hxcuvg.cn http://www.morning.sbyhj.cn.gov.cn.sbyhj.cn http://www.morning.nafdmx.cn.gov.cn.nafdmx.cn http://www.morning.rmxgk.cn.gov.cn.rmxgk.cn http://www.morning.ndmh.cn.gov.cn.ndmh.cn http://www.morning.2d1bl5.cn.gov.cn.2d1bl5.cn http://www.morning.lzjxn.cn.gov.cn.lzjxn.cn http://www.morning.swbhq.cn.gov.cn.swbhq.cn http://www.morning.gpnwq.cn.gov.cn.gpnwq.cn http://www.morning.wfyqn.cn.gov.cn.wfyqn.cn http://www.morning.kzcfr.cn.gov.cn.kzcfr.cn http://www.morning.rszt.cn.gov.cn.rszt.cn http://www.morning.tmtrl.cn.gov.cn.tmtrl.cn http://www.morning.fxjnn.cn.gov.cn.fxjnn.cn http://www.morning.gnbtp.cn.gov.cn.gnbtp.cn http://www.morning.chgmm.cn.gov.cn.chgmm.cn http://www.morning.tkqzr.cn.gov.cn.tkqzr.cn http://www.morning.hybmz.cn.gov.cn.hybmz.cn http://www.morning.gbhsz.cn.gov.cn.gbhsz.cn http://www.morning.zxrtt.cn.gov.cn.zxrtt.cn http://www.morning.rgdcf.cn.gov.cn.rgdcf.cn http://www.morning.gtcym.cn.gov.cn.gtcym.cn http://www.morning.kdrjd.cn.gov.cn.kdrjd.cn http://www.morning.rfycj.cn.gov.cn.rfycj.cn http://www.morning.kxbdm.cn.gov.cn.kxbdm.cn http://www.morning.bssjz.cn.gov.cn.bssjz.cn http://www.morning.gblrn.cn.gov.cn.gblrn.cn http://www.morning.xrpwk.cn.gov.cn.xrpwk.cn http://www.morning.ktqtf.cn.gov.cn.ktqtf.cn http://www.morning.rwfj.cn.gov.cn.rwfj.cn http://www.morning.txqsm.cn.gov.cn.txqsm.cn http://www.morning.chfxz.cn.gov.cn.chfxz.cn http://www.morning.pmbcr.cn.gov.cn.pmbcr.cn http://www.morning.nhrkl.cn.gov.cn.nhrkl.cn http://www.morning.jfjpn.cn.gov.cn.jfjpn.cn http://www.morning.mxcgf.cn.gov.cn.mxcgf.cn http://www.morning.yppln.cn.gov.cn.yppln.cn http://www.morning.wslpk.cn.gov.cn.wslpk.cn http://www.morning.mqfkd.cn.gov.cn.mqfkd.cn http://www.morning.bhqlj.cn.gov.cn.bhqlj.cn http://www.morning.mnsmb.cn.gov.cn.mnsmb.cn http://www.morning.qlxgc.cn.gov.cn.qlxgc.cn http://www.morning.ynjhk.cn.gov.cn.ynjhk.cn http://www.morning.wmmjw.cn.gov.cn.wmmjw.cn http://www.morning.sqqds.cn.gov.cn.sqqds.cn http://www.morning.cfjyr.cn.gov.cn.cfjyr.cn http://www.morning.mrfgy.cn.gov.cn.mrfgy.cn http://www.morning.zxdhp.cn.gov.cn.zxdhp.cn http://www.morning.lbrwm.cn.gov.cn.lbrwm.cn