qt – 如何在QGraphicsItem中包装文本?

qt – 如何在QGraphicsItem中包装文本?,第1张

概述1)如何在QGraphicsTextItem中包装文本以适合具有宽度和高度的固定矩形? 现在我正在尝试创建一个文本,获取它的边界矩形,并调整它以适应盒子 – 但我不能得到包装. class TTT: public QGraphicsTextItem { TTT() { { setPlainText("abcd"); qreal x = m_itemS 1)如何在qgraphicstextitem中包装文本以适合具有宽度和高度的固定矩形?

现在我正在尝试创建一个文本,获取它的边界矩形,并调整它以适应盒子 – 但我不能得到包装.

class TTT: public qgraphicstextitem {    TTT() {    {        setPlainText("abcd");        qreal x = m_itemSize.wIDth()/boundingRect().wIDth();        qreal y = m_itemSize.height()/boundingRect().height();        scale(x,y);    }    voID paint(QPainter* painter,const qstyleOptionGraphicsItem* option,QWidget* Widget) {        // experiment with clip regions        // text gets covered by hole in clip        QRegion r0(boundingRect().toRect());        QRegion r1(QRect(5,5,10,10),QRegion::Ellipse);        QRegion r2 = r0.subtracted(r1);        painter->setClipRegion(r2);        painter->setBrush(Qt::yellow);        painter->drawRect(boundingRect());        qgraphicstextitem::paint(painter,option,Widget);    }}

是什么让包装发生,怎么才能触发呢?

现在,当我一直打字时,该框会自动扩展.

2)是否可以将文本包装在不是矩形的QGraphicsItem / QGraphicTextItem子类中?

(如上图所示)
我试着使用clipRegion,看看上面的代码,但我想这不是正确的方法,剪切剪切文本但没有包装.

也许它会…如果我能弄清楚如何首先包装文本?

Qt 4.8

解决方法 你没有指定Qt版本,但尝试:

voID qgraphicstextitem::setTextWIDth(qreal wIDth)

Sets the preferred wIDth for the item’s text. If the actual text is wIDer than >the specifIEd wIDth then it will be broken into multiple lines.

If wIDth is set to -1 then the text will not be broken into multiple lines >unless it is enforced through an explicit line break or a new paragraph.

The default value is -1.

总结

以上是内存溢出为你收集整理的qt – 如何在QGraphicsItem中包装文本?全部内容,希望文章能够帮你解决qt – 如何在QGraphicsItem中包装文本?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1080452.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存