March 1, 2012

Deleted Member # 269f Deleted Member # 269f
Lab Rat
251 posts

“Not so basic” drawing application

Page  
1

Hi there, I am looking for suggestions how to implement a basic drawing program that consists of a pixmap brush that leaves imprints of itself on another pixmap (the canvas) when the mouse key is pressed, at the appropriate location.

I tried to find something like that in the examples, but I didn’t find anything. Any suggestions?

Thanks!

23 replies

March 1, 2012

Jake007 Jake007
Robot Herder
248 posts

Finger Paint Example maybe?
You only use QMouseEvent instead of QTouchEvent.

Regards,
Jake

 Signature 

——————————————-
Code is poetry

March 1, 2012

Deleted Member # 269f Deleted Member # 269f
Lab Rat
251 posts

ah yes, I missed this one, will take a look

I actually did half the work already, but now I need to find a way to continiously do it while the mouse is pressed and moved, currently I only print the brush on the canvas in the location of click only upon mouse click.

March 1, 2012

Lukas Geyer Lukas Geyer
Dinosaur Breeder
2074 posts

Too late.

March 1, 2012

Deleted Member # 269f Deleted Member # 269f
Lab Rat
251 posts

You still have a chance to redeem yourself ;)

I have another problem – when I move the mouse faster there is nasty spacing in between the painting.

spacing

I suppose I can fix that if I increase the rate at which events are updated, since QWidget is optimized for basic GUI stuff it doesn’t update nearly as fast enough to produce smooth lines when the mouse is moved faster, a performance optimization that makes sense but not in the current case.

So how can I increase the frequency with which events are polled?

March 1, 2012

Eddy Eddy
Gene Splicer
1296 posts

have a look at the scribble example. It does what you want. at least when testing I couldn’t get your results , but maybe you are much faster than I am ;-)

 Signature 

Qt Certified Specialist
Qt Ambassador

March 1, 2012

Andre Andre
Area 51 Engineer
6079 posts

Why don’t you change the logic of the painting instead? It looks like now you are paiting a point whereever you find the mouse, but why don’t you draw a line between where your mouse last was and your mouse is now instead?

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

March 1, 2012

Deleted Member # 269f Deleted Member # 269f
Lab Rat
251 posts
Andre wrote:
Why don’t you change the logic of the painting instead? It looks like now you are paiting a point whereever you find the mouse, but why don’t you draw a line between where your mouse last was and your mouse is now instead?

Because this is not the way a drawing application works. If I wanted to just create lines, I would have done exactly that. I need drawing logic that can draw with different stencils / brushes much like photoshop, and in fact the user needs to be able to create his own custom brushes. The location of brushes must be randomized, the size – dynamic, brushes must rotate and all that stuff, scatter and so on, this cannot be achieved by drawing lines between locations on the screen.

There is nothing wrong with the logic of the painting as it is, I tested it with very big stencils and performance is great, the bottleneck is the update rate of the event loop.

How can I control it, so I can make it faster while drawing?

March 1, 2012

Andre Andre
Area 51 Engineer
6079 posts

You can not control it, that’s the point. Perhaps you should not use lines as such, but you should probably use the last and the current position of the mouse to do your rendering. You simply cannot rely on getting a mouse move event for every pixel on your screen you pass over.

You could perhaps use the moving speed in some way, using not only the last and the previous position, but also the time between these events. That would allow you to gauge if the user is very quick, or your system is just a bit slow. That might result in a different stroke in terms of your painting.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

March 1, 2012

Deleted Member # 269f Deleted Member # 269f
Lab Rat
251 posts

I see I will have to interpolate to get the in-between polling locations…

I noticed photoshop has an option on the brush called “spacing” and with it disabled, it behaves EXACTLY the same way as my test application. In fact the spacing in photoshop is even larger, probably because the application is heavy on the cpu.

March 1, 2012

Deleted Member # 269f Deleted Member # 269f
Lab Rat
251 posts

So, my next logical question is if there is a way to “draw a line” but not with a pen but by dragging a pixmap or a QBrush? I was just looking through the doc and all the methods for drawing lines involve QPen or a stroke which doesn’t seem to be able to consist of an image.

If not, I will have to do go back to the tedious interpolation…

March 1, 2012

Lukas Geyer Lukas Geyer
Dinosaur Breeder
2074 posts

Well, a QPen consists of a QBrush consists of a QPixmap.

March 1, 2012

Deleted Member # 269f Deleted Member # 269f
Lab Rat
251 posts

Yes, but it works nowhere near the way I need it to. Here is what it looks like:
wrong

I don’t need to fill the “volume” of the line with a pattern of the QBrush, I need to paint by dragging the brush for every pixel of the line. Notice the subtle difference? :)
right

March 1, 2012

Eddy Eddy
Gene Splicer
1296 posts

Have you looked at the scribble example?

 Signature 

Qt Certified Specialist
Qt Ambassador

March 1, 2012

Deleted Member # 269f Deleted Member # 269f
Lab Rat
251 posts

Yes, it only uses an outline pen, it will work for the image posted above but it wont work for something like this:

pixmap

The whole point is to not be restricted to a plain line (like scribble example is) but be able to paint by dragging the stencil you want to draw with.

Here is what scribble draws with:

  1. painter.setPen(QPen(myPenColor, myPenWidth, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
  2. painter.drawLine(lastPoint, endPoint);

And in fact, my very first post states that I failed to find what I need in the examples ;)

March 1, 2012

Andre Andre
Area 51 Engineer
6079 posts

Looks like this code will do exactly what you just showed above in your second example, and that you showed as an example of what you wanted…

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

Page  
1

  ‹‹ Accessing an item in QTreeWidget      QListView inside QTreeWidget ››

You must log in to post a reply. Not a member yet? Register here!