[prev in list] [next in list] [prev in thread] [next in thread] 

List:       pykde
Subject:    QGraphicsItem setCursor not working correctly with viewport margins
From:       Maurizio Berti <maurizio.berti () gmail ! com>
Date:       2020-12-17 19:06:04
Message-ID: CAPn+-XTAps4CDLZN1xxFX7_JW3k_8YgiaooiK6cSjLVP=QzuNw () mail ! gmail ! com
[Download RAW message or body]

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

[Attachment #3 (text/html)]

<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr">I noticed a strange \
behavior that I&#39;m able to reproduce at least with PyQt 5.12 on \
linux.<div><br></div><div>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.</div><div><br></div><div><br></div><div><div><font \
face="monospace">class TestItem(QtWidgets.QGraphicsRectItem):</font></div><div><font \
face="monospace">      def __init__(self):</font></div><div><font face="monospace">   \
super().__init__()</font></div><div><font face="monospace">            \
self.setRect(0, 0, 50, 50)</font></div><div><font face="monospace">            \
self.setFlags(self.ItemIsMovable)</font></div><div><font \
face="monospace"><br></font></div><div><font face="monospace">      def \
mousePressEvent(self, event):</font></div><div><font face="monospace">            \
super().mousePressEvent(event)</font></div><div><font face="monospace">            if \
event.button() == QtCore.Qt.LeftButton:</font></div><div><font face="monospace">      \
self.setCursor(QtCore.Qt.ClosedHandCursor)</font></div><div><font \
face="monospace"><br></font></div><div><font face="monospace">      def \
mouseReleaseEvent(self, event):</font></div><div><font face="monospace">            \
super().mouseReleaseEvent(event)</font></div><div><font face="monospace">            \
self.unsetCursor()</font></div><div><font \
face="monospace"><br></font></div><div><font \
face="monospace"><br></font></div><div><font face="monospace">def \
setMargin(margin=False):</font></div><div><font face="monospace">      \
view.setViewportMargins(0, 30 if margin else 0, 0, 0)</font></div><div><font \
face="monospace"><br></font></div><div><font face="monospace">import \
sys</font></div><div><font face="monospace">app = \
QtWidgets.QApplication(sys.argv)</font></div><div><font face="monospace">test = \
QtWidgets.QWidget()</font></div><div><font face="monospace">layout = \
QtWidgets.QVBoxLayout(test)</font></div><div><font \
face="monospace">test.setMinimumSize(300, 300)</font></div><div><font \
face="monospace">marginButton = QtWidgets.QPushButton(&#39;Set margin&#39;, \
checkable=True)</font></div><div><font \
face="monospace">layout.addWidget(marginButton)</font></div><div><font \
face="monospace">marginButton.toggled.connect(setMargin)</font></div><div><font \
face="monospace">view = QtWidgets.QGraphicsView()</font></div><div><font \
face="monospace">layout.addWidget(view)</font></div><div><font face="monospace">scene \
= QtWidgets.QGraphicsScene()</font></div><div><font \
face="monospace">view.setScene(scene)</font></div><div><font \
face="monospace">scene.addItem(TestItem())</font></div><div><font \
face="monospace">test.show()</font></div><div><font \
face="monospace">sys.exit(app.exec_())</font></div><div><br></div><div>By looking at \
the sources of QGraphicsItem I suspect that there&#39;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 <a href="https://github.com/qt/qtbase/blob/dev/src/widgets/graphicsview/qgraphicsit \
em.cpp#L2308">https://github.com/qt/qtbase/blob/dev/src/widgets/graphicsview/qgraphicsitem.cpp#L2308</a> \
).</div><div><br>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)?</div><div><br></div><div>Thanks,</div><div>Maurizio</div><div> \
</div>-- <br><div dir="ltr" class="gmail_signature">È difficile avere una \
convinzione precisa quando si parla delle ragioni del cuore. - &quot;Sostiene \
Pereira&quot;, Antonio Tabucchi<br><a href="http://www.jidesk.net" \
target="_blank">http://www.jidesk.net</a></div></div></div></div></div></div>



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic