How to make qt qgraphicsview scale to not affect stipple pattern?
Asked Answered
D

3

6

I draw few rectangles inside the QGraphicsView ; I use custom stipple pattern for these by creating a QBrush with my QPixmap. This gets displayed with the default zoom level as expected.

When I call view->scale(), the rectangles show up bigger or smaller as I expected. However Qt has scaled the individual bits of the stipple pattern which is not expected; I expected it to draw the larger or smaller rectangle again with the brush. Eg. If I had used a stipple pattern with one pixel dot and pixel space, after zooming in, I want to see a larger rectangle but I want the same stipple pattern with same pixel gaps. Is this achievable somehow? Thanks.

Diatomite answered 19/12, 2012 at 18:4 Comment(0)
M
6

I ran into the same problem while developing an EDA tool companion in Qt.

After some trying, what I did (and seems to work for me) is to create a custom graphics item. On the paint method, I do:

QBrush newBrush = brush_with_pattern;
newBrush.setTransform(QTransform(painter->worldTransform().inverted()));
painter->setBrush(newBrush);

That is to apply the inverse transformation of the item to the brush (so it does not scale).

I think that the setDashOffset is only for the border of the shapes (not the fill).

Masturbation answered 8/1, 2013 at 13:56 Comment(1)
Would it not do same to set ItemIgnoresTransformations?Stcyr
J
0

You may use QPen::setDashOffset:

http://harmattan-dev.nokia.com/docs/library/html/qt4/qpen.html#setDashOffset

You'll need to set the offset based on the scenes zoom/scale level. You can grab a pointer to the scene in your item by calling scene(), don't forget to check for NULL though since it will be NULL when not added to the scene (although you shouldn't in theory get a paint() when not in a scene).

The other option is to use:

http://doc.qt.digia.com/qt/qpainter.html#scale

To undo the views scaling on your painter.

Joon answered 19/12, 2012 at 18:58 Comment(3)
Thanks for reply. I think I had not explained well. I want the size of the rectangle to scale but the stipple pattern to remain same.Diatomite
using setDashOffset should achieve that result, for example at 100% the dash offset might be 1, so at 50% you can change it 0.5 and the pattern should look the same (but the rect will be twice as small).Joon
I am using QBrush to set the stipple pattern. I was looking for a *Offset method in the brush. Could not see.Diatomite
P
0

In case anyone is still looking on this, a related question here regarding scaling of standard fill patterns instead of pixmap fill patterns may help. Basically, it may not be possible to modify scaling of standard fill patterns (a few workaround ideas are listed), but, working with alpha values instead gives the desired effect if you are looking for varying colors, especially gray levels - and is much less convoluted.

Progenitor answered 9/1, 2017 at 16:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.