[Qt-qml] Model view example showing editing capability
Tim O'Callaghan
tocallaghan at meyn.net
Thu Mar 17 16:50:00 CET 2011
On 01/17/2011 08:04 AM, bea.lam at nokia.com 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<jamil.m.naja at gmail.com> 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
>> Qt-qml at qt.nokia.com
>> http://lists.qt.nokia.com/mailman/listinfo/qt-qml
>>
>>
>> <ATT00001..txt>
>
> _______________________________________________
> Qt-qml mailing list
> Qt-qml at qt.nokia.com
> http://lists.qt.nokia.com/mailman/listinfo/qt-qml
-------------- next part --------------
A non-text attachment was scrubbed...
Name: main.cpp
Type: text/x-c++src
Size: 3475 bytes
Desc: not available
Url : http://lists.qt.nokia.com/pipermail/qt-qml/attachments/20110317/4f5042b4/attachment.bin
More information about the Qt-qml
mailing list