Reputation: 105
Is there a way to easily add padding to a QPainter area?
The whole idea is to have a border, within the drawable area, where I can't draw so that when I draw a line from (0, 0) to (10, 10) I'm actually drawing at (0 + padding, 0 + padding) to (10 + padding, 10 + padding). The padding border should be seen though.
Assuming a QPainter
is created as:
QPainter painter(aWidget); // aWIdget is a Widget*
and a padding
integer variable. Now let's consider the area that is drawable of the widget as "A". How can I get to have a drawable area "B", so that B has:
B_width = A_width - 2 * padding;
B_height = A_height - 2 * padding;
and what would have been at QPoint(padding, padding) in A would now be at QPoint(0, 0) in B?
I began to implement it by my own (which is going to be painful), but I was wondering if there way an easier and "pre-made" way to do this in Qt? Maybe Transformations?
Thanks.
Upvotes: 1
Views: 1691
Reputation: 112
You can set the window area too. If you take a look in documentation of QPainter, you will se two interesting methods: setWindow (which you can transform your printable area into custom coordinates) and setViewport (which you can use to limit your printable area to a given rect).
Upvotes: 1
Reputation: 560
Yes, doing a transform would be the best way. If you apply a transformation, then all subsequent draw calls will be transformed by that transform. For example applying a translation of (5,5) would make a line (0,0) to (10,0) become (5,5) to (15,5).
The QPainter documentation can be found here and if you look near the bottom you will see a translate
method. That's exactly what you're looking for.
painter.translate(5, 5); // that should do it
Edit: To allow the draw calls to only edit a specific part of the surface use QPainter's setClipRect method.
painter.setClipRect(5, 5, originalWidth - 5, originalHeight - 5);
Upvotes: 2