亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

QT實(shí)戰(zhàn)之打開(kāi)最近圖片功能的實(shí)現(xiàn)

 更新時(shí)間:2022年06月15日 15:54:29   作者:wendy_ya  
這篇文章主要為大家詳細(xì)介紹了如何利用Qt和QSettings實(shí)現(xiàn)打開(kāi)最近圖片功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)QT有一定的幫助,感興趣的可以了解一下

一、項(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++中的內(nèi)存對(duì)齊實(shí)例詳解

    C++中的內(nèi)存對(duì)齊實(shí)例詳解

    這篇文章主要介紹了C++中的內(nèi)存對(duì)齊實(shí)例詳解的相關(guān)資料,這里不僅提供實(shí)現(xiàn)方法及代碼還提供了手工制作圖,來(lái)幫助到大家理解這部分知識(shí),需要的朋友可以參考下
    2017-07-07
  • C++類(lèi)與對(duì)象的詳細(xì)說(shuō)明

    C++類(lèi)與對(duì)象的詳細(xì)說(shuō)明

    這篇文章主要為大家詳細(xì)介紹了C++的類(lèi)與對(duì)象,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-02-02
  • C++修煉之拷貝構(gòu)造函數(shù)

    C++修煉之拷貝構(gòu)造函數(shù)

    這篇文章主要內(nèi)容是6個(gè)默認(rèn)成員函數(shù)之一的拷貝構(gòu)造函數(shù)的認(rèn)識(shí)與學(xué)習(xí),讓同學(xué)們充分理解淺拷貝與深拷貝,感興趣的小伙伴可以參考閱讀
    2023-04-04
  • C++實(shí)現(xiàn)十進(jìn)制數(shù)轉(zhuǎn)為其它進(jìn)制數(shù)

    C++實(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ǔ)句

    如何用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-08
  • c++模板自定義數(shù)組

    c++模板自定義數(shù)組

    這篇文章主要介紹了c++模板自定義數(shù)組,通過(guò)制造通用模板,創(chuàng)建自定義的數(shù)組展開(kāi)文章相關(guān)內(nèi)容,具有一的參考價(jià)值,需要的小伙伴可以參考一下
    2022-03-03
  • C++?命名空間?using聲明使用示例詳解

    C++?命名空間?using聲明使用示例詳解

    這篇文章主要為大家介紹了C++?命名空間?using聲明使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • opencv實(shí)現(xiàn)圖形輪廓檢測(cè)

    opencv實(shí)現(xiàn)圖形輪廓檢測(cè)

    這篇文章主要為大家詳細(xì)介紹了opencv實(shí)現(xiàn)圖形輪廓檢測(cè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • C語(yǔ)言實(shí)現(xiàn)大數(shù)值金額大寫(xiě)轉(zhuǎn)換的方法詳解

    C語(yǔ)言實(shí)現(xiàn)大數(shù)值金額大寫(xiě)轉(zhuǎn)換的方法詳解

    這篇文章主要為大家詳細(xì)介紹了如何利用C語(yǔ)言實(shí)現(xiàn)大數(shù)值金額大寫(xiě)轉(zhuǎn)換的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下
    2023-03-03
  • C++  數(shù)據(jù)結(jié)構(gòu)鏈表的實(shí)現(xiàn)代碼

    C++ 數(shù)據(jù)結(jié)構(gòu)鏈表的實(shí)現(xiàn)代碼

    這篇文章主要介紹了C++ 數(shù)據(jù)結(jié)構(gòu)鏈表的實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下
    2017-01-01

最新評(píng)論