我想添加一个QGraphicsTextItem,我想改变背景的颜色.我的意思是我希望包含文本的boundingRect具有特定的颜色.一种方法是创建一个QGraphicsRectItem并将其放在文本的背面,但我想知道是否还有另一种方法可以做到这一点?
谢谢你的帮助!
我会子类QGraphicsTextItem,例如:
class QGraphicsTextItemWithBackgroundColorOfMyChoosing : public QGraphicsTextItem
{
public:
QGraphicsTextItemWithBackgroundColorOfMyChoosing(const QString &text) :
QGraphicsTextItem(text) { }
void paint( QPainter *painter, const QStyleOptionGraphicsItem *o, QWidget *w) {
painter->setBrush(Qt::red);
painter->drawRect(boundingRect());
QGraphicsTextItem::paint(painter, o, w);
}
};
Run Code Online (Sandbox Code Playgroud)
您可以使用setHtml()将 HTML 写入 QGraphicsTextItem ,因此您可以使用例如填充背景
item->setHtml("<div style='background-color:#666666;'>" + yourText + "</div>");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3760 次 |
| 最近记录: |