samedi 3 février 2018

create and set custom read only attached properties from cpp

say i have some custom view class in c++ which defines a set of attached properties. A component delegate to instantiate a viewItem for when a model object is added. How would i make the Attached properties work properly, to keep external data the attache cannot set itself?

Demo code example

class someModel: public QObject
{
    QOBJECT
    Q_PROPERTY(data_1 ...)
    Q_PROPERTY(data_2 ...)
    public:
    SomeView(QObject* parent);
};

class SomviewViewAttached: public QObject
{
    QOBJECT
    // read only  properties with signals
    Q_PROPERTY(SomeView view...)
    Q_PROPERTY(SomeModel model...)
    Q_PROPERTY(int index ...)
    public:
    SomviewViewAttached(QObject* parent);
};

class SomeView : public QQuickItem
{
    QOBJECT
    // ill skip giving the full get setter implementation
    Q_PROPERTY(QQmlCommponent delegate .........)
public:
    SomeView(QObject* parent);
    Q_INVOKABLE void addToView(SomeModel* model);
private:
    List<SomeModel*> items;
static SomviewViewAttached *qmlAttachedProperties(QObject *item)
{
    return new SomeViewAttached(item);
}

};
QML_DECLARE_TYPEINFO(SomeView,QML_HAS_ATTACHED_PROPERTIES)

in qml the usage would be

SomeView{
    id: view

    Component{
        Item{
            foo1: model.data_1
            foo2: model.data_2
            number : SomeView.index
            condition : SomeView.view.something...
        }
    }
    Component.onCompleted: view.addToView(new someModel)
}

Aucun commentaire:

Enregistrer un commentaire