简体   繁体   English

如何根据其内容确定QGraphicsTextItem的boundingRect()?

[英]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() -是否可能或已经实现?

  1. why you are subclassing QGraphicsTextItem ? 为什么要子类化QGraphicsTextItem I don't see that you are adding any kind of functionality which is not already done in QGraphicsTextItem . 我看不到您要添加QGraphicsTextItem尚未完成的任何功能。
  2. You have quite nice SO reputation but you ask question like a newbie. 您的SO名声很好,但是您像新手一样问问题。 First describe what is your primary goal! 首先描述一下您的主要目标! The way you are trying to solve this is secondary thing! 您尝试解决此问题的方法是次要的事情!
  3. read documentation. 阅读文档。 It quite good. 很好 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. 如果该值不正确,您可能会看到一些绘画瑕疵。 It is always implemented. 它总是被实施。
  4. it is qiute possible that if you will read documentation of it will turn out that you don't have to subclass anything. 如果您将阅读文档,可能会发现您不必继承任何子类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的场景中只显示了我的 QGraphicsTextItem 的一部分。 为了解决这个问题,我想改变 boundingRect 的大小 - Only part of my QGraphicsTextItem is shown in my scene. To solve the problem i want to change the size of the boundingRect 如何删除QPlainTextEdit及其内容之间的空间 - How do I remove the space between QPlainTextEdit and its contents 销毁向量时如何释放其内容? - How do I deallocate contents of a vector upon its destruction? 如何根据位置删除列表中的特定项目 - How do I remove a specific item in a list based on its position 在Qt中,如何使对话框不可调整大小,而又自动根据内容调整其大小? - In Qt, how do I make a dialog un-resizeable, yet automatically adjusting its size to the contents? 如何在CGI应用程序中解析HTML文件并修改其内容? C ++ - How do I Parse an HTML file in CGI application and modify its contents? C++ 如何使链接列表按字母顺序打印出其内容? - How do i make a link list print out its contents in alphabetical order? 按下触摸屏会触发 mousePressEvent,我如何确定是鼠标点击还是触摸屏点击? - Pressing touchscreen triggers mousePressEvent, how do I determine if its a mouse click or a touchscreen click? 如何使boundingRect()呈弧形? - How to make boundingRect() in an arc shape? 如何使QGraphicsTextItem单行? - How to make QGraphicsTextItem single line?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM