Home > Back-end >  On the graph are qquickpainteditem type problems
On the graph are qquickpainteditem type problems

Time:10-10

I want to use qquickpainteditem class to draw graphics in QML, have implemented before drawing function

This time I slavish write again, I found it won't work, tried a lot of ways how to show never call paint () function,
Think because it is inherited qquickitem, I will rewrite updatepaintnode (), found that he had a call this function, rather than the I want to paint (), it makes me very strange

The code
ROI. H:
 
The class rectRoi: public QQuickPaintedItem
{
Q_OBJECT
Public:
RectRoi (QQuickItem * parent=0);
Virtual ~ rectRoi Q_DECL_OVERRIDE ();
Protected:
Void paint (QPainter * painter) Q_DECL_OVERRIDE;
QRectF boundingRect () const Q_DECL_OVERRIDE;
QSGNode * updatePaintNode (QSGNode * Node, UpdatePaintNodeData *) override.
Private:
QRectF the Rect, mBoundingRect;
QPen PointPen LinePen;
Void setRect (const int & amp; X, const int & amp; Y, const int & amp; W, const int & amp; 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 (300200200100);
//setFlag (QQuickItem: : ItemHasContents);
}

RectRoi: : ~ rectRoi ()
{

}

QSGNode * rectRoi: : updatePaintNode (QSGNode * Node, UpdatePaintNodeData *)
{
QDebug () & lt; <"UpdatePaintNode";
Return the Node;
}

Void rectRoi: : paint (QPainter * painter)
{
QDebug () & lt; & lt;" Paint ";

Painter - & gt; SetRenderHint (QPainter: : Antialiasing, true);

Painter - & gt; SetPen (PointPen);
Painter - & gt; DrawPoint (the Rect topLeft ());
Painter - & gt; DrawPoint (the Rect topRight ());
Painter - & gt; DrawPoint (the Rect bottomLeft ());
Painter - & gt; DrawPoint (the Rect bottomRight ());

Painter - & gt; SetPen (LinePen);
Painter - & gt; DrawLine (the Rect. TopLeft (), the Rect topRight ());
Painter - & gt; DrawLine (the Rect. TopRight (), the Rect bottomRight ());
Painter - & gt; DrawLine (the Rect. BottomRight (), the Rect bottomLeft ());
Painter - & gt; DrawLine (the Rect. BottomLeft (), the Rect topLeft ());
}

Const QRectF rectRoi: : boundingRect ()
{
Return mBoundingRect;
}

Void rectRoi: : setRect (const int & amp; X, const int & amp; Y, const int & amp; W, const int & amp; H)
{
The Rect=QRectF (x, y, w, h);
MBoundingRect. Your setX (the Rect. X () - the Rect. Width ()/2);
MBoundingRect. SetY (the Rect. Y () - the Rect. Height ()/2);
MBoundingRect. SetWidth (the Rect. Width ());
MBoundingRect. SetHeight (the Rect. Height ());
QDebug () & lt; & lt;" BoundingRect: "& lt; The update ();
QDebug () & lt; & lt;" SetRect ";
}


Welcome each great spirit to guide discussion

CodePudding user response:

I also encountered the same problem, finally I forced to modify the rectangle size is a function can enter the paint of paint
  • Related