32. PyQt6 中的拖拽操作2
2023-10-17 python 588
import sys from PyQt6.QtWidgets import QApplication, QWidget, QPushButton from PyQt6.QtGui import QGuiApplication, QDrag from PyQt6.QtCore import Qt, QMimeData class Button(QPushButton): def __init__(self, title, parent): super().__init__(title, parent) def mouseMoveEvent(self, e): if e.buttons() != Qt.MouseButton.RightButton: return mimeData = QMimeData() drag = QDrag(self) drag.setMimeData(mimeData) drag.setHotSpot(e.position().toPoint() - self.rect().topLeft()) # print(e.position().toPoint(), self.rect().topLeft()) dropAction = drag.exec(Qt.DropAction.MoveAction) def mousePressEvent(self, e): super().mousePressEvent(e) if e.button() == Qt.MouseButton.LeftButton: print("press") class App(QWidget): def __init__(self): super().__init__() self.button = None self.initUI() def initUI(self): self.setWindowTitle("PyQt6 中的拖拽操作2") self.setAcceptDrops(True) self.button = Button("按钮", self) self.button.move(100, 65) self.setGeometry(100, 100, 400, 300) self.center() def dragEnterEvent(self, e): e.accept() def dropEvent(self, e): position = e.position() self.button.move(position.toPoint()) e.setDropAction(Qt.DropAction.MoveAction) e.accept() def center(self): qr = self.frameGeometry() cp = QGuiApplication.primaryScreen().availableGeometry().center() qr.moveCenter(cp) self.move(qr.topLeft()) if __name__ == "__main__": app = QApplication(sys.argv) cls_app = App() cls_app.show() sys.exit(app.exec())
很赞哦! (0)
相关文章
文章评论
-
-
-
0条评论