[英]How do I determine the boundingRect() of a QGraphicsTextItem based on its contents?
I am trying to subclass some QGraphicsItem
classes: 我试图子类化一些
QGraphicsItem
类:
for QGraphicsTextItem
how do I determine the boundingRect()
and shape()
? 为
QGraphicsTextItem
如何确定boundingRect()
和shape()
?
I am trying to create a boundingRect
out of textWidth()
and... ? 我试图用
textWidth()
和...创建一个boundingRect
?
apparently, in the paint(...) I also have to specify the rectangle I must draw in... and I thought I solved it simply by using 显然,在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());
}
I need to get the boundingRect()
out of the actual text - is that possible or already implemented ? 我需要从实际文本中获取
boundingRect()
-是否可能或已经实现?
QGraphicsTextItem
? QGraphicsTextItem
? I don't see that you are adding any kind of functionality which is not already done in QGraphicsTextItem
. QGraphicsTextItem
尚未完成的任何功能。 boundingRect
has to return smallest possible rectangle which contains whole drawing content of the graphics item. boundingRect
必须返回最小的矩形,其中包含图形项的整个绘图内容。 If this value is incorrect you may see some painting artifacts.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.