How to make qt qgraphicsview scale to not affect stipple pattern?

梦想的初衷 提交于 2019-12-05 06:01:30

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

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.

Tom Grundy

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!