首页| 论坛| 消息

标题:Qpainter如何 绘制如下所示
作者:xing181512
日期:2021-01-04 17:25
内容:

注册:QT for
Test
使用QPainter 如何绘制如上所示,在一个固定的宽度区域 ,文字的背景色随文字增加/换行 而增加/换行


#1 [XChinux 01-04 20:45]
这个似乎难办到。
QFontMetrics通过这个类,可以获得你要绘制的文字到底要占多大的区域,可能能自己计算出来。
#2 [青空飞羽 01-04 21:06]
QLable有个wordWrap 属性,应该有用
wordWrap : bool
This property holds the label's word-wrapping policy
If this property is true then label text is wrapped where necessary at word-breaks; otherwise it is not wrapped at all.
By default, word wrap is disabled.
#3 [snow_man_0 01-05 02:35]
QPainter::drawText(const QRectF &rectangle, int flags, const QString &text, QRectF *boundingRect = nullptr)
flags: Qt::TextWordWrap
QFontMetrics算出rectangle,填充颜色就可以
#4 [lemonzll 01-05 08:59]
void Widget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
QTextDocument doc;
QColor textColor = "green";//文字颜色
doc.setDefaultStyleSheet(QStringLiteral("span { background-color: %1}")
.arg(textColor.name()));
doc.setHtml(tr("

Register: Qt for test Qt for test Qt for test Qt for test

"));
doc.setDocumentMargin(1);
doc.setTextWidth(this->width() / 2);//设置绘制区域宽度,这里例子是只有整体宽度一半
doc.drawContents(&painter);
}
#5 [xing181512 01-05 10:25]
厉害,可以实现

<< 1 2 >> (1/2)

回复 发表
主题 版块