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

时间:2021-11-18 19:32:06

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

在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)

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.)

有没有办法让它重新绘制绘制文本或我必须手动执行? (不太难,但我希望Qt可以在这里帮助我。)

2 个解决方案

#1


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):

看一下这些重载方法,你必须在text参数之后指定Bounding Rectangle(它明显不同于第一个参数位置的矩形):

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.

根据指定的标志在提供的矩形内绘制给定文本。 boundingRect(如果不为null)设置为边界矩形应该是什么,以包围整个文本。

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

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:

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

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.

返回文本的边界矩形,当使用当前设置的字体()在指定的标志内绘制时,将显示该文本的边界矩形;即该函数告诉您在给定相同参数时drawText()函数将在何处绘制。

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.

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

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

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

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):

因为这么该死的密集而我非常友好。我忘了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()

Here is the output:

这是输出:

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

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

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

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.

因此,您似乎必须自己设置边界矩形,但至少在传入标记时由drawText()方法的输出返回给您。

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.

这似乎不是理想的行为,您必须自己设置边界矩形。我希望其他人有你想要的答案,但我怀疑你可能想报告这个bug。

#2


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

绘画不会改变Qt中某些东西的大小。主要原因是:

  • 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.

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

[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...)

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

#1


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):

看一下这些重载方法,你必须在text参数之后指定Bounding Rectangle(它明显不同于第一个参数位置的矩形):

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.

根据指定的标志在提供的矩形内绘制给定文本。 boundingRect(如果不为null)设置为边界矩形应该是什么,以包围整个文本。

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

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:

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

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.

返回文本的边界矩形,当使用当前设置的字体()在指定的标志内绘制时,将显示该文本的边界矩形;即该函数告诉您在给定相同参数时drawText()函数将在何处绘制。

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.

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

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

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

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):

因为这么该死的密集而我非常友好。我忘了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()

Here is the output:

这是输出:

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

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

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

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.

因此,您似乎必须自己设置边界矩形,但至少在传入标记时由drawText()方法的输出返回给您。

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.

这似乎不是理想的行为,您必须自己设置边界矩形。我希望其他人有你想要的答案,但我怀疑你可能想报告这个bug。

#2


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

绘画不会改变Qt中某些东西的大小。主要原因是:

  • 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.

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

[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...)

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