我试图将一些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()
- 是可能的还是已经实现的?