簡體   English   中英

PYQT如何正確繪制QGraphicsTextItem

[英]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())

我可以畫QGraphicsTextItem。 但是問題是...。當我繪制它時,QGraphicsTextItem中的文本消失了。 我該如何解決?

通過覆蓋父QGraphicsTextItem類提供的paint方法,您將說您將不會使用它的文本繪制方式。 相反,您正在實現自己的文本繪畫。 但是您實際上並沒有繪制任何文字,因此屏幕上沒有文字出現。

如果要自己控制所有繪畫,則至少需要添加以下內容:

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

或者你也許可以打電話

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

在您自己畫的末尾,取決於您要尋找的效果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM