gpt4 book ai didi

python - QGraphicsPathItem 计算boundingRect 的问题

转载 作者:行者123 更新时间:2023-12-01 00:24:08 27 4
gpt4 key购买 nike

我对 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com