当前位置:   article > 正文

Qt实现图片列表显示_qt图片列表

qt图片列表

一、功能说明

选择文件夹,显示该文件夹下所有图片

二、ui控件及函数说明

1、使用QListWidget控件显示图片列表
2、添加一个QPushButton,选择加载文件夹

  • 在.h文件下定义变量
QStringList imagePathList;  //所有图片路径加名字及后缀名
QString dirPath_;   //文件夹路径
  • 1
  • 2
  • 为QPushButton添加clicked()槽函数
void MainWindow::on_openFolderBtn_clicked()
{
	imagePathList.clear();  
    QString path = QFileDialog::getExistingDirectory(this,QStringLiteral("选择图片文件夹"));
    if(path.isEmpty())
        return;
    dirPath_ = path;
    qDebug()<<path;
    QDir dir(path);
    QStringList filters;
    filters<<"*.png"<<"*.jpg";  // 设置哪些格式图片的可以显示
    dir.setNameFilters(filters);
    // 设置显示的item
    for(uint idx=0;idx<dir.count();idx++)
    {
        QString imagePath = QString("%1\\%2").arg(path).arg(dir[idx]);
        imagePathList.push_back(imagePath);
        QListWidgetItem *pItem = new QListWidgetItem;
        QCheckBox *checkbox = new QCheckBox;  //复选框
        //checkbox->setIcon(QIcon(imagePath));
        pItem->setIcon(QIcon(imagePathList[idx]));
        pItem->setSizeHint(QSize(100,100));
        ui->imgList->addItem(pItem);
        ui->imgList->setItemWidget(pItem,checkbox);
    }
    qDebug()<<"all imagepath: "<<imagePathList;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • QListWidget控件设置,在构造函数中初始化
ui->imgList->setViewMode(QListView::IconMode);
    ui->imgList->setIconSize(QSize(100,100));
    ui->imgList->setWrapping(false);   //设置图片单行显示
    ui->imgList->setSpacing(10);
    ui->imgList->setMovement(QListView::Static);
  • 1
  • 2
  • 3
  • 4
  • 5

三、显示效果

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/764780
推荐阅读
相关标签
  

闽ICP备14008679号