- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 Qt5
(PySide2
) 和 Qt4
(PySide
) 之间的行为差异感到困惑。我的印象是 Qt5
有一个错误,但也许我做错了什么?
简而言之:当将计算出的QPainterPath
应用到QGraphicsPathItem
(使用setPath
)时,QGraphicsPathItem<的结果大小
比 QPainterPath
本身的大小大 1.5 像素。这对我来说毫无意义,而 Qt4 的大小是完全相同的。
我提供了一段简单的代码,可以使用 PySide 和 PySide2 进行重现。
使用 PySide:
#!/usr/bin/env python2
from PySide.QtCore import *
from PySide.QtGui import *
class Foo (QGraphicsPathItem):
def __init__(self, parent):
super(Foo, self).__init__()
path = QPainterPath()
path.addRect(0,0,10,10)
print(str(path.boundingRect()))
self.setPath(path)
print(str(self.boundingRect()))
x=Foo(None)
结果是:
$ python2 ./with_py2.py
PySide.QtCore.QRectF(0.000000, 0.000000, 10.000000, 10.000000)
PySide.QtCore.QRectF(0.000000, 0.000000, 10.000000, 10.000000)
大小相同,符合预期。一切都好。
与Qt5完全相同的代码:
#!/usr/bin/env python3
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
class Foo (QGraphicsPathItem):
def __init__(self, parent):
super(Foo, self).__init__()
path = QPainterPath()
path.addRect(0,0,10,10)
print(str(path.boundingRect()))
self.setPath(path)
print(str(self.boundingRect()))
x=Foo(None)
结果:
$ python3 bug.py
PySide2.QtCore.QRectF(0.000000, 0.000000, 10.000000, 10.000000)
PySide2.QtCore.QRectF(-0.500000, -0.500000, 11.000000, 11.000000)
有人看到任何明显的解释吗?
谢谢
最佳答案
boundingRect 依赖于 QGraphicsPathItem 的 QPen 来计算它,如源代码中所示。
<强> Qt4 :
QRectF QGraphicsPathItem::boundingRect() const
{
Q_D(const QGraphicsPathItem);
if (d->boundingRect.isNull()) {
qreal pw = pen().widthF();
if (pw == 0.0)
d->boundingRect = d->path.controlPointRect();
else {
d->boundingRect = shape().controlPointRect();
}
}
return d->boundingRect;
}
<强> Qt5
QRectF QGraphicsPathItem::boundingRect() const
{
Q_D(const QGraphicsPathItem);
if (d->boundingRect.isNull()) {
qreal pw = pen().style() == Qt::NoPen ? qreal(0) : pen().widthF();
if (pw == 0.0)
d->boundingRect = d->path.controlPointRect();
else {
d->boundingRect = shape().controlPointRect();
}
}
return d->boundingRect;
}
如果您检查两个版本中的 Qt 文档,您会发现默认创建的 QPen 的值发生了变化:
The default pen is a solid black brush with 0 width, square cap style (Qt::SquareCap), and bevel join style (Qt::BevelJoin).
(强调我的)
The default pen is a solid black brush with 1 width, square cap style (Qt::SquareCap), and bevel join style (Qt::BevelJoin).
(强调我的)
如果您想观察 PySide2 中的 PySide 行为,请将 QPen(Qt::NoPen)
设置为 QGraphicsPathItem
:
class Foo(QGraphicsPathItem):
def __init__(self, parent=None):
super(Foo, self).__init__(parent)
self.setPen(QPen(Qt.NoPen))
path = QPainterPath()
path.addRect(0, 0, 10, 10)
print(str(path.boundingRect()))
self.setPath(path)
print(str(self.boundingRect()))
x = Foo()
输出
PySide2.QtCore.QRectF(0.000000, 0.000000, 10.000000, 10.000000)
PySide2.QtCore.QRectF(0.000000, 0.000000, 10.000000, 10.000000)
关于python - QGraphicsPathItem 计算boundingRect 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58734077/
我正在编写一个带圆圈的物理模拟。 Ball.cpp 代码: Ball::Ball() { angle = 0; setRotation(angle); //set the
尝试在 opencv 中使用 boundingRect() 函数时出现错误。给出的是点列表 lists = [] for match in enumerate(matches): lists.
我在使用以下代码时遇到了非常奇怪的问题。 extension String { func height(withConstrainedWidth width: CGFloat, fon
我在使用 QFontMetrics 的“boundingRect”函数时遇到问题,它没有返回正确的结果。 mfntArial = QFont("Arial", 12, QFont::Bold)
我有一些代码,我可以在其中绘制符号并将其附加到绘图中。现在我想知道是否有一个用符号自动创建的边界矩形,这样我就可以编写一些代码来选择符号,以便用户可以编辑它 - 例如选择它以便删除它。 我在 qwt_
我试图在用户移动 QGraphicsRectItem 时获取它的子类的 boundingRect() 。但是,随着矩形在场景中移动,boundingRect() 返回的矩形始终返回边界矩形的原始值。下
我有一个 class Edge : public QGraphicsItem,它实现了从一个节点到另一个节点的绘制箭头(下图)。 现在我需要添加在自己身上画箭头的功能 (arc)。 我无法绘制弧线,覆
我像这样计算 NSAttributedString 的高度。 let maxSize = NSSize(width: w, height: CGFloat.greatestFiniteMagnitud
我一直在尝试使用 QGraphicsItemGroup 来获取一组 QGraphicsItem* 的边界矩形。在我看来,当我将所有项目插入组中时,边界矩形已正确确定;但是如果我随后移动组中的项目,边界
QGraphicsItem::boundingRect () 是一个虚函数,所以我重写了它来处理我自己的一些东西,然后我发现它被重复调用,但我从未从我自己的代码中显式调用它。 谁在召唤它?表演或绘画之
我是 OpenCV 和 Python 的新手,我制作了一个程序来查找面积大于 500 的轮廓并将它们保存到新图像中我按照互联网上的建议使用了 boundingRect,它运行并完成了工作但我得到了图像
谷歌搜索 suggests that it should . 但是拖放机器人 example implementation (在父 Robot 对象上)建议不要: QRectF Robot::boun
我想实现一个 QGraphicsElement,它在圆角矩形内“按原样”绘制文本。 为了实现 QGraphicsElement 我需要实现 boundedRect 函数,所以我需要原样的多行消息的 b
当我尝试通过 NSString 函数 boundingRect 包装到盒子中并注意初始插入 (8,0,8,0) 时,我尝试使用以下方法计算文本的每一帧: let size = CGSize(width
在我的应用程序中,我在UITextView中的文本行下绘制自定义背景。为此,我使用boundingRect(forGlyphRange:in:)的NSLayoutManager方法。它适用于LTR语言
Kwadrat 类有什么问题?我有一个错误: Invalid new-expression of abstract class type 'Kwadrat' Kwadrat* kwadrat = ne
我正在使用 Qt 5.2,我目前正在尝试从 QTableView 打印一个表格,但是我在根据内容计算行高时遇到了这个问题。我现在得到的是下面的循环,它循环遍历 QTableView 行并使用 boun
我以为我可以使用下面的代码在它离开场景后删除任何项目,但事实并非如此。在尝试了不同的实现之后,我想我应该尝试另一种方法。有些 QGraphicsItems 实际上是在 boundingRect 之外开
我目前在 UITextField 上有以下扩展来计算给定字符串的边界矩形。 func widthHeight(font: UIFont) -> CGRect { let constraintR
我已经尝试了多种方法来围绕minAreaRect()创建boundingRect(),但是我一直遇到错误。我可以简单地使用原始轮廓,但是,这是要理解为什么与cv2.isContourConvex()和
我是一名优秀的程序员,十分优秀!