Table of contents
1. The actual combat scene
Practical scenario: Drag-and-drop button component of PyQt6 in Python
2. Main points of knowledge
Three, rookie combat
Create a file!
import sys
from PyQt6.QtCore import Qt, QMimeData
from PyQt6.QtGui import QDrag
from PyQt6.QtWidgets import QPushButton, QWidget, QApplication
class Button(QPushButton):
def __init__(self, title, parent):
super().__init__(title, parent)
def mouseMoveEvent(self, e):
if e.buttons() != Qt.MouseButtons.RightButton:
return
mimeData = QMimeData()
drag = QDrag(self)
drag.setMimeData(mimeData)
drag.setHotSpot(e.position().toPoint() - self.rect().topLeft())
dropAction = drag.exec(Qt.DropActions.MoveAction)
def mousePressEvent(self, e):
super().mousePressEvent(e)
if e.button() == Qt.MouseButtons.LeftButton:
print('press')
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setAcceptDrops(True)
self.button = Button('Button', self)
self.button.move(100, 65)
self.setWindowTitle('Click or Move')
self.setGeometry(300, 300, 550, 450)
def dragEnterEvent(self, e):
e.accept()
def dropEvent(self, e):
position = e.position()
self.button.move(position.toPoint())
e.setDropAction(Qt.DropActions.MoveAction)
e.accept()
def main():
app = QApplication(sys.argv)
ex = Example()
ex.show()
app.exec()
if __name__ == '__main__':
main()
In this example, there is one in the window. QPushButton
Clicking on it with the left mouse button will print a 'press' message to the console, and you can click and drag it with the right mouse button.
class Button(QPushButton):
def __init__(self, title, parent):
super().__init__(title, parent)
QPushButton
Created a class based on Button
and implemented two QPushButton
methods: mouseMoveEvent
and mousePressEvent
. mouseMoveEvent
The method is where the handling of the drag and drop operation begins.
if e.buttons() != Qt.MouseButtons.RightButton:
return
Define the right mouse button as the button that triggers the drag operation, and the left mouse button will only trigger the click event.
drag = QDrag(self)
drag.setMimeData(mimeData)
drag.setHotSpot(e.position().toPoint() - self.rect().topLeft())
Create QDrag
objects to provide drag and drop operations based on MIME data types.
dropAction = drag.exec(Qt.DropActions.MoveAction)
drag
The methods of the object exec
perform the drag and drop operation.
def mousePressEvent(self, e):
super().mousePressEvent(e)
if e.button() == Qt.MouseButtons.LeftButton:
print('press')
If the button is clicked with the left mouse button, the 'press' message will be printed on the console. Note that the mousePressEvent
method is also called on the parent here, otherwise the button press action will not be displayed.
position = e.pos() self.button.move(position)
dropEvent
The method handles the operation after the mouse releases the button - modify the position of the component to the current coordinates of the mouse.
e.setDropAction(Qt.MoveAction) e.accept()
Use the setDropAction
specified type of drag-and-drop operation -- mouse movement.
Rookie combat, keep learning!