On 01/17/2011 08:04 AM, [email protected] wrote:
You can find an example of using QAbstractListModel with QML under 
examples/declarative/modelviews/abstractitemmodel. The example is also online 
at 
http://doc.qt.nokia.com/4.7-snapshot/declarative-modelviews-abstractitemmodel.html.

The example doesn't modify the model from QML, but you could for example add a 
method to the model class like this:

        Q_INVOKABLE void updateModel(int index, const QVariant&value);

and then you can call this from QML to modify the model data.

The example shows some of the signals that needs to be emitted from a Qt model 
in order to update the view (e.g. QAbstractItemModel::beginInsertRows() ). If 
you are simply changing the data in an existing row, then emit dataChanged(). 
See 
http://doc.qt.nokia.com/4.7-snapshot/model-view-programming.html#model-subclassing-reference.



I've been playing with this example, to try adding a thread that randomly adds new items to an Abstract model, using a separate thread.

It seems the abstractmodel+QML cannot cope with updating. I'm hoping its something wrong with the model implementation, i guess it needs to send a dataChanged signal or something? The error it generates when attempting to add the second new item is:

QObject::connect: Cannot queue arguments of type 'QModelIndex'
(Make sure 'QModelIndex' is registered using qRegisterMetaType().)

How do i fix this?

The changed main.cpp is attached, the rest of the source for the example is here:
http://qt.gitorious.org/qt/qt/trees/4.7/examples/declarative/modelviews/abstractitemmodel

regards,

Tim.


regards,

Bea


On 15/01/2011, at 6:38 PM, ext Pelle Johnsen wrote:

Hi,

I can recommend subclassing QAbstractListModel 
(http://doc.trolltech.com/4.7/qabstractlistmodel.html). You can use a normal 
QList, but it only has a single change signal, so if e.g. you insert an item 
then QML thinks the whole list has changed, which can cause performance 
problems. The whole Qt model/view classes are rather complex (compared to 
QList), but in my experience it's worth the effort to use it.

  -pjoe

On Fri, Jan 14, 2011 at 6:26 PM, Jamil Naja<[email protected]>  wrote:
Hi guys,
As the title suggests, I am looking for an example with a C++ model and QML 
view where the QML view (or other QML elements within the QML code) doing 
modification on the c++ model data.

Any suggestions ?

Thanks
Jamil

_______________________________________________
Qt-qml mailing list
[email protected]
http://lists.qt.nokia.com/mailman/listinfo/qt-qml


<ATT00001..txt>

_______________________________________________
Qt-qml mailing list
[email protected]
http://lists.qt.nokia.com/mailman/listinfo/qt-qml
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation ([email protected])
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
**     the names of its contributors may be used to endorse or promote
**     products derived from this software without specific prior written
**     permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
** $QT_END_LICENSE$
**
****************************************************************************/
#include "model.h"
#include <QDeclarativeContext>
#include <QDeclarativeView>
#include <QApplication>

#include <QtCore>
QMutex paws;
QWaitCondition newAnimal;
QDeclarativeContext *ctxt;
const int maxTime = 1000;
AnimalModel model;

class MyThread : public QThread
{
 public:
    void run();
};

void MyThread::run()
 {
     // reset random seed
     qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
     int randomTime;

     forever {
         // calculate a random nonzero time.
         do {
             randomTime=qrand() % maxTime;
         } while (randomTime == 0);

         QString lengthMessage = QString::number(randomTime) + " cm long";
         qDebug() << lengthMessage;

         // add new snake to the abstract model
         model.addAnimal(Animal("Snake", lengthMessage));

         // wait randomTime before adding another snake
         paws.lock();
         newAnimal.wait(&paws,randomTime);
         paws.unlock();
     }
 }

int main(int argc, char ** argv)
{
    QApplication app(argc, argv);

    model.addAnimal(Animal("Wolf", "Medium"));
    model.addAnimal(Animal("Polar bear", "Large"));
    model.addAnimal(Animal("Quoll", "Small"));

    QDeclarativeView view;
    ctxt = view.rootContext();
    ctxt->setContextProperty("myModel", &model);

    // begin adding snakes
    MyThread snakes; 
    snakes.start();

    view.setSource(QUrl("qrc:view.qml"));
    view.show();

    return app.exec();
}

_______________________________________________
Qt-qml mailing list
[email protected]
http://lists.qt.nokia.com/mailman/listinfo/qt-qml

Reply via email to