如何设置QGraphicsSimpleTextItem(QtC++)的背景
How to set background for QGraphicsSimpleTextItem (Qt C++)?
我在QGraphicsScene
中添加了一个QGraphicsSimpleTextItem
,但只有一个简单的文本无法读取当前背景。所以我想设置QGraphicsSimpleTextItem
的背景色,但是。。。没有这样的方法。最简单的解决方案是什么?
似乎最简单的解决方案是使用QGraphicsTextItem
而不是QGraphicsSimpleTextIem
,并在构造函数中调用setHtml()
,例如:
this->setHtml(QString("<div style='background-color: #ffff00;'>") + text + "</div>");
要更改整个场景的背景:
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 );
}
以下是如何访问背景颜色。
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 );
相关文章:
- 如何在全屏模式下(在OpenGL中)使背景透明
- 使用窗口中的QSlider更改背景音量
- SDL2 透明背景
- 在 Visual C++ 中以编程方式更改按钮的背景图像
- 多重采样背景不显示
- 直接2D用WM_ERASEBKGND擦除背景?
- 在特定时间内更改 QLabel 的背景颜色
- 如何设置与其背景图像大小相对应的窗口大小?
- 更改包含 QButton 的 Qlayout 的背景颜色
- 如何为使用创建窗口创建的窗口创建自定义背景颜色
- 删除K的背景掩码-意味着Python或C++中的集群/
- SystemParametersInfo将背景设置为纯色,而不是实际设置图片
- 背景任务UWP C++只触发一次
- 我正在尝试更改背景的颜色,但它不起作用
- 在 C 中获取背景外壳命令的 PID
- 如何在QT中为QInputDialog输入密码时设置背景非活动和灰色?
- MFC 更改 CListCtrl 背景颜色
- 如何仅设置 QTabWidget 背景颜色样式表
- Qt为所有对话框设置通用背景
- 如何设置QGraphicsSimpleTextItem(QtC++)的背景