| # -*- coding: utf-8 -*- |
| |
| """ |
| ZetCode PyQt5 tutorial |
| |
| In this program, we can press on a button with |
| a left mouse click or drag and drop the button |
| with the right mouse click. |
| |
| author: Jan Bodnar |
| website: zetcode.com |
| last edited: January 2015 |
| """ |
| |
| import sys |
| from PyQt5.QtWidgets import QPushButton, QWidget, QApplication |
| from PyQt5.QtCore import Qt, QMimeData |
| from PyQt5.QtGui import QDrag |
| |
| from userale.ale import Ale |
| |
| class Button(QPushButton): |
| |
| def __init__(self, title, parent): |
| super().__init__(title, parent) |
| |
| |
| def mouseMoveEvent(self, e): |
| |
| if e.buttons() != Qt.RightButton: |
| return |
| |
| mimeData = QMimeData() |
| |
| drag = QDrag(self) |
| drag.setMimeData(mimeData) |
| drag.setHotSpot(e.pos() - self.rect().topLeft()) |
| |
| dropAction = drag.exec_(Qt.MoveAction) |
| |
| |
| def mousePressEvent(self, e): |
| |
| QPushButton.mousePressEvent(self, e) |
| |
| if e.button() == Qt.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, 280, 150) |
| |
| |
| def dragEnterEvent(self, e): |
| |
| e.accept() |
| |
| |
| def dropEvent(self, e): |
| |
| position = e.pos() |
| self.button.move(position) |
| |
| e.setDropAction(Qt.MoveAction) |
| e.accept() |
| |
| def test_drag2 (): |
| app = QApplication(sys.argv) |
| ex = Example() |
| ale = Ale () |
| # install globally |
| app.installEventFilter (ale) |
| ex.show() |
| app.exec_() |
| |