简体   繁体   中英

QGraphicsItem.setTransformations does not work as expected (also: discrepancy between PySide2 vs PyQt5)

When I try to add a transform to a QGraphicsItem , it does not show up in the list of transformations of that object:

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)    

What surprises me it that the exact same code works as expected using PyQt5.

from PyQt5.QtWidgets import QGraphicsRectItem, QGraphicsScale
...
# No AssertionError

What's with this discrepency between PyQt5 and PySide2, is it a bug in PySide2 or am I using PySide2 wrong? In the later case, how to fix the problem, and, for bonus points, in a way that works for both packages?

To know which one is correct then the behavior of the original source must be analyzed:

#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);
}

And when it is executed, the error is not released, so PyQt5 has the appropriate behavior.

In the case of PySide2 it is that it has problems in handling objects that are not assigned to a variable, in this case the solution is to assign the list to a variable:

from PySide2.QtWidgets import QGraphicsRectItem, QGraphicsScale

rect = QGraphicsRectItem()

rect.setTransformations()
n = len(rect.transformations())
assert n == 1, f"object has {n} transformations (expecting 1)"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM