Question

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.

Was it helpful?

Solution

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).

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top