QGraphicsPathItem问题与计算boundingRect

问题描述 投票:0回答:1

我为Qt5PySide2)和Qt4PySide)之间的行为差​​异感到困惑。我觉得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)

有人看到明显的解释吗?

谢谢

python pyside pyside2 qgraphicspathitem
1个回答
0
投票

boundingRect考虑到QGraphicsPathItem的QPen进行计算,如源代码(Qt4Qt5)所示。

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值发生了变化:

[默认笔是宽度为0的纯黑色画笔,方帽样式(Qt :: SquareCap)和斜角连接样式(Qt :: BevelJoin)。

[默认笔是带有1个宽度的方形黑色实心黑色笔刷(Qt :: SquareCap)和斜角连接样式(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)
© www.soinside.com 2019 - 2024. All rights reserved.