Shubham Saini
Shubham Saini

Reputation: 95

Getting the pixels index covered by qpen while using qpainter on qimage

I am using qt for developing a simple drawing application.

I have a qImage and filled it as trasparent.

QImage *m_markerImg = new QImage(400,320, QImage::Format_ARGB32_Premultiplied); m_markerImg -> fill( Qt::transparent );

I have created a custom graphics scene by inheriting it from qgraphicsscene & drawing a line on this image in the mousemove event of graphics scene as:

QPointF plotPoint = mouseEvent->scenePos(); m_painter.drawLine(m_initPoint,plotPoint); m_initPoint=plotPoint;

where m_initPoint is being assigned in mouse press event. Everything is working fine and i am able to draw lines over this image. Now i want to store the pixels covered by this line at runtime i.e. during line draw. Although i can store the points on which i am drawing i.e. m_initPoint & plotPoint but in case of penwidth is set to more than 1 , then i will get only a single line pixel while i need whole of the pixels covered by the width of this line.

How can i get that?

Upvotes: 1

Views: 520

Answers (1)

You need to:

  1. Convert the line to a path,
  2. Stroke the path using QPainterPathStroker (see also this example),
  3. Get the stroked path as a polygon,
  4. Scan (iterate) the pixels of the polygon - see this question for a complete example.

Upvotes: 0

Related Questions