如何在PyQt中绘制自定义椭圆形?

3

我一直在尝试使用QGraphicsEllipseItem制作自定义椭圆形状。

阅读Qt官方文档有关QGraphicsEllipseItem的内容后,似乎并没有找到如何管理它。

这是自定义的椭圆形状:

enter image description here


这似乎不是“椭圆形”,更像是一个复杂的曲线。考虑到这一点,请提供您已经尝试过的[mre],因为现在您的问题太模糊了,我们不会回答广泛的方面。此外,请花些时间查看[tour]并阅读[ask]。 - musicamante
嗨@musicamante,感谢您的反馈。我已经阅读了您的建议,并会牢记指南。我没有提供代码,因为我的代码仅包含使用QGraphicsEllipseItem创建的形状,我认为这不是绘制复杂曲线的可能方法。 - jon_bovi
1个回答

2
如果您想要实现复杂的形状,那么可能的解决方案是使用QPainterPathItem:
from PyQt5.QtCore import QRectF
from PyQt5.QtGui import QColor, QPainterPath
from PyQt5.QtWidgets import (
    QApplication,
    QGraphicsPathItem,
    QGraphicsScene,
    QGraphicsView,
)


def main():
    app = QApplication([])

    radius = 20
    length = 100

    square = QRectF(0, 0, 2 * radius, 2 * radius)

    path = QPainterPath()
    path.moveTo(radius, 0)
    path.arcTo(square, 90, 180)
    path.lineTo(length, 2 * radius)
    square.moveRight(length + 2 * radius)
    path.arcTo(square, -90, 180)
    path.lineTo(radius, 0)

    item = QGraphicsPathItem()
    item.setBrush(QColor("red"))
    item.setPen(QColor("green"))
    item.setPath(path)

    scene = QGraphicsScene()
    view = QGraphicsView(scene)
    scene.addItem(item)
    view.show()

    app.exec_()


main()

嗨@eyllanesc,非常感谢您的帮助!您有没有任何建议可以告诉我在哪里可以找到更多的示例来学习QGraphicsPathItem?我已经阅读了官方的Qt文档,但仍然不是很清楚。再次感谢。 - jon_bovi
1
@husniandre 你应该查看 https://doc.qt.io/qt-5/qpainterpath.html - eyllanesc
我看到了你在许多PyQt问题上的回答,我相信你的工作对许多其他人有所帮助。非常感谢你的支持@eyllanesc! - jon_bovi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接