如何将图像显示为缩略图 [英] How can I display the image as a thumbnail
问题描述
我有一个 QTreeView
来显示硬盘和目录。我还有一个 QListView
来显示图像文件如下:
I have a QTreeView
to display the hard drives and Directories. also I have a QListView
to display the images files as the following:
但我想以缩略图的形式显示图像,如下所示:
But I want to display the images as thumbnails, like the following:
我的代码:
mainWidget::mainWidget(QWidget *parent) : QWidget(parent), ui(new Ui::mainWidget){
ui->setupUi(this);
dirsModel = new QFileSystemModel;
filesModel = new QFileSystemModel;
dirsModel->setRootPath("");
ui->treeView->setModel(dirsModel);
ui->listView->setModel(filesModel);
dirsModel->setFilter(QDir::AllDirs | QDir::NoDotAndDotDot);
filesModel->setFilter(QDir::Files);
ui->treeView->hideColumn(1);
ui->treeView->hideColumn(2);
ui->treeView->hideColumn(3);
ui->treeView->header()->hide();
}
void mainWidget::on_treeView_clicked(const QModelIndex &index){
ui->listView->setRootIndex(filesModel->setRootPath(dirsModel->filePath(index)));
}
不幸的是,我不知道改变图像视图的方式是什么从图标到缩略图。
Unfortunately, I don't know what is the way of changing the image view from icon to thumbnail.
推荐答案
你应该使用特殊的 ViewMode
:
ui->listView->setViewMode(QListView::IconMode);
但它只显示图标(不是整个图像),所以我认为你应该创建例如 QStandardItemModel
(因为 QFileSystemModel
不太合适)并使用 Qt将pixmap设置为此模型: :DecorationRole
,但将此图像缩放为较小的尺寸。如你所知,如果目录中有很多图像,这个过程可能很长。
But it will show you only icons(not whole images), so I think you should create for example QStandardItemModel
(because QFileSystemModel
is not very suitable) and set pixmap to this model with Qt::DecorationRole
, but scale this images to smaller size. As you understand if there are many images in the dir, this process can be long.
正如你所看到的,你应该每次都这样(每个 on_treeView_clicked
)获取目录中的新图像列表,你可以这样做:
As you can see, you should every time (every on_treeView_clicked
) get new list of images in the directory, You can do this with:
QStringList QDir::entryList(const QStringList & nameFilters, Filters filters = NoFilter, SortFlags sort = NoSort) const
带有特殊过滤器。当你有文件列表时,你可以在循环中创建像素图,缩放它并设置为模型。
with special filters. When you have list of files, in the loop you can create pixmaps, scale it and set to model.
默认情况下 QListView :: IconMode
提供自由移动。如果你想避免这种情况,你应该使用:
By default QListView::IconMode
provide Free movement. If you want avoid this you should use:
ui->listView->setMovement(QListView::Static);
这篇关于如何将图像显示为缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!