3

So I have been trying to make a custom oval shape using QGraphicsEllipseItem.

Upon reading the Qt's official documentation regarding QGraphicsEllipseItem, I didn't seem to find out how to manage it.

Here is the custom oval shape:

enter image description here

jon_bovi
  • 71
  • 3
  • 11
  • That doesn't seem an "oval shape", it's more like a complex curve. Considering this, please provide a [mre] of what you've tried so far, because right now your question is too vague, and we don't provide answer to broad aspects. Also please take your time to review the [tour] and read [ask]. – musicamante Sep 20 '21 at 06:23
  • Hi @musicamante thank you for your feedback. I've read your suggestion and I'll make sure to keep the guidelines in mind. I did not provide codes because my code contains only a shape made with QGraphicsEllipseItem, which I believe is not a possible approach to draw the complex curve. – jon_bovi Sep 20 '21 at 07:20

1 Answers1

2

If you want to implement complex shapes then a possible solution is to use 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
  • 235,170
  • 19
  • 170
  • 241
  • Hi @eyllanesc, thank you very much for your help! Do you have any suggestions on where can I find more examples to learn QGraphicsPathItem? I have read the official Qt documentation but still does not grasp all of it clearly. Thanks again. – jon_bovi Sep 20 '21 at 07:25
  • 1
    @husniandre You should check https://doc.qt.io/qt-5/qpainterpath.html – eyllanesc Sep 20 '21 at 07:28
  • I've seen your answers on a lot of PyQt questions and I believe your work helps a lot of other people.Thanks a bunch for your support @eyllanesc! – jon_bovi Sep 20 '21 at 07:31