#include "mainwindow.h"
#include<QFileDialog>
#include<QMessageBox>
#include<QSettings>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
imageLabel = new QLabel;
setCentralWidget(imageLabel);//设置中心部件
//Open Action
openAction = new QAction(tr("&Open..."), this);//open
openAction->setShortcuts(QKeySequence::Open); //设置快捷键
connect(openAction, &QAction::triggered, this, [=]()
{
QString filePath = QFileDialog::getOpenFileName(
this, tr("Open File"), "",
tr("Images (*.png *.xpm *.jpg *.gif)"));
if (!filePath.isEmpty())
loadFile(filePath);
});
//recentFile Action
QAction* recentFileAction = nullptr;
for(auto i = 0; i < maxFileNr; ++i){
recentFileAction = new QAction(this);
recentFileAction->setVisible(false);
connect(recentFileAction, &QAction::triggered, this, [=]()
{
loadFile(recentFileAction->data().toString());
});
recentFileActionList.append(recentFileAction);
}
// create menus
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(openAction);
recentFilesMenu = fileMenu->addMenu(tr("Open Recent"));
for(auto i = 0; i < maxFileNr; ++i)
recentFilesMenu->addAction(recentFileActionList.at(i));
updateRecentActionList();
resize(350, 250);//调整尺寸
}
MainWindow::~MainWindow()
{
}
//加载图片
void MainWindow::loadFile(const QString &filePath){
QFile file(filePath);
//如果不能打开
if (!file.open(QFile::ReadOnly)) {
QMessageBox::warning(this, tr("Recent Photos"),
tr("This file could not be found:\n%1.")
.arg(filePath));
return;
}
QPixmap pMap(filePath);
//如果图片为空
if (pMap.isNull()) {
QMessageBox::information(this, tr("Recent Photos"),
tr("Cannot load:\n%1.")
.arg(filePath));
return;
}
imageLabel->setPixmap(pMap); //显示图像
imageLabel->setAlignment(Qt::AlignCenter); //居中对齐
adjustForCurrentFile(filePath);
}
//调整当前文件(使得每次新打开的文件都在最上方)
void MainWindow::adjustForCurrentFile(const QString &filePath){
currentFilePath = filePath;
setWindowFilePath(currentFilePath);
QSettings settings("Recently", "Recent Photos1");
QStringList recentFilePaths = settings.value("recentPhotos").toStringList();//获取键对应的值
recentFilePaths.removeAll(filePath); //移除filePath
recentFilePaths.prepend(filePath); //在开头增加filePath
//如果尺寸超过最大尺寸,则删除最后一项
while (recentFilePaths.size() > maxFileNr)
recentFilePaths.removeLast();
settings.setValue("recentPhotos", recentFilePaths);//设置键recentPhotos对应的值
updateRecentActionList();
}
//更新recentFileActionList
void MainWindow::updateRecentActionList(){
QSettings settings("Recently", "Recent Photos1");
QStringList recentFilePaths = settings.value("recentPhotos").toStringList();//获取键对应的值
auto itEnd = 0;
if(recentFilePaths.size() <= maxFileNr)
itEnd = recentFilePaths.size();
else
itEnd = maxFileNr;
for (auto i = 0; i < itEnd; ++i) {
QString strippedName = QFileInfo(recentFilePaths.at(i)).fileName();//返回文件名(不包含路径)
recentFileActionList.at(i)->setText(strippedName); //描述性文本
recentFileActionList.at(i)->setData(recentFilePaths.at(i)); //数据
recentFileActionList.at(i)->setVisible(true);
}
for (auto i = itEnd; i < maxFileNr; ++i)
recentFileActionList.at(i)->setVisible(false);
}
Qt实战案例之利用Qt实现打开最近图片功能
需积分: 5 137 浏览量
2022-06-13
21:14:14
上传
评论
收藏 5KB ZIP 举报
wendy_ya
- 粉丝: 3w+
- 资源: 202
最新资源
- 基于opencv的dnn模块实现Yolo-Fastest的目标检测python源码+模型+说明(高分项目).zip
- 使用Python调用微信本地ocr服务.zip
- 【精品推荐】人工智能在医疗中的应用.pptx
- 【精品推荐】电子医疗仪器人机接口-(1).ppt
- 【精品推荐】电子医疗仪器人机接口.ppt
- ubuntu镜像ubuntu镜像01
- 基于paddle搭建神经网络实现5种水果识别分类python源码+数据集(高分毕设).zip
- 【精品推荐】电子商务网店类型介绍.ppt
- 基于paddle搭建神经网络实现水果识别分类python源码+数据集(高分项目).zip
- 三菱plc编程口通信学习笔记.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0