void MyplayerWidget::slot_open_pBtn_clicked() { QStringList fileNames = QFileDialog::getOpenFileNames(this,"获取音视频文件", "./", "*.jpg *.mp3 *.mp4 *.avi"); // m_MfileNameAndFilePath.clear(); for(int i = 0;i < fileNames.count();i++) { QStringList strSplitList = fileNames.at(i).split("/"); QString str; for(int j = 0;j < strSplitList.count();j++) { str = strSplitList.at(strSplitList.count()-1); } m_MfileNameAndFilePath.insert(str,fileNames.at(i)); QListWidgetItem *item = new QListWidgetItem(str); item->setSizeHint(ITEM_SIZE); ui->listWidget->insertItem(i,item); } }如何将这段代码改成也能添加图片
时间: 2023-06-11 12:09:31 浏览: 920
戴尔笔记本修图图纸:Dell 5400 EDC41 LA-G891P
可以将文件过滤器改为支持图片格式的,例如:
```
QStringList fileNames = QFileDialog::getOpenFileNames(this,"获取音视频文件和图片", "./", "*.jpg *.jpeg *.png *.bmp *.gif *.mp3 *.mp4 *.avi");
```
这样用户可以选择图片文件和音视频文件进行添加。然后在添加文件的循环中,判断文件类型是图片还是音视频,如果是图片则创建一个QPixmap对象,将图片文件路径和QPixmap对象存储到map中,同时在QListWidget中添加一个QListWidgetItem对象,将QPixmap对象设置为该项的icon,就可以实现添加图片文件的功能了。示例代码如下:
```
for(int i = 0; i < fileNames.count(); i++) {
QString fileName = fileNames.at(i);
QStringList strSplitList = fileName.split("/");
QString str;
for(int j = 0; j < strSplitList.count(); j++) {
str = strSplitList.at(strSplitList.count()-1);
}
QString suffix = QFileInfo(fileName).suffix();
if(suffix == "jpg" || suffix == "jpeg" || suffix == "png" || suffix == "bmp" || suffix == "gif") {
QPixmap pixmap(fileName);
m_MfileNameAndPixmap.insert(str, pixmap);
QListWidgetItem *item = new QListWidgetItem(str);
item->setIcon(pixmap);
item->setSizeHint(ITEM_SIZE);
ui->listWidget->insertItem(i, item);
}
else if(suffix == "mp3" || suffix == "mp4" || suffix == "avi") {
m_MfileNameAndFilePath.insert(str, fileName);
QListWidgetItem *item = new QListWidgetItem(str);
item->setSizeHint(ITEM_SIZE);
ui->listWidget->insertItem(i, item);
}
}
```
阅读全文