简体   繁体   English

PyQt 沿 QPainterPath 绘制 QPixmap

[英]PyQt drawing a QPixmap along QPainterPath

I want to draw patterns (preferable as QPixmap) along a QGraphicsPathItem inside my QGraphicsView.我想在我的 QGraphicsView 中沿着 QGraphicsPathItem 绘制模式(最好是 QPixmap)。 This should work similar to filling a QBrush with a texture by using QBrush.setTexture("example.png") .这应该类似于使用QBrush.setTexture("example.png")用纹理填充 QBrush。 Is there a way to do this on a QPen?有没有办法在 QPen 上做到这一点? I couldn't find anything related to this in the PyQt documentation.我在 PyQt 文档中找不到与此相关的任何内容。

The texture should be placed similar to this example picture纹理应与此示例图片类似

It doesn't matter if the texture keeps it's own width or is scaled to QPen's width.纹理是保持自己的宽度还是缩放到 QPen 的宽度都没有关系。

Is there some form of workaround to implement this?是否有某种形式的解决方法来实现这一点? I was thinking about using QTransform in a way to transform QPixmap to the shape of the desired QGraphicsPathItem.我正在考虑使用 QTransform 将 QPixmap 转换为所需的 QGraphicsPathItem 的形状。

I still haven't found a statisfying solution.我还没有找到一个令人满意的解决方案。 But for people that dont require the pattern to be aligned along the path, you can use this piece of code for your QPen:但是对于不需要沿路径对齐模式的人,您可以将这段代码用于您的 QPen:

pen = QPen()
patternBrush = QBrush(QPixmap('patter.png'))
pen.setBrush(patternBrush)
pen.setWidth(5)

It fills your QGraphicsPathItem like this它像这样填充您的 QGraphicsPathItem

I hope this is helpful for others.我希望这对其他人有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM