从cpp创建和设置自定义只读附加属性

时间:2018-02-04 00:15:17

标签: c++ qt c++11 qtquick2

说我在c ++中有一些自定义视图类,它定义了一组附加属性。用于在添加模型对象时实例化viewItem的组件委托。我如何使Attached属性正常工作,以保持attache无法设置的外部数据?

演示代码示例

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)

在qml中的用法是

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)
}

0 个答案:

没有答案