[SOLVED] How do I know my QQmlListProperty changed from QML?



  • With "this example":http://qt-project.org/doc/qt-5.0/qtquick/qml-referenceexamples-properties.html I've managed to make a list property for my custom type. Now I want to process the list every time it is set from QML, how this is done?

    I can think about QQmlParserStatus, but is only gives me ability to know all my object properties are set at instantiation time. What if my list somehow changes on event? Does it even able to change? Because if not, QQmlParserStatus looks sufficient.

    An example of list processing can be seen "somewhere here":http://qt.gitorious.org/qt/qtdeclarative/blobs/6492909d345681dbe54deecbb02307a96b977b6d/src/quick/items/qquickitem.h. QtQuick Item does split drawable elements from resource ones. But overall code looks complicated and tricky, that is why I look for simple solutions like in example from which I started.



  • Better implement a QAbstractListModel derived collection class instead of using the QQmlListProperty template class. The QALM class interface offers the required signals to detect changes on the list itself.



  • @Image
    {
    id: gameMenu
    window: system.graphic
    image: "mainbg"
    alpha: 0
    fadeTime: 200
    width: system.value("graphic/width")
    height: system.value("graphic/height")
    Component.onCompleted: fadeIn()
    onFadeInDone: grabControl()
    cursor: true

    ButtonSilver { x: 20; y: 20; }
    ButtonSilver { x: 20; y: 70; }
    ButtonSilver { x: 20; y: 120; }
    ButtonSilver { x: 20; y: 170; }
    }@

    This is why I use QQmlListProperty. Now in "Image" class I have to process those buttons in list to tell Image's backend that those buttons are children and they are drawn correctly then.
    QAbstractListModel looks more like imperative way of working, but I want declarative way to be preferred for best QML syntax available.



  • If you want your list property content to be modifiable from JS/QML and detect the changes in your Image class then either your container class (list property implementation) allows you to detect the changes or you have to provide a method in class Image to be called when adding/removing items.
    QQmlListProperty is a simple container and as far as I know does not offer you detecting changes to the underlying QList content.



  • You control the implementation of both the parent class and the subclass. Do something like the following:

    @
    mylistproperty_append(CustomType *t, ParentObject *obj)
    {
    connect(t, SIGNAL(somePropertyChangeSignal()), obj, SLOT(doWhatever()));
    obj->m_someList.append(t);
    emit obj->mylistpropertyChanged();
    }
    @

    Because you control the entire thing, you can connect change signals from appended instances to slots in the parent class which "do whatever" (eg, emit further change signals, do some processing, whatever).

    Cheers,
    Chris.



  • Thank you, Chris, this seems more like it is in Qt sources: custom append function implemented for property. I think such an implementation really gives you more control over the situation, will give it a try. However,
    @{
    connect(t, SIGNAL(somePropertyChangeSignal()), obj, SLOT(doWhatever()));
    ...
    emit obj->mylistpropertyChanged();
    }@

    I'd use metacall for that, if connect is to be made each call like here. May be you wanted to say connect is somewhere else around. But I got the point, thanks.


Log in to reply
 

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