QQuickPaintedItem在QML设计器中不可见

时间:2019-05-19 12:02:10

标签: c++ qt qml

我创建了QQuickPaintedItem类,该类负责使用可重写的paint()方法绘制UI内容。该组件放置在Qml中,但是我不能在Designer中看到它。但是我可以在运行时看到结果。 我正在关注https://doc.qt.io/qt-5/qtquick-customitems-painteditem-example.html

什么都没用,我真的需要在运行时不在qml设计器中查看输出。

我正在关注https://doc.qt.io/qt-5/qtquick-customitems-painteditem-example.html 这是头文件的源代码

class TextBalloon : public QQuickPaintedItem
{
Q_OBJECT
Q_PROPERTY(bool rightAligned READ isRightAligned WRITE setRightAligned NOTIFY rightAlignedChanged)

public:
    TextBalloon(QQuickItem *parent = 0);
    void paint(QPainter *painter);

    bool isRightAligned();
    void setRightAligned(bool rightAligned);

private:
    bool rightAligned;

signals:
    void rightAlignedChanged();
};

和定义

TextBalloon::TextBalloon(QQuickItem *parent)
: QQuickPaintedItem(parent)
, rightAligned(false){}

void TextBalloon::paint(QPainter *painter){
QBrush brush(QColor("#007430"));

painter->setBrush(brush);
painter->setPen(Qt::NoPen);
painter->setRenderHint(QPainter::Antialiasing);

QSizeF itemSize = size();
painter->drawRoundedRect(0, 0, itemSize.width(), itemSize.height() - 10, 10, 10);

if (rightAligned)
{
    const QPointF points[3] = {
        QPointF(itemSize.width() - 10.0, itemSize.height() - 10.0),
        QPointF(itemSize.width() - 20.0, itemSize.height()),
        QPointF(itemSize.width() - 30.0, itemSize.height() - 10.0),
    };
    painter->drawConvexPolygon(points, 3);
}
else
{
    const QPointF points[3] = {
        QPointF(10.0, itemSize.height() - 10.0),
        QPointF(20.0, itemSize.height()),
        QPointF(30.0, itemSize.height() - 10.0),
    };
    painter->drawConvexPolygon(points, 3);
}
}

这是qml文件

ListModel {
id: balloonModel
ListElement {
    balloonWidth: 200
}
ListElement {
    balloonWidth: 120
}
}

ListView {
anchors.bottom: controls.top
anchors.bottomMargin: 2
anchors.top: parent.top
id: balloonView
delegate: TextBalloon {
    anchors.right: index % 2 == 0 ? undefined : parent.right
    height: 60
    rightAligned: index % 2 == 0 ? false : true
    width: balloonWidth
}
model: balloonModel
spacing: 5
width: parent.width
}

请对此提供帮助,我真的需要在qml设计器中查看绘制的输出 或请提出另一种绘画方式,以便我可以在QML Designer中看到输出。 谢谢:)

0 个答案:

没有答案