Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw and fill a triangle with QPainter?

Tags:

qt

qpainter

This is what I tried, it gave me no output. Where am I going wrong?

      // 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 have just tried to create a path between these 3 points and fill the area, but there is no output shown.

like image 952
Aquarius_Girl Avatar asked Oct 14 '13 09:10

Aquarius_Girl


Video Answer


3 Answers

If you want 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")));
like image 193
Sergio Cabral Avatar answered Sep 28 '22 16:09

Sergio Cabral


I think you do not need to call moveTo() function after you call lineTo() because the current position already updated to the the end point of the line you draw. 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")));
like image 25
vahancho Avatar answered Sep 28 '22 16:09

vahancho


The documentation says: "Moving the current point will also start a new subpath (implicitly closing the previously current path when the new one is started)".

This means you should once move to the origin of the path, then use only lineTo in order to draw the shape to be filled.

I added this answer because the answer "I think you do not need to call moveTo() function after you call lineTo() because the current position already updated to the the end point of the line you draw." is quite misleading. The moveTo is not unnecessary, it's actually causing the problem.

like image 38
pi3 Avatar answered Sep 28 '22 15:09

pi3