在哪里对 QML 类型的成员进行初始化?
我生成了一个自定义类型的多边形
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
Point 也是我生成的类型。
我在polygon.h中用一条线处理顶点:
Q_PROPERTY(QDeclarativeListProperty<Point> vertices READ vertices)
因为需要使用点作为QVector,
我使用这些线,m_vertices是处理从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()));
}
我想问我应该把这些线放在哪里。 在油漆中?然后这些线一遍又一遍地运行,哪里叫paint? 那么在构造函数中,m_vertices 当时没有初始化吗? 谢谢你的任何想法。
I generated a custom type polygon
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}
]
}
Here is my polygon.h file:
#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
Point is also a type generated by me.
I handle vertices with a line in polygon.h:
Q_PROPERTY(QDeclarativeListProperty<Point> vertices READ vertices)
because of the need to use points as a QVector,
i use these lines, m_vertices is the variable name that handle vertices array got from 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()));
}
I want to ask where should i put these lines.
In paint? Then these lines run again and again where paint is called?
In constructer then, m_vertices is not initialized that time?
Thanks for any idea.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据文档,如果您的 Polygon 继承自 QDeclarativeItem 那么您还可以实现 QDeclarativeParserStatus 接口,当组件准备好并且值已初始化时,您将收到通知。
这看起来是更新 vPnt QVector 的好地方。
(我没有亲自测试过这个解决方案)
According to the documentation if your Polygon inherits from QDeclarativeItem then you can also implement the QDeclarativeParserStatus interface and you will be notified when the component is ready and values have been initialized.
That looks like a good place where to update your vPnt QVector.
(I haven't tested this solution personally)