python - QGraphicsItem.setTransformations 不能按预期工作(也:PySide2 与 PyQt5 之间的差异)
问题描述
当我尝试向 a 添加转换时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 错误?在后一种情况下,如何解决问题,并且对于奖励积分,以一种适用于两个套餐的方式?
解决方案
要知道哪个是正确的,必须分析原始来源的行为:
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsRectItem rect;
rect.setTransformations({new QGraphicsScale()});
int n = rect.transformations().length();
char message[80];
sprintf(message, "object has %d transformations (expecting 1)", n);
Q_ASSERT_X(n == 1, "QGraphicsItem::setTransformations()", message);
}
并且在执行的时候并没有释放错误,所以 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)"
推荐阅读
- django - `basename` 参数未指定,并且可以'\
- java - Elasticsearch:使用 Java 客户端查找给定索引的所有映射类型
- php - 如何在 composer 和 /vendor/autoload.php 之前加载文件?
- java - View、Viewgroup、onClick、getChildAt问题
- android - 如何创建一个可以在一段时间后自行卸载的应用程序
- python - 为什么我在使用 python fire 时总是打印类型信息?
- python - 基于if语句的for循环,错误信息= Series的真值不明确
- javascript - 加载/显示最后搜索值的页面
- nginx - “/usr/share/nginx/html/AppzillonServer”失败(2:没有这样的文件或目录)
- html - Markdown 中“隐藏”属性的替代方法是什么?