在QWidget中拖动QPushButton时,它会工作,直到它通过(穿过(主窗口(QWidget(的边界时消失
我想限制这个QPushButton不越过它的容器的边界,但在QWidget中保持可见
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_()
有办法做到这一点吗?
由于要求确保子级在父级的边界内,因此在移动之前,您需要检查转换到新位置的子级rect()
是否在该区域内
注意,为了实现";自我;移动,没有必要不断地转换全球位置。
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.pos()
super(DragButton, self).mousePressEvent(event)
def mouseMoveEvent(self, event):
if event.buttons() == Qt.LeftButton:
delta = event.pos() - self.__mouseMovePos
newPos = self.pos() + delta
if self.parent():
geo = self.rect().translated(newPos)
parentRect = self.parent().rect()
if geo.x() < 0:
geo.moveLeft(0)
elif geo.right() > parentRect.right():
geo.moveRight(parentRect.right())
if geo.y() < 0:
geo.moveTop(0)
elif geo.bottom() > parentRect.bottom():
geo.moveBottom(parentRect.bottom())
self.move(geo.topLeft())
else:
self.move(newPos)
super(DragButton, self).mouseMoveEvent(event)
还要考虑,拖动启用的按钮不是一个好主意,因为它可能会导致不必要的、非直觉的和意外的行为。您的实现显示了这一点,因为如果按钮移动超过3像素限制,它将保持按下状态。