QML 简单扩展不起作用
QML simple extension doesn't work
我正在尝试使用 C++ class 扩展 QML。我从创建一个简单的 class Line
开始。它调用 setter,但不调用 getter 和 paint
方法。任何想法为什么?在 QtQuick 1.x 中,我应该在构造函数中调用 setFlag(QGraphicsItem::ItemHasNoContents, false);
。也许我应该叫类似的东西?
代码如下:
Line.h
#ifndef LINE_H
#define LINE_H
#include <QQuickPaintedItem>
class Line : public QQuickPaintedItem
{
Q_OBJECT
Q_PROPERTY(int x1 READ x1 WRITE setx1 NOTIFY x1Changed)
Q_PROPERTY(int y1 READ y1 WRITE sety1 NOTIFY y1Changed)
Q_PROPERTY(int x2 READ x2 WRITE setx2 NOTIFY x2Changed)
Q_PROPERTY(int y2 READ y2 WRITE sety2 NOTIFY y2Changed)
Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
Q_PROPERTY(int penWidth READ penWidth WRITE setPenWidth NOTIFY penWidthChanged)
public:
Line(QQuickItem *parent = nullptr);
~Line();
void paint(QPainter *painter) override;
int x1() const;
...
signals:
void x1Changed(int arg);
...
public slots:
void setx1(int arg);
...
private:
int m_x1;
...
};
#endif // LINE_H
Line.cpp
#include "Line.h"
#include <QPen>
#include <QPainter>
Line::Line(QQuickItem *parent) : QQuickPaintedItem(parent)
{
}
Line::~Line()
{
}
void Line::paint(QPainter *painter)
{
QPen pen(m_color, m_penWidth);
painter->setPen(pen);
int x = qMin(m_x1, m_x2) - m_penWidth/2;
int y = qMin(m_y1, m_y2) - m_penWidth/2;
painter->drawLine(m_x1 - x, m_y1 - y, m_x2 - x, m_y2 - y);
}
int Line::x1() const
{
return m_x1;
}
...
void Line::setx1(int arg)
{
if (m_x1 == arg)
return;
m_x1 = arg;
emit x1Changed(arg);
}
...
另外很明显,我在 main
中添加了我的 class 的注册:
qmlRegisterType("extentiongui", 1, 0, "Line");
我的QML代码:
Line {
id: lineDelimiter
x1: 863
x2: 863
y1: 660
y2: 800
penWidth: 8000
color: "#c2c2c2"
}
如果您的项目有视觉内容,您应该设置 QQuickItem::ItemHasContents
标志。
setFlag(QQuickItem::ItemHasContents, true);
此外,您应该在 setter 函数中调用 update()
来重绘您的项目。
void Line::setx1(int arg)
{
if (m_x1 == arg)
return;
m_x1 = arg;
emit x1Changed(arg);
update();
}
我正在尝试使用 C++ class 扩展 QML。我从创建一个简单的 class Line
开始。它调用 setter,但不调用 getter 和 paint
方法。任何想法为什么?在 QtQuick 1.x 中,我应该在构造函数中调用 setFlag(QGraphicsItem::ItemHasNoContents, false);
。也许我应该叫类似的东西?
代码如下:
Line.h
#ifndef LINE_H
#define LINE_H
#include <QQuickPaintedItem>
class Line : public QQuickPaintedItem
{
Q_OBJECT
Q_PROPERTY(int x1 READ x1 WRITE setx1 NOTIFY x1Changed)
Q_PROPERTY(int y1 READ y1 WRITE sety1 NOTIFY y1Changed)
Q_PROPERTY(int x2 READ x2 WRITE setx2 NOTIFY x2Changed)
Q_PROPERTY(int y2 READ y2 WRITE sety2 NOTIFY y2Changed)
Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
Q_PROPERTY(int penWidth READ penWidth WRITE setPenWidth NOTIFY penWidthChanged)
public:
Line(QQuickItem *parent = nullptr);
~Line();
void paint(QPainter *painter) override;
int x1() const;
...
signals:
void x1Changed(int arg);
...
public slots:
void setx1(int arg);
...
private:
int m_x1;
...
};
#endif // LINE_H
Line.cpp
#include "Line.h"
#include <QPen>
#include <QPainter>
Line::Line(QQuickItem *parent) : QQuickPaintedItem(parent)
{
}
Line::~Line()
{
}
void Line::paint(QPainter *painter)
{
QPen pen(m_color, m_penWidth);
painter->setPen(pen);
int x = qMin(m_x1, m_x2) - m_penWidth/2;
int y = qMin(m_y1, m_y2) - m_penWidth/2;
painter->drawLine(m_x1 - x, m_y1 - y, m_x2 - x, m_y2 - y);
}
int Line::x1() const
{
return m_x1;
}
...
void Line::setx1(int arg)
{
if (m_x1 == arg)
return;
m_x1 = arg;
emit x1Changed(arg);
}
...
另外很明显,我在 main
中添加了我的 class 的注册:
qmlRegisterType("extentiongui", 1, 0, "Line");
我的QML代码:
Line {
id: lineDelimiter
x1: 863
x2: 863
y1: 660
y2: 800
penWidth: 8000
color: "#c2c2c2"
}
如果您的项目有视觉内容,您应该设置 QQuickItem::ItemHasContents
标志。
setFlag(QQuickItem::ItemHasContents, true);
此外,您应该在 setter 函数中调用 update()
来重绘您的项目。
void Line::setx1(int arg)
{
if (m_x1 == arg)
return;
m_x1 = arg;
emit x1Changed(arg);
update();
}