| # Licensed to the Apache Software Foundation (ASF) under one or more |
| # contributor license agreements. See the NOTICE file distributed with |
| # this work for additional information regarding copyright ownership. |
| # The ASF licenses this file to You under the Apache License, Version 2.0 |
| # (the "License"); you may not use this file except in compliance with |
| # the License. You may obtain a copy of the License at |
| # |
| # http://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| # See the License for the specific language governing permissions and |
| # limitations under the License. |
| |
| """ |
| 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 (output="drag2.log", user="testUser", toolname="dragtest", toolversion="0.0.1") |
| # install globally |
| app.installEventFilter (ale) |
| ex.show() |
| app.exec_() |
| |