在QML中如何使用QGalleryQueryModel提取系统图片
Article Metadata
Contents |
介绍
在QML 中提供了DocumentGalleryModel来提取系统图片,但我们发现在使用过程中,当图片数量众多时,在图片缩放过程中耗费了大量时间,加载十分缓慢,这就需要我们自定义自己的数据MODEL,便于在数据MODEL中通过线程来实现自己的图片缩放方法,以提高加载速度。接下来我们将首先介绍在在QML中如何使用QGalleryQueryModel提取系统图片。
代码实现
.h文件
#include <QGalleryQueryModel>
QTM_USE_NAMESPACE
class GalleryModel : public QGalleryQueryModel
{
Q_OBJECT
public:
explicit GalleryModel(QAbstractGallery *gallery,QObject *parent = 0);
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
};
我们首先需要定义一个类派生自QGalleryQueryModeL
.cpp部分实现
GalleryModel::GalleryModel(QAbstractGallery *gallery,QObject *parent) :
QGalleryQueryModel(gallery, parent)
{
qDebug()<<"create model";
setRootType(QDocumentGallery::Image);
QHash<int, QByteArray> properties;
properties.insert(Qt::UserRole + 1, "name");
setRoleNames(properties);
QHash<int, QString> columns;
properties.insert(Qt::UserRole + 1, "name");
addColumn(columns);
execute();
}
QVariant GalleryModel::data(const QModelIndex &index, int role) const
{
return role == Qt::UserRole + 1 && index.isValid()
? itemUrl(index).path()
: QGalleryQueryModel::data(index, role);
}
这里我们需要注意的是不要漏写execute()函数,否则将不会获得任何数据
QML部分实现
GridView {
id: gridView
width: parent.width; height: parent.height
cellWidth: parent.width/3
cellHeight: parent.width/3
model: galleryModel
delegate: Column {
Image
{ source: url; width: gridView.cellWidth; height: gridView.cellHeight;
asynchronous: true
}
}
}代码下载
相关链接
'Add categories below. Remove Category:Draft when the page is complete or near complete


(no comments yet)