PySide2: Understanding QGraphicsItemGroup.setScale()



  • Here is a little example that doesn't do what I expect it to do. I expect a 50x50 px² circle at (500, 1000) px. What I get is a single pixel.

    #!/usr/bin/python3
    # -*- coding: utf-8 -*-
    """Show an incomprehension in setScale for QGraphicsItemGroup."""
    
    from PySide2 import QtCore
    from PySide2 import QtGui
    from PySide2 import QtWidgets
    
    point_size = 0.1  # In real-world units.
    
    # Get entrypoint through which we control underlying Qt framework
    app = QtWidgets.QApplication([])
    
    scene = QtWidgets.QGraphicsScene()
    group = QtWidgets.QGraphicsItemGroup()
    scale = 500  # 1 real-world units = 500 px.  !! Has no apparent effect.
    group.setScale(scale)
    pen = QtGui.QPen(QtCore.Qt.red)
    brush = QtGui.QBrush(QtCore.Qt.blue)
    x = 1  # Real-world units.
    y = 2  # Real-world units.
    top_left = (x - point_size / 2, y - point_size / 2)
    ellipse = QtWidgets.QGraphicsEllipseItem(
        QtCore.QRectF(*top_left, point_size, point_size))
    ellipse.setPen(pen)
    ellipse.setBrush(brush)
    group.addToGroup(ellipse)
    scene.addItem(group)
    
    view = QtWidgets.QGraphicsView(scene)
    view.show()
    
    # Start Qt/PySide2 application. If we don't show any windows, the
    # app would just loop at this point without the means to exit
    app.exec_()
    


  • Hi @galou_breizh ,

    It seems it works (windows) after moving group.setScale(scale) below group.addToGroup(ellipse)

    group.addToGroup(ellipse)
    group.setScale(scale)
    

    Edit: It works also by putting the scale on the ellipse object. ellipse.setScale(scale)



  • Thanks for the super-quick answer!

    Ok, I don't get it but the result is now different. However, it's still not the correct result. What I get is something between a circle and a square with red stroke (size 500x500 px²) filled with white color with a 50x50 px² blue circle in the middle cf. screenshot. The same as in my other post: https://forum.qt.io/topic/110762/pyside2-filling-not-scaled#



  • @galou_breizh I answered in the other post. But it's not a good way to duplicate post for the same issue. Please try to avoid it in the future, thanks