关于qquickpainteditem类绘制图形的问题

[复制链接]
查看11 | 回复1 | 2021-1-27 05:08:47 | 显示全部楼层 |阅读模式
我想使用qquickpainteditem类来在qml上绘制图形,之前有实现过绘图功能
这次我照葫芦画瓢再写一遍,发现行不通了,试过很多方法怎么都显示没调用过paint()函数,
想到由于它是继承的qquickitem,我就重写updatepaintnode(),发现他有调用这个函数,而不是调我想要的paint(),这让我很奇怪
上代码
roi.h:
classrectRoi:publicQQuickPaintedItem
{
Q_OBJECT
public:
rectRoi(QQuickItem*parent=0);
virtual~rectRoi()Q_DECL_OVERRIDE;
protected:
voidpaint(QPainter*painter)Q_DECL_OVERRIDE;
QRectFboundingRect()constQ_DECL_OVERRIDE;
QSGNode*updatePaintNode(QSGNode*Node,UpdatePaintNodeData*)override;
private:
QRectFRect,mBoundingRect;
QPenPointPen,LinePen;
voidsetRect(constint&x,constint&y,constint&w,constint&h);
};

roi.cpp:
rectRoi::rectRoi(QQuickItem*parent)
:QQuickPaintedItem(parent)
{
PointPen.setColor("red");
PointPen.setWidth(9);
LinePen.setColor("red");
LinePen.setStyle(Qt::SolidLine);
LinePen.setWidth(3);
setRect(300,200,200,100);
//setFlag(QQuickItem::ItemHasContents);
}
rectRoi::~rectRoi()
{
}
QSGNode*rectRoi::updatePaintNode(QSGNode*Node,UpdatePaintNodeData*)
{
qDebug()setRenderHint(QPainter::Antialiasing,true);
painter->setPen(PointPen);
painter->drawPoint(Rect.topLeft());
painter->drawPoint(Rect.topRight());
painter->drawPoint(Rect.bottomLeft());
painter->drawPoint(Rect.bottomRight());
painter->setPen(LinePen);
painter->drawLine(Rect.topLeft(),Rect.topRight());
painter->drawLine(Rect.topRight(),Rect.bottomRight());
painter->drawLine(Rect.bottomRight(),Rect.bottomLeft());
painter->drawLine(Rect.bottomLeft(),Rect.topLeft());
}
QRectFrectRoi::boundingRect()const
{
returnmBoundingRect;
}
voidrectRoi::setRect(constint&x,constint&y,constint&w,constint&h)
{
Rect=QRectF(x,y,w,h);
mBoundingRect.setX(Rect.x()-Rect.width()/2);
mBoundingRect.setY(Rect.y()-Rect.height()/2);
mBoundingRect.setWidth(Rect.width());
mBoundingRect.setHeight(Rect.height());
qDebug()
欢迎各路大神指导讨论
分 -->
回复

使用道具 举报

千问 | 2021-1-27 05:08:47 | 显示全部楼层
我也遇到这问题一模一样,最后我强制不停地修改paint的矩形大小是可以进入paint函数的
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行