How to use QGalleryQueryModel with QML?



  • Hi everyBody,

    I am trying to use QGalleryQueryModel and QML to display the images on the device within my application.
    but without any results here what I did
    //GalleryModel.cpp
    @ GalleryModel::GalleryModel(QObject *parent) :
    QGalleryQueryModel(parent)
    {
    QHash<int, QByteArray> properties;
    properties.insert(Qt::UserRole + 1, "url");
    setRoleNames(properties);
    }

    QVariant GalleryModel::data(const QModelIndex &index, int role) const
    {
    return role == (Qt::UserRole + 1) && index.isValid()
    ? itemUrl(index).path()
    : QGalleryQueryModel::data(index, role);
    }@
    

    GalleryModel.h
    @ #ifndef GALLERYMODEL_H
    #define GALLERYMODEL_H

    #include <QGalleryQueryModel>
     
    QTM_USE_NAMESPACE
     
    class GalleryModel : public QGalleryQueryModel
    {
    Q_OBJECT
    public:
    explicit GalleryModel(QObject *parent = 0);
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
    signals:
     
    public slots:
     
    };
     
    #endif // GALLERYMODEL_H@
    

    //main.cpp
    @ #include <QtGui/QApplication>
    #include "qmlapplicationviewer.h"
    #include <QDeclarativeContext>
    #include <QGalleryQueryModel>
    #include <QDocumentGallery>
    #include <QHash>
    #include "gallerymodel.h"

    QTM_USE_NAMESPACE
     
    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
     
    QmlApplicationViewer viewer;
    viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
     
    QDocumentGallery * gallery = new QDocumentGallery;
    GalleryModel * model = new GalleryModel(gallery);
    model->setRootType(QDocumentGallery::Image);
     
     
    viewer.rootContext()->setContextProperty("galleryModel", model);
    viewer.setMainQmlFile&#40;QLatin1String("qml/GalleryProject/main.qml"&#41;);
    viewer.showExpanded();
     
    return app.exec();
    }
    

    @
    //main.qml
    @ import Qt 4.7
    import QtQuick 1.0
    import QtMobility.gallery 1.1

    Rectangle {
    width: 1024
    height: 768
     
    GridView {
    anchors.fill: parent
    cellWidth: 128
    cellHeight: 128
     
    model: galleryModel
     
    delegate: Image {
    source: url
    width: 128
    height: 128
     
    }
    }
    }
    

    @
    but nothing displayed
    I will appreciate any Help Thank you



  • Why are you instantiating the model on the C++ side? There is a QML model you should be able to use for this:

    http://doc.qt.nokia.com/qtmobility-1.2/qml-documentgallerymodel.html

    I notice that you insert a new property called "url", but there is already a property called "url" in the Gallery model so perhaps that is causing some problems. If you remove that additional code and simply call the base class implementation, does it work then?



  • When I use the QML model the application takes a lot of memory and could not load all the images it loads only two of them , I think this is because the images size which is (1.5 MB). and I got the following errors
    bq. [Qt Message] qrc:/qml/contactface/delegates/GalleryDelegate.qml:18:17: QML Image: Error decoding: file:///e:/images/datanil/31/image.jpg: Unable to read image data
    [Qt Message] QVGImagePool: cannot reclaim sufficient space for a 2592x1944 image

    When I used QGalleryQueryModel I am still not able to display the images
    @QGalleryQueryModel * model = new QGalleryQueryModel;
    model->setRootType(QDocumentGallery::Image);

    viewer.rootContext()->setContextProperty("galleryModel", model);
    viewer.setSource(QUrl(QLatin1String("qrc:/qml/GalleryProject/main.qml")));@
    

    and the QML file was
    @GridView {
    anchors.fill: parent
    cellWidth: 128
    cellHeight: 128

         model:galleryModel
    
         delegate: Image {
             source: url
             width: 128
             height: 128
             onSourceChanged: {
                 console.log("source changed");
             }
    
         }
     }@
    

    Thank you



  • I think that there is a bug in the QGalleryQueryModel



  • Is there anybody here?


  • Moderators

    bq.
    [Qt Message] QVGImagePool: cannot reclaim sufficient space for a 2592×1944 image

    I would try with a much smaller image first if i were you. I think your image is rather big for a mobile application.



  • but those image are displayed using the mobile gallery, also I tried small images and it was working.
    I have another question does the gallery searches all photos in the mobile or it searches on specific places?



  • Hi,

    I have same code as above. But same results. I m not retreiving any images.

    NOTE: im using QtMobility 1.1

    I can't use http://doc.qt.nokia.com/qtmobility-1.2/qml-documentgallerymodel.html since filters does not work properly:

    http://bugreports.qt.nokia.com/browse/QTMOBILITY-1741

    So im wondering what is wrong with above code and why images are not retreived?



  • Hi Jano,
    Finally I am able to use QGalleryQueryModel it took me several weeks to make it work.
    and here are the solution
    @ThumbnailModel::ThumbnailModel(QAbstractGallery *gallery, QObject *parent)
    : QGalleryQueryModel(gallery, parent)
    {
    setRootType(QDocumentGallery::Image);

    QHash<int, QByteArray> properties;
    properties.insert(Qt::UserRole + 1, "url");
    setRoleNames(properties);
    
    QHash<int, QString> columns;
    properties.insert(Qt::UserRole + 1, "url");
    addColumn(columns);
    execute();
    

    }@

    I hope I helped you



  • Hi Mismael

    It is neccessary to pass QAbstractGallery into constructor?

    Can you share all code or even project?

    Many Thanks for your help!



  • Please,

    If is possible just post whole .h and .cpp files and main.cpp ( or where do you use tuhmbnailmodel ).

    It will be really helpfull.

    Many thanks



  • In QML can you try this
    @ delegate: Image {
    source: url
    width: 128
    height: 128
    //add these two lines
    sourceSize.width: 1024
    sourceSize.height:1024
    onSourceChanged: {
    console.log("source changed");
    }@
    This will fix " [Qt Message] QVGImagePool: cannot reclaim sufficient space for a 2592×1944 image" issue



  • Any update on this?
    The standard QML Gallery Model still lets the app hang for minutes...


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.