有没有办法让drawText()更新QPicture的边界矩形?
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看一下这些重载方法,您必须在文本参数之后指定边界矩形(这显然与第一个参数位置的矩形不同):
QPainter.drawText (1), QPainter.drawText (2)
更新:
它出现如果你想提前为drawText()方法生成一个边界矩形,你只需调用QPainter上的boundingRect()方法,它会执行以下操作:
QPainter.boundingRect
我用 QRectF 输出链接到 BoundingRect,但信息也适用于其他版本。
所以基本上,将 QPainter.boundingRect() 的结果传递到 QPainter.drawText() 方法的boundingRect 参数(第二个 QRect 参数)。
更新2:
我为自己的言论深表歉意。 我忘记了drawText在PyQt中的工作方式与在Qt中的工作方式不同。 边界矩形由drawText函数返回(不像Qt那样传入),此外,您必须在获得返回给您的边界矩形之前指定对齐标志。 (我什至按照 Aaron Digulla 的评论包含了 p.end()):
这是输出:
所以看起来你必须自己设置边界矩形,尽管至少它是由传入标志时,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):
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:
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):
Here is the output:
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.
绘画不会改变 Qt 中物体的大小。 主要原因是这样的:
因此调整大小必须在布局阶段进行。 此后,界限不应改变。
要解决您的问题,请使用 QFontMetric 计算出在构建图片期间或接近构建图片时文本的大小,然后相应地调整其大小。
[编辑] 嗯...在请求边界矩形之前尝试调用 end() 。 如果有效,您就发现了一个错误(看不到添加元素时边界矩形不应该存在的原因......)
Painting doesn't change the size of something in Qt. The main reason is this:
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...)