Qt Quick:无法渲染QQuickPaintedItem的孩子

时间:2014-12-02 12:16:38

标签: c++ qt qml qtquick2

我试图渲染一些QQuickPaintedItem和他们的孩子,但我只得到了父母。请告诉我我的代码有什么问题。

Graph.h(我的自定义QQuickPaintedItem):

#include <QQuickPaintedItem>
#include "voltrule.h"

class Graph : public QQuickPaintedItem
{
    Q_OBJECT
public:
    explicit Graph(QQuickItem *parent = 0);
    void paint(QPainter *painter);


private:
    VoltRule *_rule;
};

Graph::Graph(QQuickItem *parent) :
    QQuickPaintedItem(parent)
{
    _rule = new VoltRule(this);
    _rule->setParentItem(this);
}

void Graph::paint(QPainter *painter)
{
    QRect rect(10, 20, 100, 200);
    QPen pen(Qt::green);
    painter->setPen(pen);
    painter->drawRect(rect);
}

VoltRule.h

#include <QQuickPaintedItem>

class VoltRule : public QQuickPaintedItem
{
    Q_OBJECT
public:
    explicit VoltRule(QQuickItem *parent = 0);
    void paint(QPainter *painter);
signals:

public slots:

};

VoltRule.cpp

#include "voltrule.h"
#include <QPainter>

VoltRule::VoltRule(QQuickItem *parent) :
    QQuickPaintedItem(parent)
{
    setFlag(QQuickItem::ItemHasContents);
}

void VoltRule::paint(QPainter *painter)
{
    QRect rect(10, 20, 100, 200);
    QPen pen(Qt::white);
    painter->setPen(pen);
    painter->drawRect(rect);
}

main.qml

ApplicationWindow {
    width: 1367
    height: 766
   Graph{
      anchors.fill:parent
   }
}

提前致谢

1 个答案:

答案 0 :(得分:0)

jbh回答了问题;只需在VoltRule构造函数中设置大小即可解决问题:

VoltRule::VoltRule(QQuickItem *parent) :
    QQuickPaintedItem(parent)
{
    setSize(QSizeF(100, 200));
}

这看起来有点奇怪,因为孩子在父母的绿线上方画了白色,只擦除了父母的部分线。

这是根据父母的身高设定身高的代码。调整窗口大小,您将看到子更新,而没有明确的“ update()”调用:

VoltRule.h

class VoltRule : public QQuickPaintedItem
{
    Q_OBJECT
public:
    explicit VoltRule(QQuickItem *parent = 0);
    void paint(QPainter *painter);

private:
    QQuickItem* parentItem();
    void onParentChanged();
    void onParentWidthChanged();
    void onParentHeightChanged();
};

VoltRule.cpp

QQuickItem* VoltRule::parentItem()
{
    return qobject_cast<QQuickItem*>(parent());
}

VoltRule::VoltRule(QQuickItem *parent) :
    QQuickPaintedItem(parent)
{
    connect(this, &QQuickItem::parentChanged, this, &VoltRule::onParentChanged);
    onParentChanged();
}

void VoltRule::paint(QPainter *painter)
{
    QRect rect(.1*width(), .1*height(), .8*width(), .8*height());
    QPen pen(Qt::blue);
    painter->setPen(pen);
    painter->drawRect(rect);
}

void VoltRule::onParentChanged()
{
    // disconnect signals from previous parent, if there was one
    disconnect();

    if (const auto obj = parentItem()) {
        connect(obj, &QQuickItem::widthChanged,  this, &VoltRule::onParentWidthChanged);
        connect(obj, &QQuickItem::heightChanged, this, &VoltRule::onParentHeightChanged);
        onParentWidthChanged();
        onParentHeightChanged();
    }
}

void VoltRule::onParentWidthChanged()
{
    if (const auto obj = parentItem()) {
        setWidth(obj->width());
    }
}

void VoltRule::onParentHeightChanged()
{
    if (const auto obj = parentItem()) {
        setHeight(obj->height());
    }
}