Jason
Jason

Reputation: 75

PYQT How to paint QGraphicsTextItem properly

class TextItem(QGraphicsTextItem):

    def __init__(self,text):
        QGraphicsTextItem.__init__(self,text)
        self.text=text
        self.setFlag(QGraphicsItem.ItemIsMovable, True)
        self.setFlag(QGraphicsItem.ItemIsSelectable, True)
        self.setFlag(QGraphicsItem.ItemIsFocusable, True)



    def paint(self,painter,option,widget):
        self.pen = QPen()
        self.pen.setWidth(2)
        painter.setPen(self.pen)
        painter.drawRect(self.boundingRect())

I can paint QGraphicsTextItem. But the problem is.... Text in QGraphicsTextItem disappear when I paint it. How can I solve?

Upvotes: 0

Views: 1047

Answers (1)

strubbly
strubbly

Reputation: 3507

By overriding the paint method supplied by the parent QGraphicsTextItem class you are saying you aren't going to use how it paints its text. Instead you are implementing your own text painting. But then you don't actually paint any text and so no text appears on screen.

If you want to control all the painting yourself then you are going to need to add at least something like:

painter.drawText(self.boundingRect(),self.text)

Or you could perhaps call

QGraphicsTextItem.paint(self,painter,option,widget)

at the end of your own painting depending on the effect you are looking for.

Upvotes: 1

Related Questions