How to draw circle with line pattern using QGraphicsScene

  • Hi there,
    I'm using QGraphicsScene and don't know how to draw a colored circle with horizantal line, vertical line, or other patern in a given color inside. The background of GraphicsView should be in a different color from circle and lines. Something like the image below.


    I need to change the lines' thickness and color, and color of the circle.
    Thank you.

  • Moderators

    • Draw a circle using a QGraphicsEllipseItem
    • Draw a line using a QGraphicsRectItem
    • You can enter the dimensions of the circle/line into their constructors, or change the dimensions by calling setRect()
    • Set the circle/line colour using setBrush()

    Most importantly, set the circle as the parent of the line and set the QGraphicsItem::ItemClipsChildrenToShape flag. This puts the line inside the circle, and clips the line to the circle's perimeter.

    const int DIAMETER = 100;
    auto circle = new QGraphicsEllipseItem(0, 0, DIAMETER, DIAMETER);
    circle->setFlag(QGraphicsItem::ItemClipsChildrenToShape, true);
    const int WIDTH = 10;
    const int LENGTH = DIAMETER;
    auto line = new QGraphicsRectItem(45, 0, WIDTH, LENGTH);
    auto scene = new QGraphicsScene;

  • It just works for me very well. I just used QGraphicsLineItem instead QGraphicsRectItem.
    Thank you in advance

Log in to reply