欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)文章

最新評(píng)論