1

オブジェクトに背景色を追加するQGraphicsTextItem方法はありますか?

を作成しましたがQGraphicsScene、その中にテキストを表示する必要があります。私はQGraphicsTextItem仕事をするために作成しました。ただし、背景に対してはあまり明確ではないため、強調表示するか、背景色を設定して見やすくすることを検討しています。ただし、これを行うドキュメントには何も見つかりませんでした。

より良いオプションが利用可能な場合、これを長い間行うことは避けたいと思います。回答ありがとうございます。

4

2 に答える 2

2

いくつかのオプションがあります。

最も簡単なのは、目的のスタイルで HTML を設定することです。

htmlItem = QtGui.QGraphicsTextItem()
htmlItem.setHtml('<div style="background:#ff8800;">html item</p>')

別のアプローチは、 をサブクラス化し、メソッドQGraphicsTextItemでカスタム背景を実行するpaintことです。

class MyTextItem(QtGui.QGraphicsTextItem):
    def __init__(self, text, background, parent=None):
        super(MyTextItem, self).__init__(parent)
        self.setPlainText(text)
        self.background = background

    def paint(self, painter, option, widget):
        # paint the background
        painter.fillRect(option.rect, QtGui.QColor(self.background))

        # paint the normal TextItem with the default 'paint' method
        super(MyTextItem, self).paint(painter, option, widget)

両方を示す基本的な例を次に示します。

import sys
from PySide import QtGui, QtCore

class MyTextItem(QtGui.QGraphicsTextItem):
    def __init__(self, text, background, parent=None):
        super(MyTextItem, self).__init__(parent)
        self.setPlainText(text)
        self.background = background

    def paint(self, painter, option, widget):
        # paint the background
        painter.fillRect(option.rect, QtGui.QColor(self.background))

        # paint the normal TextItem with the default 'paint' method
        super(MyTextItem, self).paint(painter, option, widget)

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)

    w = QtGui.QGraphicsView()
    s = QtGui.QGraphicsScene()

    htmlItem = QtGui.QGraphicsTextItem()
    htmlItem.setHtml('<div style="background:#ff8800;">html item</p>')

    myItem = MyTextItem('my item', '#0088ff')

    s.addItem(htmlItem)
    myItem.setPos(0, 30)
    s.addItem(myItem)
    w.setScene(s)
    w.show()

    sys.exit(app.exec_())
于 2013-01-14T20:27:18.753 に答える
0

これを試して:

item = QtGui.QGraphicsTextItem()
item.setFormatTextColor("#value")
于 2013-01-14T03:36:16.007 に答える