[PyQt] stopping tool button from staying depressed in D

danny shevitz at lanl.gov
Tue Sep 21 21:14:03 BST 2010


> Danny, show us your overload, or even better a minimum runnable example.
> 
> You should also catch the mousePressEvent and prevent the press from being 
> delivered to the Qt event machinery, e.g. call event.ignore() on left 
> clicks.
> 
> Consequently, you should start the drag yourself in the mouseMoveEvent, as 
> the above might prevent button.pressed.connect() from working correctly..

The following is my code as currently implimented. I was messing around and
hooked up two handlers. One triggers on the button.clicked signal. The other
is an overload of the mouseMove event. I realize that the following is not good
coding style, overloading a button with two distinct features (click or drop
source), but I'm trying to learn. The two handlers do not seem to clobber each
other. Both work as expected. The line I was originally inquiring about was
the self.setDown(False). It seems like there should be a better way to do it
with event.ignore, but I wasn't sure where. 

If you feel like offering any free advice, I'd like to hear advice on when to
connect slots, and when to overload event methods. For instance, in this case,
there is button.clicked, but also button.mousePressEvent. When would a person
want to use one or the other.

thanks,
Danny 


class myToolButton(QtGui.QToolButton):
 
    def __init__(self):
        super(myToolButton, self).__init__()
        self.clicked.connect(self.slotClicked)
        
    def mouseMoveEvent(self,  event):
        self.setDown(False)
        self.startDrag(event)    
        
    def startDrag(self,  event):
        mimeData = QtCore.QMimeData()
        mimeData.setText("funky duck")
        drag = QtGui.QDrag(self)
        drag.setMimeData(mimeData)
        drag.setPixmap(QtGui.QPixmap(":save.png"))
        drag.exec_(QtCore.Qt.CopyAction | QtCore.Qt.MoveAction,
QtCore.Qt.MoveAction)#QtCore.Qt.CopyAction #
        
    def slotClicked(self):
        print "caught clicked"




More information about the PyQt mailing list