QGraphicsItem setCursor not working correctly with viewport margins

Maurizio Berti maurizio.berti at gmail.com
Thu Dec 17 19:06:04 GMT 2020


I noticed a strange behavior that I'm able to reproduce at least with PyQt
5.12 on linux.

If I use setCursor on an item in the mousePressEvent *and* the view has
left or top margins set, the cursor is not applied if clicked in the area
just over the right (or bottom) of the item, with the area corresponding to
the margin size.


class TestItem(QtWidgets.QGraphicsRectItem):
    def __init__(self):
        super().__init__()
        self.setRect(0, 0, 50, 50)
        self.setFlags(self.ItemIsMovable)

    def mousePressEvent(self, event):
        super().mousePressEvent(event)
        if event.button() == QtCore.Qt.LeftButton:
            self.setCursor(QtCore.Qt.ClosedHandCursor)

    def mouseReleaseEvent(self, event):
        super().mouseReleaseEvent(event)
        self.unsetCursor()


def setMargin(margin=False):
    view.setViewportMargins(0, 30 if margin else 0, 0, 0)

import sys
app = QtWidgets.QApplication(sys.argv)
test = QtWidgets.QWidget()
layout = QtWidgets.QVBoxLayout(test)
test.setMinimumSize(300, 300)
marginButton = QtWidgets.QPushButton('Set margin', checkable=True)
layout.addWidget(marginButton)
marginButton.toggled.connect(setMargin)
view = QtWidgets.QGraphicsView()
layout.addWidget(view)
scene = QtWidgets.QGraphicsScene()
view.setScene(scene)
scene.addItem(TestItem())
test.show()
sys.exit(app.exec_())

By looking at the sources of QGraphicsItem I suspect that there's a Qt bug,
since the cursor is instantly applied on the current items under the mouse
but the position is used with mapFromGlobal against the view and *not* the
viewport (see setCursor() implementation on
https://github.com/qt/qtbase/blob/dev/src/widgets/graphicsview/qgraphicsitem.cpp#L2308
).

I was going to submit a bug report, but I wanted to be sure that I got it
right. Can anyone else confirm this behavior (and eventually concur with my
doubt about mapFromGlobal)?

Thanks,
Maurizio

-- 
È difficile avere una convinzione precisa quando si parla delle ragioni del
cuore. - "Sostiene Pereira", Antonio Tabucchi
http://www.jidesk.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20201217/b565196f/attachment.htm>


More information about the PyQt mailing list