1

QGraphicsSceneaに追加しましたQGraphicsSimpleTextItemが、単純なテキストだけが現在の背景を読み取ることができません。そこで の背景色を設定したいのQGraphicsSimpleTextItemですが・・・その方法がありません。最も簡単な解決策は何ですか?

4

3 に答える 3

5

最も簡単な解決策は、QGraphicsTextItem代わりに使用してコンストラクターQGraphicsSimpleTextIemを呼び出すsetHtml()ことです。たとえば、次のようになります。

this->setHtml(QString("<div style='background-color: #ffff00;'>") + text + "</div>");
于 2012-08-04T11:12:56.563 に答える
4

シーン全体の背景を変更するには:

myScene->setBackgroundBrush( Qt::red );

または、テキスト項目だけの背景を変更したい場合は、おそらくメソッドをサブクラス化QGraphicsSimpleTextItemしてオーバーライドする必要がありますpaint()

class MyTextItem : public QGraphicsSimpleTextIem {
    public:
        void paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=0 )
        {
            painter->setBrush( Qt::red );
            painter->drawRect( boundingRect() );
            QGraphicsSimpleTextItem::paint( painter, option, widget );
        }
于 2012-07-26T14:55:53.273 に答える
-1

背景色にアクセスする方法は次のとおりです。

QPalette currentPalette = myGraphicScene.palette();

// Set a new color for the background, use QPalette::Window
// as QPalette::Background is obsolete.
currentPalette.setColor( QPalette::Window, Qt::red );

// Set the palette.
myGraphicScene.setPalette( currentPalette );
于 2012-07-26T14:52:01.017 に答える