在QML中如何使用QGalleryQueryModel提取系统图片
(Liuting - - →相关链接) |
vineet.jain
(Talk | contribs) (Vineet.jain -) |
||
| (4 intermediate revisions by 3 users not shown) | |||
| Line 1: | Line 1: | ||
| − | [[Category: | + | [[Category:Qt]][[Category:Qt Mobility]][[Category:Qt Quick]][[Category:Lang-Chinese]] |
| − | + | ||
| − | |||
| − | |||
| − | |||
{{ArticleMetaData <!-- v1.2 --> | {{ArticleMetaData <!-- v1.2 --> | ||
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] --> | |sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] --> | ||
| Line 32: | Line 28: | ||
== 代码实现== | == 代码实现== | ||
| − | + | === .h文件=== | |
<code> | <code> | ||
#include <QGalleryQueryModel> | #include <QGalleryQueryModel> | ||
| Line 46: | Line 42: | ||
}; | }; | ||
</code> | </code> | ||
| − | 我们首先需要定义一个类派生自QGalleryQueryModeL | + | 我们首先需要定义一个类派生自QGalleryQueryModeL<br> |
| − | + | === .cpp部分实现 === | |
<code> | <code> | ||
GalleryModel::GalleryModel(QAbstractGallery *gallery,QObject *parent) : | GalleryModel::GalleryModel(QAbstractGallery *gallery,QObject *parent) : | ||
| Line 73: | Line 69: | ||
} | } | ||
</code> | </code> | ||
| − | 这里我们需要注意的是不要漏写execute()函数,否则将不会获得任何数据 | + | 这里我们需要注意的是不要漏写execute()函数,否则将不会获得任何数据<br> |
| − | + | ===QML部分实现=== | |
<code> | <code> | ||
GridView { | GridView { | ||
Latest revision as of 08:51, 22 March 2012
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

