×
Namespaces

Variants
Actions
Revision as of 05:46, 22 March 2012 by liuting (Talk | contribs)

在QML中如何使用QGalleryQueryModel提取系统图片

From Nokia Developer Wiki
Jump to: navigation, search

Delete instructional text in italic

This article explains how to ... Replace the abstract text with a short paragraph (or sentence) describing what the topic covers.

Enter article metadata as described below. Note that this template can be placed anywhere in the article. Do not remove parameters that you do not use

Article Metadata
Article
Created: ()
Last edited: liuting (22 Mar 2012)

Contents

介绍

在QML 中提供了DocumentGalleryModel来提取系统图片,但我们发现在使用过程中,当图片数量众多时,在图片缩放过程中耗费了大量时间,加载十分缓慢,这就需要我们自定义自己的数据MODEL,便于在数据MODEL中通过线程来实现自己的图片缩放方法,以提高加载速度。接下来我们将首先介绍在在QML中如何使用QGalleryQueryModel提取系统图片。

代码实现

1. .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
2.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()函数,否则将不会获得任何数据
3 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
}
}
}

代码下载

File:Picbrowser.zip

相关链接

'Add categories below. Remove Category:Draft when the page is complete or near complete

219 page views in the last 30 days.
×