C++ Qt-Quick:无法渲染QQuickPaintedItem';孩子们
我试图渲染一些C++ Qt-Quick:无法渲染QQuickPaintedItem';孩子们,c++,qt,qml,qtquick2,C++,Qt,Qml,Qtquick2,我试图渲染一些QQuickPaintedItems及其子对象,但我只渲染了父对象。请告诉我我的代码有什么问题 Graph.h(我的自定义QQuickPaintedItem): VoltRule.cpp #include "voltrule.h" #include <QPainter> VoltRule::VoltRule(QQuickItem *parent) : QQuickPaintedItem(parent) { setFlag(QQuickItem::Ite
QQuickPaintedItem
s及其子对象,但我只渲染了父对象。请告诉我我的代码有什么问题
Graph.h
(我的自定义QQuickPaintedItem
):
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);
}
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());
}
}
提前感谢回答了问题;只需在VoltRule构造函数中设置大小即可解决问题:
VoltRule::VoltRule(QQuickItem *parent) :
QQuickPaintedItem(parent)
{
setSize(QSizeF(100, 200));
}
这看起来有点奇怪,因为孩子在父母的绿线上画白色,只擦除了父母的一部分线
下面是基于父对象大小设置大小的代码。调整窗口大小,您将看到子级更新,而无需显式的“update()”调用:
VoltRule.h
#include <QQuickPaintedItem>
class VoltRule : public QQuickPaintedItem
{
Q_OBJECT
public:
explicit VoltRule(QQuickItem *parent = 0);
void paint(QPainter *painter);
signals:
public slots:
};
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
#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);
}
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());
}
}
QQuickItem*VoltRule::parentItem()
{
返回qobject_cast(parent());
}
VoltRule::VoltRule(QQuickItem*父项):
QQuickPaintedItem(父级)
{
连接(此,&QQuickItem::parentChanged,此,&VoltRule::onParentChanged);
onParentChanged();
}
无效规则::绘制(QPainter*painter)
{
QRect rect(.1*width(),.1*height(),.8*width(),.8*height());
QPen笔(Qt::蓝色);
画师->画笔(画笔);
画师->绘图矩形(矩形);
}
void VoltRule::onParentChanged()
{
//断开前一个父级的信号(如果有)
断开连接();
if(const auto obj=parentItem()){
连接(obj、&QQuickItem::widthChanged、this、&VoltRule::onParentWidthChanged);
连接(obj、&QQuickItem::heightChanged、this、&VoltRule::onParentHeightChanged);
onParentWidthChanged();
onParentHeightChanged();
}
}
void VoltRule::onParentWidthChanged()
{
if(const auto obj=parentItem()){
设置宽度(对象->宽度());
}
}
void VoltRule::onParentHeightChanged()
{
if(const auto obj=parentItem()){
设置高度(obj->height());
}
}
您没有给孩子指定尺寸。给它一个宽度/高度。默认情况下,相对于其父项,默认位置为(0,0)!非常感谢!