如何根据其内容来确定Qgraphicstextitem的界限()

How do I determine the boundingRect() of a QGraphicsTextItem based on its contents?

本文关键字:Qgraphicstextitem 界限 何根      更新时间:2023-10-16

我正在尝试为某些QGraphicsItem类分类:

对于QGraphicsTextItem,我如何确定boundingRect()shape()

我正在尝试从textWidth()和...?

创建boundingRect

显然,在油漆(...)中,我还必须指定我必须绘制的矩形...我以为我只是通过使用

来解决它
QRectF TextItem::boundingRect() const
{
    qreal w = textWidth(); qreal h = 1000;  // h = ?
//    QRectF rect(QGraphicsTextItem::boundingRect());  // this leads to crash, maybe undefined ?
    QRectF rect(0,0,w,h)
    return rect;
}
void TextItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/)
{
    setDefaultTextColor(m_color);
    setPlainText("Hello world");
    QFont f;
    f.setPointSize(200); // calculated
    f.setBold(true);
    painter->setFont(f); // which of this and the next I need...
    setFont(f);          // really seems one of this is not needed
    painter->drawText(QGraphicsTextItem::boundingRect(), Qt::AlignCenter, this->toPlainText());
}

我需要从实际文本中获取boundingRect() - 是可能的还是已经实现的?

  1. 为什么您要亚分 QGraphicsTextItem?我看不到您添加了QGraphicsTextItem中尚未完成的任何功能。
  2. 您的声誉非常好,但是您像新手一样问问题。首先描述您的主要目标是什么!您试图解决这是次要的事情!
  3. 阅读文档。很好。boundingRect必须返回最小的矩形,其中包含图形项目的整个图形内容。如果此值不正确,您可能会看到一些绘画文物。它总是实现的。
  4. Qiute可能会读取文档,结果您不必为任何内容进行任何子。
相关文章:
  • 没有找到相关文章