我想使用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()
欢迎各路大神指导讨论
分 -->
|