QT实战之打开最近图片功能的实现

 更新时间:2022年06月15日 15:54:29   作者:wendy_ya  
这篇文章主要为大家详细介绍了如何利用Qt和QSettings实现打开最近图片功能,文中的示例代码讲解详细,对我们学习QT有一定的帮助,感兴趣的可以了解一下

一、项目介绍

本文介绍利用Qt和QSettings实现打开最近图片功能。

二、项目基本配置

新建一个Qt案例,项目名称为“RecentPhotoTest”,基类选择“QMainWindow”,取消选中创建UI界面复选框,完成项目创建。

三、UI界面设置

无UI界面

四、主程序实现

4.1 mainwindow.h头文件

头文件中需要声明若干槽函数、变量和相应函数:

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源文件

需要在构造函数中添加如下代码:

    imageLabel = new QLabel;
    setCentralWidget(imageLabel);//设置中心部件
    //Open Action
    openAction = new QAction(tr("&Open..."), this);//open
    openAction->setShortcuts(QKeySequence::Open);  //设置快捷键
    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);//调整尺寸

新建一个imageLabel,用于图片显示;新建Open Action和RecentFile Action,将这两个action与相应的槽函数相连,然后在菜单栏上创建File和Open Recent菜单,用于承接相应的action,最后更新RecentActionList及调整尺寸。

当点击Open菜单时,选择图像并在界面中进行显示:

//加载图片
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);
}

调整菜单中最近文件的位置,使得每次新打开的文件都在RecentFile Action菜单栏的最上方:

//调整当前文件(使得每次新打开的文件都在最上方)
void MainWindow::adjustForCurrentFile(const QString &filePath){
    currentFilePath = filePath;
    setWindowFilePath(currentFilePath);


    QSettings settings("Recently", "Recent Photos");
    QStringList recentFilePaths = settings.value("recentPhotos").toStringList();//获取键对应的值
    recentFilePaths.removeAll(filePath);    //移除filePath
    recentFilePaths.prepend(filePath);      //在开头增加filePath
    //如果尺寸超过最大尺寸,则删除最后一项
    while (recentFilePaths.size() > maxFileNr)
        recentFilePaths.removeLast();
    settings.setValue("recentPhotos", recentFilePaths);//设置键recentPhotos对应的值

    updateRecentActionList();
}

最后更新RecentActionList,使得每次打开的图片都放到RecentActionList中:

//更新recentFileActionList
void MainWindow::updateRecentActionList(){
    QSettings settings("Recently", "Recent Photos");
    QStringList recentFilePaths = settings.value("recentPhotos").toStringList();//获取键对应的值

    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)); //数据
        recentFileActionList.at(i)->setVisible(true);
    }

    for (auto i = itEnd; i < maxFileNr; ++i)
        recentFileActionList.at(i)->setVisible(false);
}

五、效果演示

完整效果如下:

到此这篇关于QT实战之打开最近图片功能的实现的文章就介绍到这了,更多相关QT打开图片内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 利用C语言编写“剪刀石头布”小游戏

    利用C语言编写“剪刀石头布”小游戏

    这篇文章主要给大家介绍了关于如何利用C语言编写“剪刀石头布”小游戏的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12
  • C++ LeetCode1769移动所有球到每个盒子最小操作数示例

    C++ LeetCode1769移动所有球到每个盒子最小操作数示例

    这篇文章主要为大家介绍了C++ LeetCode1769移动所有球到每个盒子所需最小操作数示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-12-12
  • C++ using namespace std 用法深入解析

    C++ using namespace std 用法深入解析

    以下是对C++中using namespace std的用法进行了详细的分析介绍,需要的朋友可以过来参考下
    2013-07-07
  • C语言光标旋转与倒计时功能实现示例详解

    C语言光标旋转与倒计时功能实现示例详解

    这篇文章主要为大家介绍了C语言实现光标旋转与倒计时功能的示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步早日升职加薪
    2021-11-11
  • Linux C 获取进程退出值的实现代码

    Linux C 获取进程退出值的实现代码

    本篇文章是对在Linux下使用c语言获取进程退出值的方法进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • c语言中static修饰函数的方法及代码

    c语言中static修饰函数的方法及代码

    在本篇内容里小编给大家分享的是一篇关于c语言中static如何修饰函数的知识点内容,有需要朋友们可以跟着学习下。
    2021-10-10
  • C语言 深入浅出讲解指针的使用

    C语言 深入浅出讲解指针的使用

    指针是C语言中一个非常重要的概念,也是C语言的特色之一。使用指针可以对复杂数据进行处理,能对计算机的内存分配进行控制,在函数调用中使用指针还可以返回多个值
    2022-03-03
  • VC下实现fopen支持中文的方法

    VC下实现fopen支持中文的方法

    这篇文章主要介绍了VC下实现fopen支持中文的方法,需要的朋友可以参考下
    2014-07-07
  • C++中最常用的容器用法与排序实例

    C++中最常用的容器用法与排序实例

    C++ 中容器被定义为:在数据存储上,有一种对象类型,它可以持有其它对象或指向其它对像的指针,这种对象类型就叫做容器,这篇文章主要给大家介绍了关于C++中最常用的容器用法与排序的相关资料,需要的朋友可以参考下
    2021-08-08
  • C语言实现bmp图像对比度扩展

    C语言实现bmp图像对比度扩展

    这篇文章主要为大家详细介绍了C语言实现bmp图像对比度扩展,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-10-10

最新评论