[PyQt] Is this a bug or ?

Phil Thompson phil at riverbankcomputing.com
Wed Mar 19 19:24:21 GMT 2008


On Wednesday 19 March 2008, Gilles CHAUVIN wrote:
> Hi,
>
> I'm new to this list so I apologize if this has been asked / reported
> before. I'm trying to add drag n' drop support to a QListWidget. Here
> is my current test code:
>
> ---------------------------------------------------------------------------
>--- from PyQt4.Qt import *
>
> class ListWidget(QListWidget):
>     def __init__(self, parent=None):
>         QListWidget.__init__(self, parent)
>
>         self.setAcceptDrops(True)
>         self.startPos = QPoint();
>
>
>     def mousePressEvent(self, event):
>         if event.button() == Qt.LeftButton:
>             self.startPos = event.pos()
>         QListWidget.mousePressEvent(self, event)
>         print "mPE - Pos: %dx%d" % (self.startPos.x(), self.startPos.y())
>
>
>     def mouseMoveEvent(self, event):
>         if event.buttons() & Qt.LeftButton:
>             print "mME - Pos: %dx%d" % (self.startPos.x(),
> self.startPos.y())
> ---------------------------------------------------------------------------
>---
>
> While the app is running, I left-click on my "ListWidget" and then
> (while holding the button clicked) move my mouse. The output looks
> like:
> mPE - Pos: 56x257
> mME - Pos: 137x129
> mME - Pos: 149x105
> mME - Pos: 155x87
> mME - Pos: 159x75
> mME - Pos: 161x67
> mME - Pos: 163x57
> mME - Pos: 165x49
> mME - Pos: 165x48
> [...]
>
> I don't understand why self.startPos keeps changing whenever I move
> the mouse ?! Am I doing something wrong or is there a bug somewhere in
> Python / SIP / PyQt / Qt ?
>
> I'm running the following software versions (under ArchLinux):
> - python 2.5.2
> - qt 4.3.3
> - sip 4.7.3
> - pyqt 4.3.3

event.pos() is returning a C++ reference to a temporary object and you are, in 
effect, keeping a permanent pointer to it. The memory is getting reused for 
each new event.

The fix is to take an explicit copy of the QPoint...

    self.startPos = QPoint(event.pos())

...which is what happens implicitly in C++.

I plan to fix this generally, but haven't yet decided how.

Phil


More information about the PyQt mailing list