Wednesday, January 29, 2014

Qt's Drag-and-Drop Architecture for Python and PyQt5
A Doh! Moment

Sometimes you have to take the long way around to the obvious answer...

So I am concerned about knowing where the mouse is, when a drag action ends. I even hinted I could use a signal to send back the value of QCursor.pos() from inside some drag object.

Then it occurred to me: if I want the cursor position at the end of a drag, why don't I just, oh, I don't know, sample the cursor position at the end of the drag?

In other words, change the doSomeDraggin() routine like this:

        act = dragster.exec_(actions)
        global_pos = QCursor.pos() # cursor immediately after drag ends
        local_pos = self.mapFromGlobal(global_pos)
        print('cursor at local  {0}, {1}'.format(local_pos.x(),local_pos.y()))
        if not self.parentWidget().rect().contains(local_pos) :
            print('drop was outside my parent')

And of course this works fine. Even on a slow machine, the mouse can't have moved more than a pixel or two between release of the mouse button and the return from the exec_() of the drag.

No comments: