How to draw and fill a triangle using QPainter?

This is what I tried, it did not give me a result. Where am I mistaken?

// Start point of bottom line qreal startPointX1 = 600.0; qreal startPointY1 = 600.0; // End point of bottom line qreal endPointX1 = 600.0; qreal endPointY1 = 1200.0; // Start point of top line qreal startPointX2 = 600.0; qreal startPointY2 = 600.0; // End point of top line qreal endPointX2 = 800.0; qreal endPointY2 = 1200.0; QPainterPath path; // Set pen to this point. path.moveTo (startPointX1, startPointY1); // Draw line from pen point to this point. path.lineTo (endPointX1, endPointY1); path.moveTo (endPointX1, endPointY1); path.lineTo (endPointX2, endPointY2); path.moveTo (endPointX2, endPointY2); path.lineTo (startPointX1, startPointY1); painter.setPen (Qt :: NoPen); painter.fillPath (path, QBrush (QColor ("blue"))); 

I just tried to create a path between these three points and fill the area, but the result is not displayed.

+6
source share
3 answers

It seems to me that you do not need to call the moveTo() function after calling lineTo() , because the current position is already updated to the end point of the line you are drawing. Here is the code that draws a rectangle for me:

 // Start point of bottom line qreal startPointX1 = 600.0; qreal startPointY1 = 600.0; // End point of bottom line qreal endPointX1 = 600.0; qreal endPointY1 = 1200.0; // Start point of top line qreal startPointX2 = 600.0; qreal startPointY2 = 600.0; // End point of top line qreal endPointX2 = 800.0; qreal endPointY2 = 1200.0; QPainterPath path; // Set pen to this point. path.moveTo (startPointX1, startPointY1); // Draw line from pen point to this point. path.lineTo (endPointX1, endPointY1); //path.moveTo (endPointX1, endPointY1); // <- no need to move path.lineTo (endPointX2, endPointY2); //path.moveTo (endPointX2, endPointY2); // <- no need to move path.lineTo (startPointX1, startPointY1); painter.setPen (Qt :: NoPen); painter.fillPath (path, QBrush (QColor ("blue"))); 
+10
source

If you want to use QRectF

 QRectF rect = QRectF(0, 0, 100, 100); QPainterPath path; path.moveTo(rect.left() + (rect.width() / 2), rect.top()); path.lineTo(rect.bottomLeft()); path.lineTo(rect.bottomRight()); path.lineTo(rect.left() + (rect.width() / 2), rect.top()); painter.fillPath(path, QBrush(QColor ("blue"))); 
+5
source

The documentation says: "Moving the current point will also lead to the launch of a new subpath (implicit closing earlier than the current path when starting a new one)."

This means that you need to somehow go to the beginning of the path, and then use only lineTo to draw the shape you want to fill.

I added this answer because the answer "It seems to me that you do not need to call the moveTo () function after calling lineTo (), because the current position is already updated to the end point of the line you are drawing." quite misleading. MoveTo is not unnecessary, it actually causes a problem.

+1
source

Source: https://habr.com/ru/post/955898/


All Articles