如何为QGraphicsSimpleTextItem(Qt C++)设置背景?
问题描述:
我已添加到我的QGraphicsScene
a QGraphicsSimpleTextItem
,但只是一个简单的文本是不可读的当前背景。因此,我想设置QGraphicsSimpleTextItem
的背景颜色,但是......没有这种方法。什么是最简单的解决方案?如何为QGraphicsSimpleTextItem(Qt C++)设置背景?
答
看来,最简单的解决方案是使用QGraphicsTextItem
,而不是QGraphicsSimpleTextIem
,并呼吁setHtml()
在构造函数中,例如:
this->setHtml(QString("<div style='background-color: #ffff00;'>") + text + "</div>");
答
下面介绍如何访问背景颜色。
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);
+0
我想改变QGraphicsSimpleTextItem的背景,而不是整个场景。 – 2012-10-11 15:36:40
答
要改变你的整个场景的背景:
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);
}
你是不是想改变文本项目或背景你的整个场景? – Chris 2012-07-26 14:53:03
文本项目的背景。 – 2012-08-04 11:05:57