在哪里初始化QML类型的成员?

在哪里初始化QML类型的成员?,qml,Qml,我生成了一个自定义类型的多边形 Polygon { id: aPieChart anchors.centerIn: parent width: 100; height: 100 name: "A simple polygon" color: "blue" vertices:[ Point{x:20.0; y:40.

我生成了一个自定义类型的多边形

Polygon {
             id: aPieChart
             anchors.centerIn: parent
             width: 100; height: 100
             name: "A simple polygon"
             color: "blue"
             vertices:[

             Point{x:20.0; y:40.0},
             Point{x:40.0; y:40.0},
             Point{x:20.0; y:20.0}
             ]

         }
这是我的polygon.h文件:

#ifndef POLYGON_H
#define POLYGON_H


#include <QDeclarativeItem>
#include <QColor>
#include "point.h"

class Polygon : public QDeclarativeItem
{
    Q_OBJECT
    Q_PROPERTY(QString name READ name WRITE setName)
    Q_PROPERTY(QColor color READ color WRITE setColor)
    Q_PROPERTY(QDeclarativeListProperty<Point> vertices READ vertices)

public:
    Polygon(QDeclarativeItem *parent = 0);

    QString name() const;
    void setName(const QString &name);

    QColor color() const;
    void setColor(const QColor &color);

    QDeclarativeListProperty<Point> vertices();
    static void append_vertex(QDeclarativeListProperty<Point> *list, Point *vertex);

    //void Polygon::dragEnterEvent(QGraphicsSceneDragDropEvent *event);
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);

private:
    QString m_name;
    QColor m_color;
    QList<Point *> m_vertices;
};


#endif // POLYGON_H
\ifndef POLYGON\u H
#定义多边形
#包括
#包括
#包括“h点”
类多边形:公共QDeclarativeItem
{
Q_对象
Q_属性(QString名称读取名称写入集合名称)
Q_属性(QColor颜色读取颜色写入设置颜色)
Q_属性(QdeCrativeListProperty顶点读取顶点)
公众:
多边形(QdeCrativeItem*parent=0);
QString name()常量;
void setName(常量QString&name);
QColor color()常数;
void setColor(常量QColor和color);
QdeCrativeListProperty顶点();
静态void append_顶点(QDeclarativeListProperty*列表,点*顶点);
//void Polygon::dragEnterEvent(Qgraphicscendragdropevent*事件);
无效绘制(QPainter*painter,const QStyleOptionGraphicsItem*选项,QWidget*widget=0);
私人:
QString m_名称;
QColor m_颜色;
QList m_顶点;
};
#endif//POLYGON_H
Point也是我生成的一种类型

我使用多边形中的线处理顶点。h:

Q_PROPERTY(QDeclarativeListProperty<Point> vertices READ vertices)
Q_属性(QdeCrativeListProperty顶点读取顶点)
因为需要使用点作为QVector

我使用这些线,m_顶点是处理从QML获得的顶点数组的变量名:

QVector<QPointF> vPnt;
for(int i=0;i<m_vertices.length();i++){
       vPnt.append(QPointF(m_vertices.at(i)->x(),m_vertices.at(i)->y()));

}
qvectorvpnt;
对于(int i=0;ix(),m_顶点.at(i)->y());
}
我想问我应该把这些线放在哪里。 在油漆里?然后这些线在叫做油漆的地方一次又一次地运行? 那么在构造函数中,m_顶点当时没有初始化?
谢谢您的建议。

根据文档,如果您的多边形继承自QDeclarativeItem,则您也可以实现该接口,并在组件准备就绪且值已初始化时收到通知

这看起来是一个更新vPnt QVector的好地方

(我没有亲自测试过此解决方案)