如何基于其内容确定QGraphicsTextItem的boundingRect()?

时间:2015-05-01 20:10:52

标签: c++ qt text qgraphicsitem

我试图将一些QGraphicsItem类子类化:

QGraphicsTextItem如何确定boundingRect()shape()

我正在尝试从boundingRect创建textWidth()并且......?

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

来解决它
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() - 是可能的还是已经实现的?

0 个答案:

没有答案