Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do chalk style drawing with Qt

Tags:

qt

drawing

I want to use Qt to draw lines in chalk style, as you typically see on a blackboard. Here is an example of what I have in mind:

Chalk style drawing sample image

What is the best way to achieve this rendering style? Do I need to draw a lot of little lines with a special brush, or is there a better way to get the "curvy" style you see in the sample image?

And where is the best place to integrate this? Theoretically it would be ideal to get this underneath QPainter, e.g. in a custom QPaintEngine, so that e.g. all the various QPainter::drawLine calls end up using the chalk style. However, it seems while the QPaintEngine interface looks perfect for this, the class itself isn't meant to be used for this purpose...

Thanks in advance for any help.

Greetings,

Fabian

like image 598
Fabian Avatar asked Oct 22 '22 03:10

Fabian


1 Answers

I have solved the problem in a different way. Using textured brushes didn't provide good results (maybe my fault). QGraphicsEffect was unfortunately not an option since my rendering is not based on QGraphicsView.

What I have done in the end:

  • Derived an own class from QPainter (i.e. ChalkPainter)
  • Added a new drawChalkLine() method to ChalkPainter. This method takes the passed line, splits it into smaller chunks and renders these chunks as bezier curves via QPainter::drawPath. For each bezier curve chunk I randomly shift the control point orthogonal to the line.
  • Next I added additional rendering methods to the ChalkPainter class, such as drawChalkRect(), all internally using the drawChalkLine() method.

This is not the most elegant method since I can't use the QPainter methods directly, but it provides good results for my purpose. Here is an example:

enter image description here

like image 165
Fabian Avatar answered Oct 27 '22 13:10

Fabian