Important: Please read the Qt Code of Conduct -

[Solved] Adding text inside QGraphicsEllipseItem

  • Hi All,
    I have the following code which is generating 5 ellipses in a horizontal line :
    void Widget::createItem( int x, QGraphicsScene *scene )
    QRect rect(x+105, 50, 145, 100);
    ellipseItem = new QGraphicsEllipseItem( rect, 0, scene );

    QBrush brush;
    QPixmap pix(250,250);
    QPainter paint(&pix);
    paint.drawText(rect, Qt::AlignCenter ,"OK");


    Values of x = 0, 200, 400, 600, 800

    For the above code, the first instance works perfect i.e. "OK" is displayed in the center of the 1st QGraphicsEllipseItem but not for the others....what am i doing wrong?

    Please help.

  • No reply !!! Can anyone help me by sharing ideas on how to implement this - adding text inside QGraphicsEllipseItem.

  • Not sure what you are trying to accomplish, but if you want to create a graphics item that is displayed as an ellipse with text in it, this it definitely not the way to do it.

    You have to make that graphics item draw the text as well, you cannot just do that yourself when you create the item. To do that, you could subclass QGraphicsEllipseItem, reimplement the paint event, and paint the text in there (before or after you call the QGraphicsEllipseItem paint event).

Log in to reply