“Not so basic” drawing application
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?
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.
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?
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?
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.
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.
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…
Yes, it only uses an outline pen, it will work for the image posted above but it wont work for something like this:
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:
- painter.drawLine(lastPoint, endPoint);
And in fact, my very first post states that I failed to find what I need in the examples ;)