Pyqt5 可拖动 QPushButton
Pyqt5 draggable QPushButton
我有关于如何拖动和移动 QPushButton 的示例代码。该代码的唯一问题是,当您拖动按钮并释放它时,按钮状态保持为选中状态。
谁能帮我更改代码,以便在拖动按钮并释放按钮状态后自动取消选中。所以,我不必单击它来取消选中它。
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
from PyQt5.QtCore import Qt
class DragButton(QPushButton):
def mousePressEvent(self, event):
self.__mousePressPos = None
self.__mouseMovePos = None
if event.button() == Qt.LeftButton:
self.__mousePressPos = event.globalPos()
self.__mouseMovePos = event.globalPos()
super(DragButton, self).mousePressEvent(event)
def mouseMoveEvent(self, event):
if event.buttons() == 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() > 3:
event.ignore()
return
super(DragButton, self).mouseReleaseEvent(event)
def clicked():
print ("click as normal!")
if __name__ == "__main__":
app = QApplication([])
w = QWidget()
w.resize(800,600)
button = DragButton("Drag", w)
button.clicked.connect(clicked)
w.show()
app.exec_()
你 return
在 mouseReleaseEvent 中,这意味着你没有让按钮知道它实际上收到了鼠标释放,因此保持按下状态。
def mouseReleaseEvent(self, event):
if self.__mousePressPos is not None:
moved = event.globalPos() - self.__mousePressPos
if moved.manhattanLength() > 3:
event.ignore()
return # <-- the problem is here!
super(DragButton, self).mouseReleaseEvent(event)
如果将鼠标移动几个像素(低于曼哈顿长度),您会发现它的行为是正确的,因此您必须完全删除 if
块,或调用 self.setDown(False)
如果您想避免发送 clicked
信号,请在返回之前返回。
我有关于如何拖动和移动 QPushButton 的示例代码。该代码的唯一问题是,当您拖动按钮并释放它时,按钮状态保持为选中状态。
谁能帮我更改代码,以便在拖动按钮并释放按钮状态后自动取消选中。所以,我不必单击它来取消选中它。
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
from PyQt5.QtCore import Qt
class DragButton(QPushButton):
def mousePressEvent(self, event):
self.__mousePressPos = None
self.__mouseMovePos = None
if event.button() == Qt.LeftButton:
self.__mousePressPos = event.globalPos()
self.__mouseMovePos = event.globalPos()
super(DragButton, self).mousePressEvent(event)
def mouseMoveEvent(self, event):
if event.buttons() == 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() > 3:
event.ignore()
return
super(DragButton, self).mouseReleaseEvent(event)
def clicked():
print ("click as normal!")
if __name__ == "__main__":
app = QApplication([])
w = QWidget()
w.resize(800,600)
button = DragButton("Drag", w)
button.clicked.connect(clicked)
w.show()
app.exec_()
你 return
在 mouseReleaseEvent 中,这意味着你没有让按钮知道它实际上收到了鼠标释放,因此保持按下状态。
def mouseReleaseEvent(self, event):
if self.__mousePressPos is not None:
moved = event.globalPos() - self.__mousePressPos
if moved.manhattanLength() > 3:
event.ignore()
return # <-- the problem is here!
super(DragButton, self).mouseReleaseEvent(event)
如果将鼠标移动几个像素(低于曼哈顿长度),您会发现它的行为是正确的,因此您必须完全删除 if
块,或调用 self.setDown(False)
如果您想避免发送 clicked
信号,请在返回之前返回。