QT實(shí)戰(zhàn)之打開最近圖片功能的實(shí)現(xiàn)
一、項(xiàng)目介紹
本文介紹利用Qt和QSettings實(shí)現(xiàn)打開最近圖片功能。
二、項(xiàng)目基本配置
新建一個(gè)Qt案例,項(xiàng)目名稱為“RecentPhotoTest”,基類選擇“QMainWindow”,取消選中創(chuàng)建UI界面復(fù)選框,完成項(xiàng)目創(chuàng)建。
三、UI界面設(shè)置
無UI界面
四、主程序?qū)崿F(xiàn)
4.1 mainwindow.h頭文件
頭文件中需要聲明若干槽函數(shù)、變量和相應(yīng)函數(shù):
private:
QMenu* fileMenu;
QMenu* recentFilesMenu;
QAction* openAction;
QList<QAction*> recentFileActionList;
const int maxFileNr=5;
QString currentFilePath;
QLabel *imageLabel;
void loadFile(const QString& filePath);
void adjustForCurrentFile(const QString& filePath);
void updateRecentActionList();
4.2 mainwindow.cpp源文件
需要在構(gòu)造函數(shù)中添加如下代碼:
imageLabel = new QLabel;
setCentralWidget(imageLabel);//設(shè)置中心部件
//Open Action
openAction = new QAction(tr("&Open..."), this);//open
openAction->setShortcuts(QKeySequence::Open); //設(shè)置快捷鍵
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);//調(diào)整尺寸
新建一個(gè)imageLabel,用于圖片顯示;新建Open Action和RecentFile Action,將這兩個(gè)action與相應(yīng)的槽函數(shù)相連,然后在菜單欄上創(chuàng)建File和Open Recent菜單,用于承接相應(yīng)的action,最后更新RecentActionList及調(diào)整尺寸。
當(dāng)點(diǎn)擊Open菜單時(shí),選擇圖像并在界面中進(jìn)行顯示:
//加載圖片
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); //居中對(duì)齊
adjustForCurrentFile(filePath);
}
調(diào)整菜單中最近文件的位置,使得每次新打開的文件都在RecentFile Action菜單欄的最上方:
//調(diào)整當(dāng)前文件(使得每次新打開的文件都在最上方)
void MainWindow::adjustForCurrentFile(const QString &filePath){
currentFilePath = filePath;
setWindowFilePath(currentFilePath);
QSettings settings("Recently", "Recent Photos");
QStringList recentFilePaths = settings.value("recentPhotos").toStringList();//獲取鍵對(duì)應(yīng)的值
recentFilePaths.removeAll(filePath); //移除filePath
recentFilePaths.prepend(filePath); //在開頭增加filePath
//如果尺寸超過最大尺寸,則刪除最后一項(xiàng)
while (recentFilePaths.size() > maxFileNr)
recentFilePaths.removeLast();
settings.setValue("recentPhotos", recentFilePaths);//設(shè)置鍵recentPhotos對(duì)應(yīng)的值
updateRecentActionList();
}
最后更新RecentActionList,使得每次打開的圖片都放到RecentActionList中:
//更新recentFileActionList
void MainWindow::updateRecentActionList(){
QSettings settings("Recently", "Recent Photos");
QStringList recentFilePaths = settings.value("recentPhotos").toStringList();//獲取鍵對(duì)應(yīng)的值
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)); //數(shù)據(jù)
recentFileActionList.at(i)->setVisible(true);
}
for (auto i = itEnd; i < maxFileNr; ++i)
recentFileActionList.at(i)->setVisible(false);
}
五、效果演示
完整效果如下:

到此這篇關(guān)于QT實(shí)戰(zhàn)之打開最近圖片功能的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)QT打開圖片內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++?LeetCode1769移動(dòng)所有球到每個(gè)盒子最小操作數(shù)示例
這篇文章主要為大家介紹了C++?LeetCode1769移動(dòng)所有球到每個(gè)盒子所需最小操作數(shù)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
C++ using namespace std 用法深入解析
以下是對(duì)C++中using namespace std的用法進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下2013-07-07
C語言光標(biāo)旋轉(zhuǎn)與倒計(jì)時(shí)功能實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了C語言實(shí)現(xiàn)光標(biāo)旋轉(zhuǎn)與倒計(jì)時(shí)功能的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2021-11-11
Linux C 獲取進(jìn)程退出值的實(shí)現(xiàn)代碼
本篇文章是對(duì)在Linux下使用c語言獲取進(jìn)程退出值的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C語言實(shí)現(xiàn)bmp圖像對(duì)比度擴(kuò)展

