Python QGraphicsItem.setTransformations未按预期工作(另外:PySide2与PyQt5之间存在差异)
当我尝试向Python QGraphicsItem.setTransformations未按预期工作(另外:PySide2与PyQt5之间存在差异),python,pyqt,pyqt5,pyside2,qgraphicsitem,Python,Pyqt,Pyqt5,Pyside2,Qgraphicsitem,当我尝试向QGraphicsItem添加转换时,它不会显示在该对象的转换列表中: from PySide2.QtWidgets import QGraphicsRectItem, QGraphicsScale rect = QGraphicsRectItem() rect.setTransformations([QGraphicsScale()]) n = len(rect.transformations()) assert n == 1, f'object has {n} transform
QGraphicsItem
添加转换时,它不会显示在该对象的转换列表中:
from PySide2.QtWidgets import QGraphicsRectItem, QGraphicsScale
rect = QGraphicsRectItem()
rect.setTransformations([QGraphicsScale()])
n = len(rect.transformations())
assert n == 1, f'object has {n} transformations (expecting 1)'
# AssertionError: object has 0 transformations (expecting 1)
令我惊讶的是,使用PyQt5的代码与预期的完全相同
from PyQt5.QtWidgets import QGraphicsRectItem, QGraphicsScale
...
# No AssertionError
PyQt5和PySide2之间的差异是什么,是PySide2中的错误还是我使用PySide2的错误?在后一种情况下,如何解决问题,以及如何以适用于两个软件包的方式获得额外积分?要知道哪一个是正确的,则必须分析原始源的行为:
#包括
int main(int argc,char*argv[])
{
质量保证申请a(argc、argv);
qgraphicsrecitem-rect;
rect.setTransformations({new qgraphicscale()});
int n=rect.transformations().length();
字符消息[80];
sprintf(消息,“对象有%d个转换(应为1)”,n);
Q_ASSERT_X(n==1,“QGraphicsItem::setTransformations()”,消息);
}
当它被执行时,错误不会被释放,因此PyQt5具有适当的行为
在PySide2的情况下,它在处理未分配给变量的对象时存在问题,在这种情况下,解决方案是将列表分配给变量:
from PySide2.QtWidgets import QGraphicsRectItem, QGraphicsScale
rect = QGraphicsRectItem()
transforms = [QGraphicsScale()]
rect.setTransformations(transforms)
n = len(rect.transformations())
assert n == 1, f"object has {n} transformations (expecting 1)"
从PySide2.QtWidgets导入QGraphicsRectItem、QGraphicsCale
rect=qgraphicsrecitem()
转换=[qgraphicscale()]
rect.setTransformations(转换)
n=len(rect.transformations())
assert n==1,f“对象有{n}个转换(预期为1)”
Ooh,这是一个令人讨厌的转换。非常感谢。