有没有办法让drawText()更新QPicture的边界矩形?

发布于 2024-07-18 05:39:18 字数 673 浏览 2 评论 0原文

QPicture 上绘图应该更新其边界矩形。 像这样:

>>> picture = QPicture()
>>> painter = QPainter(picture)
>>> picture.boundingRect()
QRect(0,0,0,0)
>>> painter.drawRect(20,20,50,50)
>>> picture.boundingRect()
QRect(20,20,50,50)

但是如果我在上面绘制文本,则边界矩形不会更新:

>>> picture = QPicture()
>>> painter = QPainter(picture)
>>> picture.boundingRect()
QRect(0,0,0,0)
>>> painter.drawText(10,10, "Hello, World!")
>>> picture.boundingRect()
QRect(0,0,0,0)

显然,它不会更新边界矩形。

有没有办法让它尊重绘制的文本,还是我必须手动完成? (不太难,但我希望 Qt 能在这里帮助我。)

Drawing on a QPicture should update its bounding rect. Like this:

>>> picture = QPicture()
>>> painter = QPainter(picture)
>>> picture.boundingRect()
QRect(0,0,0,0)
>>> painter.drawRect(20,20,50,50)
>>> picture.boundingRect()
QRect(20,20,50,50)

But if I draw text on it, the bounding rect isn't updated:

>>> picture = QPicture()
>>> painter = QPainter(picture)
>>> picture.boundingRect()
QRect(0,0,0,0)
>>> painter.drawText(10,10, "Hello, World!")
>>> picture.boundingRect()
QRect(0,0,0,0)

Obviously, it doesn't update the bounding rect.

Is there a way to make it repsect drawn text or do I have to do it manually? (Not too hard, but I hope that Qt can assist me here.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仙女山的月亮 2024-07-25 05:39:18

看一下这些重载方法,您必须在文本参数之后指定边界矩形(这显然与第一个参数位置的矩形不同):

在范围内绘制给定的文本
根据提供的矩形
指定的标志。 边界矩形(如果
not null) 设置为
边界矩形应该是有序的
将整个文本括起来。

QPainter.drawText (1), QPainter.drawText (2)

更新:

它出现如果你想提前为drawText()方法生成一个边界矩形,你只需调用QPainter上的boundingRect()方法,它会执行以下操作:

返回边界矩形
绘制时出现的文本
在给定的矩形内
使用当前指定的标志
设置字体(); 即函数告诉你
其中drawText()函数将
当给出相同的参数时进行绘制。

如果文本不适合
使用指定的给定矩形
标志,该函数返回
所需的矩形。

QPainter.boundingRect

我用 QRectF 输出链接到 BoundingRect,但信息也适用于其他版本。

所以基本上,将 QPainter.boundingRect() 的结果传递到 QPainter.drawText() 方法的boundingRect 参数(第二个 QRect 参数)。

更新2:

我为自己的言论深表歉意。 我忘记了drawText在PyQt中的工作方式与在Qt中的工作方式不同。 边界矩形由drawText函数返回(不像Qt那样传入),此外,您必须在获得返回给您的边界矩形之前指定对齐标志。 (我什至按照 Aaron Digulla 的评论包含了 p.end()):

pic = Qt.QPicture()
p = QtGui.QPainter(pic)
brect = p.drawText(10,10,200,200, QtCore.Qt.AlignCenter, "blah")
p.end()
print brect
print pic.boundingRect()

这是输出:

PyQt4.QtCore.QRect(100, 103, 20, 14)

PyQt4.QtCore.QRect(0, 0, 0, 0)

所以看起来你必须自己设置边界矩形,尽管至少它是由传入标志时,drawText() 方法的输出。

这似乎不是理想的行为,您必须自己设置边界矩形。 我希望其他人能找到您正在寻找的答案,但我怀疑您可能想报告此错误。

Take a look at these overload methods, where you must specify the Bounding Rectangle after the text parameter (which is apparently different than the rectangle in the first argument's position):

Draws the given text within the
provided rectangle according to the
specified flags. The boundingRect (if
not null) is set to the what the
bounding rectangle should be in order
to enclose the whole text.

QPainter.drawText (1), QPainter.drawText (2)

Update:

It appears if you want to generate a bounding rectangle for the drawText() method in advance, you just call the boundingRect() method on QPainter, which does the following:

Returns the bounding rectangle of the
text as it will appear when drawn
inside the given rectangle with the
specified flags using the currently
set font(); i.e the function tells you
where the drawText() function will
draw when given the same arguments.

If the text does not fit within the
given rectangle using the specified
flags, the function returns the
required rectangle.

QPainter.boundingRect

I linked to BoundingRect with QRectF output, but the information applies to the other versions as well.

So basically, pass the result of QPainter.boundingRect() into the boundingRect parameter of the QPainter.drawText() method (the second QRect argument).

Update 2:

I APOLOGIZE PROFUSELY for being so damn dense. I forgot that drawText works differently in PyQt than in Qt. The bounding rectangle is RETURNED by the drawText function (not passed in like Qt) and in addition, you have to specify alignment flags before you get a bounding rectangle given back to you. (I even included the p.end() as per Aaron Digulla's comment):

pic = Qt.QPicture()
p = QtGui.QPainter(pic)
brect = p.drawText(10,10,200,200, QtCore.Qt.AlignCenter, "blah")
p.end()
print brect
print pic.boundingRect()

Here is the output:

PyQt4.QtCore.QRect(100, 103, 20, 14)

PyQt4.QtCore.QRect(0, 0, 0, 0)

So it appears you will have to set the bounding rectangle yourself, though at least it is returned to you by the output of the drawText() method when passing in flags.

This does not seem like ideal behaviour, that you would have to set the bounding rectangle yourself. I hope someone else has the answer you're looking for, but I suspect you may want to report this bug.

流年已逝 2024-07-25 05:39:18

绘画不会改变 Qt 中物体的大小。 主要原因是这样的:

  • 组件必须自己绘制
  • 绘制触发调整大小
  • 调整大小触发绘制 -> 无限循环

因此调整大小必须在布局阶段进行。 此后,界限不应改变。

要解决您的问题,请使用 QFontMetric 计算出在构建图片期间或接近构建图片时文本的大小,然后相应地调整其大小。

[编辑] 嗯...在请求边界矩形之前尝试调用 end() 。 如果有效,您就发现了一个错误(看不到添加元素时边界矩形不应该存在的原因......)

Painting doesn't change the size of something in Qt. The main reason is this:

  • A component has to paint itself
  • The paint triggers a resize
  • The resize triggers painting -> endless loop

So the resize has to happen during the layout phase. After that, the bounds should not change.

To solve your problem, use QFontMetric to figure out how big your text is going to be during or close to the construction of your picture and then resize it accordingly.

[EDIT] Hm ... try to call end() before requesting the bounding rect. If that works, you've found a bug (can't see a reason why the bounding rect should not exist as you add elements...)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文