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

发布于 2024-11-28 14:25:26 字数 1911 浏览 0 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水波映月 2024-12-05 14:25:26

根据文档,如果您的 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文