QT實戰(zhàn)之打開最近圖片功能的實現(xiàn)
一、項目介紹
本文介紹利用Qt和QSettings實現(xiàn)打開最近圖片功能。
二、項目基本配置
新建一個Qt案例,項目名稱為“RecentPhotoTest”,基類選擇“QMainWindow”,取消選中創(chuàng)建UI界面復(fù)選框,完成項目創(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)整尺寸
新建一個imageLabel,用于圖片顯示;新建Open Action和RecentFile Action,將這兩個action與相應(yīng)的槽函數(shù)相連,然后在菜單欄上創(chuàng)建File和Open Recent菜單,用于承接相應(yīng)的action,最后更新RecentActionList及調(diào)整尺寸。
當(dāng)點擊Open菜單時,選擇圖像并在界面中進(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); //居中對齊
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();//獲取鍵對應(yīng)的值
recentFilePaths.removeAll(filePath); //移除filePath
recentFilePaths.prepend(filePath); //在開頭增加filePath
//如果尺寸超過最大尺寸,則刪除最后一項
while (recentFilePaths.size() > maxFileNr)
recentFilePaths.removeLast();
settings.setValue("recentPhotos", recentFilePaths);//設(shè)置鍵recentPhotos對應(yīng)的值
updateRecentActionList();
}
最后更新RecentActionList,使得每次打開的圖片都放到RecentActionList中:
//更新recentFileActionList
void MainWindow::updateRecentActionList(){
QSettings settings("Recently", "Recent Photos");
QStringList recentFilePaths = settings.value("recentPhotos").toStringList();//獲取鍵對應(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實戰(zhàn)之打開最近圖片功能的實現(xiàn)的文章就介紹到這了,更多相關(guān)QT打開圖片內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++實現(xiàn)十進(jìn)制數(shù)轉(zhuǎn)為其它進(jìn)制數(shù)
這篇文章主要為大家詳細(xì)介紹了C++實現(xiàn)十進(jìn)制數(shù)轉(zhuǎn)為其它進(jìn)制數(shù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-04-04
如何用c++表驅(qū)動替換if/else和switch/case語句
本文將介紹使用表驅(qū)動法,替換復(fù)雜的if/else和switch/case語句,想了解詳細(xì)內(nèi)容,請看下文2021-08-08
C語言實現(xiàn)大數(shù)值金額大寫轉(zhuǎn)換的方法詳解
這篇文章主要為大家詳細(xì)介紹了如何利用C語言實現(xiàn)大數(shù)值金額大寫轉(zhuǎn)換的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2023-03-03
C++ 數(shù)據(jù)結(jié)構(gòu)鏈表的實現(xiàn)代碼
這篇文章主要介紹了C++ 數(shù)據(jù)結(jié)構(gòu)鏈表的實現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-01-01

