[Interest] Updating a model re-add everything in QML. How can i prevent that?

Nicolás Ulrich nikolaseu at gmail.com
Thu Nov 8 21:55:50 CET 2012


I got this and it seems to work fine (Qt 4.8), but it's not a model. I
remember a similar problem with the constructor but I think it was
because I was using Q_DECLARE_METATYPE(MyClass) instead of
Q_DECLARE_METATYPE(MyClass*).

------------------------------
MyClass.h
------------------------------
#ifndef MYCLASS_H
#define MYCLASS_H

#include <QObject>
#include <QVariant> // needed for Q_DECLARE_METATYPE

class MyClass : public QObject
{
    Q_OBJECT
public:
    explicit MyClass(QObject *parent = 0);
    ~MyClass();

signals:

public slots:

private:

};
Q_DECLARE_METATYPE(MyClass*)
#endif // MYCLASS_H


----------------------------------------
main.cpp
----------------------------------------
#include <QtDeclarative> // needed for qmlRegisterType

int main(int argc, char** argv)
{
    QApplication app(argc, argv);
    qmlRegisterType<MyClass>("MyClass", 1, 0, "MyClass");
    ...

-----------------------------
QML
-----------------------------

import MyClass 1.0

    ...
    MyClass {}





On Thu, Nov 8, 2012 at 3:09 PM, Mark <markg85 at gmail.com> wrote:
> On Thu, Nov 8, 2012 at 6:43 PM, Nicolás Ulrich <nikolaseu at gmail.com> wrote:
>> Are you really using this?
>> qRegisterMetaType<PathModel*>("PathModel*");            (with the * in
>> the string)
>>
>> I guess it should be
>> qRegisterMetaType<PathModel*>("PathModel");
>>
>>
>>
>> On Thu, Nov 8, 2012 at 10:36 AM, Mark <markg85 at gmail.com> wrote:
>>> On Thu, Nov 8, 2012 at 10:34 AM, Bo Thorsen <bo at fioniasoftware.dk> wrote:
>>>> Den 08-11-2012 01:33, Mark skrev:
>>>>> Hi,
>>>>>
>>>>> I have a models in C++ (a QStringList actually). In QML i'm displaying
>>>>> it using a repeater. Now when i remove some items from the end of the
>>>>> list (which happens quite a few times) then the entire list seems to
>>>>> go through the QML repeater again.
>>>>>
>>>>> What i want to do is put a QStringList in a QML Repeater (works), but
>>>>> when i remove items from the end i would like QML to be "smart" and
>>>>> also remove the items from the end and leave those that haven't
>>>>> changed. Thus not re-inserting all the items in the repeater when not
>>>>> needed.
>>>>>
>>>>> Is there some option to get that?
>>>>
>>>> To do this you need a better model than a QStringList in Repeater. Take
>>>> a look at QStringListModel instead. But if you just call setStringList()
>>>> you won't see any improvement. You have to use insertRows and removeRows
>>>> instead.
>>>>
>>>> Bo Thorsen.
>>>>
>>>> Come by my DevDays talk in Berlin - "Designing for testability". Learn how to build and run your unit tests with the Qt application.
>>>>
>>>> Fionia Software - Qt experts for hire.
>>>>
>>>> _______________________________________________
>>>> Interest mailing list
>>>> Interest at qt-project.org
>>>> http://lists.qt-project.org/mailman/listinfo/interest
>>>
>>> Thanks a lot for that, that will probably work.
>>> However, i'm hitting another issue when trying to make a custom
>>> StringListModel (only due to setRoleNames...) This is how it looks:
>>>
>>> // pathmodel.h
>>> #ifndef PATHMODEL_H
>>> #define PATHMODEL_H
>>>
>>> #include <QStringListModel>
>>>
>>> class PathModel : public QStringListModel
>>> {
>>> public:
>>>     PathModel();
>>> };
>>>
>>> Q_DECLARE_METATYPE(PathModel*)
>>>
>>> #endif // PATHMODEL_H
>>>
>>>
>>> // pathmodel.cpp
>>> #include "pathmodel.h"
>>>
>>> PathModel::PathModel()
>>> {
>>>     QHash<int,QByteArray> roleNames;
>>>     roleNames.insert(Qt::DisplayRole, "modelData");
>>>     setRoleNames(roleNames);
>>> }
>>>
>>> Now if i want to register the new meta type using:
>>> qRegisterMetaType<PathModel*>("PathModel*");
>>>
>>> The app instantly crashes when i run it.. It must be something very
>>> obvious that i'm missing, but i don't see it.
>>>
>>> Any idea what i'm doing wrong?
>>> _______________________________________________
>>> Interest mailing list
>>> Interest at qt-project.org
>>> http://lists.qt-project.org/mailman/listinfo/interest
>
> Yes.
>
> But even if i change it to your suggestion it doesn't work. It's all
> in the private copy ctor...



More information about the Interest mailing list