Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is an example of a moveable button that still supports the normal click signal properly:</p> <pre><code>from PyQt4 import QtCore, QtGui class DragButton(QtGui.QPushButton): def mousePressEvent(self, event): self.__mousePressPos = None self.__mouseMovePos = None if event.button() == QtCore.Qt.LeftButton: self.__mousePressPos = event.globalPos() self.__mouseMovePos = event.globalPos() super(DragButton, self).mousePressEvent(event) def mouseMoveEvent(self, event): if event.buttons() == QtCore.Qt.LeftButton: # adjust offset from clicked point to origin of widget currPos = self.mapToGlobal(self.pos()) globalPos = event.globalPos() diff = globalPos - self.__mouseMovePos newPos = self.mapFromGlobal(currPos + diff) self.move(newPos) self.__mouseMovePos = globalPos super(DragButton, self).mouseMoveEvent(event) def mouseReleaseEvent(self, event): if self.__mousePressPos is not None: moved = event.globalPos() - self.__mousePressPos if moved.manhattanLength() &gt; 3: event.ignore() return super(DragButton, self).mouseReleaseEvent(event) def clicked(): print "click as normal!" if __name__ == "__main__": app = QtGui.QApplication([]) w = QtGui.QWidget() w.resize(800,600) button = DragButton("Drag", w) button.clicked.connect(clicked) w.show() app.exec_() </code></pre> <p>In the <code>mousePressEvent</code> I record both the initial start position, and a position that will get updated throughout the drag.</p> <p>In the <code>mouseMoveEvent</code>, I get the proper offset of the widget from where it was clicked to where the actual origin is, so that the move is accurate. </p> <p>In the <code>mouseReleaseEvent</code>, I check to see if the overall move was greater than at least a tiny amount. If it was, then it was a drag and we ignore the normal event to not produce a "clicked" signal. Otherwise, we allow the normal event handler to produce the click.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload