QT實(shí)戰(zhàn)之打開(kāi)最近圖片功能的實(shí)現(xiàn)
一、項(xiàng)目介紹
本文介紹利用Qt和QSettings實(shí)現(xiàn)打開(kāi)最近圖片功能。
二、項(xiàng)目基本配置
新建一個(gè)Qt案例,項(xiàng)目名稱(chēng)為“RecentPhotoTest”,基類(lèi)選擇“QMainWindow”,取消選中創(chuàng)建UI界面復(fù)選框,完成項(xiàng)目創(chuàng)建。
三、UI界面設(shè)置
無(wú)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); //如果不能打開(kāi) 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)整菜單中最近文件的位置,使得每次新打開(kāi)的文件都在RecentFile Action菜單欄的最上方:
//調(diào)整當(dāng)前文件(使得每次新打開(kāi)的文件都在最上方) 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); //在開(kāi)頭增加filePath //如果尺寸超過(guò)最大尺寸,則刪除最后一項(xiàng) while (recentFilePaths.size() > maxFileNr) recentFilePaths.removeLast(); settings.setValue("recentPhotos", recentFilePaths);//設(shè)置鍵recentPhotos對(duì)應(yīng)的值 updateRecentActionList(); }
最后更新RecentActionList,使得每次打開(kāi)的圖片都放到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)之打開(kāi)最近圖片功能的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)QT打開(kāi)圖片內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++類(lèi)與對(duì)象的詳細(xì)說(shuō)明
這篇文章主要為大家詳細(xì)介紹了C++的類(lèi)與對(duì)象,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-02-02C++實(shí)現(xiàn)十進(jìn)制數(shù)轉(zhuǎn)為其它進(jìn)制數(shù)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)十進(jìn)制數(shù)轉(zhuǎn)為其它進(jìn)制數(shù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04如何用c++表驅(qū)動(dòng)替換if/else和switch/case語(yǔ)句
本文將介紹使用表驅(qū)動(dòng)法,替換復(fù)雜的if/else和switch/case語(yǔ)句,想了解詳細(xì)內(nèi)容,請(qǐng)看下文2021-08-08C語(yǔ)言實(shí)現(xiàn)大數(shù)值金額大寫(xiě)轉(zhuǎn)換的方法詳解
這篇文章主要為大家詳細(xì)介紹了如何利用C語(yǔ)言實(shí)現(xiàn)大數(shù)值金額大寫(xiě)轉(zhuǎn)換的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2023-03-03C++ 數(shù)據(jù)結(jié)構(gòu)鏈表的實(shí)現(xiàn)代碼
這篇文章主要介紹了C++ 數(shù)據(jù)結(jié)構(gòu)鏈表的實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-01-01