简体   繁体   English

Pyqt5 两个 QButton 拖放

[英]Pyqt5 Two QButton Drag and Drop

There are two buttons on the window called Button1 and Button2.窗口上有两个按钮,分别称为 Button1 和 Button2。 In the code below, I can move it by right-clicking the button named Button1.在下面的代码中,我可以通过右键单击名为 Button1 的按钮来移动它。 Button1 moves again when I want to right-click and drag Button2.当我想右键单击并拖动 Button2 时,Button1 再次移动。 But I can't move Button2 with the code below.但是我无法使用下面的代码移动 Button2。 I want to move which one of these two buttons right click.How can I do that?我想移动这两个按钮中的哪一个右键单击。我该怎么做?

from PyQt5.QtWidgets import QPushButton, QWidget, QApplication
from PyQt5.QtCore import Qt, QMimeData
from PyQt5.QtGui import QDrag
import sys

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):
        super().mousePressEvent(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.button1 = Button('Button1', self)
        self.button1.setFixedSize(100,100)
        self.button1.move(0, 0)

        self.button2 = Button('Button2', self)
        self.button2.move(0, 110)
        self.setWindowTitle('Click or Move')
        self.setGeometry(0, 0, 400, 400)


    def dragEnterEvent(self, e):
        e.accept()

    def dropEvent(self, e):
        position = e.pos()
        print(position)
        self.button1.move(position)
        e.setDropAction(Qt.MoveAction)
        e.accept()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    ex.show()
    app.exec_() 

You don't need to subclass a button to move it, and you certainly shouldn't use drag&drop if you're going to move the object within the same parent.你不需要子类化一个按钮来移动它,如果你要在同一个父级中移动对象,你当然不应该使用拖放。

Also, if you're going to check only movements that happen while the right button is pressed, you should always call the base implementation for the other situations (other buttons, or no button, in case mouseTracking is enabled; it's not usually the case with push buttons, but that's not the point).此外,如果您只检查按下右键时发生的移动,您应该始终为其他情况调用基本实现(其他按钮或没有按钮,以防启用mouseTracking ;通常情况并非如此按钮,但这不是重点)。

Install an event filter on the buttons, and track the following events:在按钮上安装事件过滤器,并跟踪以下事件:

  • QEvent.MouseButtonPress: if the button is the right one, set a variable for the current moving widget and another one with the current mouse position (let's say, startPosition) QEvent.MouseButtonPress:如果按钮是正确的,则为当前移动的小部件设置一个变量,并为当前鼠标位置设置另一个变量(比方说,startPosition)
  • QEvent.MouseMove: if a current moving widget exists, move it using the formula newPosition = currentPosition + eventPosition - startPosition QEvent.MouseMove:如果当前移动小部件存在,使用公式newPosition = currentPosition + eventPosition - startPosition移动它
  • QEvent.MouseButtonRelease: clear the current moving widget QEvent.MouseButtonRelease:清除当前移动的小部件
class Example(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()

        self.movingButton = None

    def initUI(self):

        self.button1 = QPushButton('Button1', self)
        self.button1.setFixedSize(100,100)
        # no need for this, every new widget is always placed at (0, 0)
        # self.button1.move(0, 0)
        self.button1.installEventFilter(self)

        self.button2 = QPushButton('Button2', self)
        self.button2.move(0, 110)
        self.button2.installEventFilter(self)

        self.setWindowTitle('Click or Move')
        self.setGeometry(0, 0, 400, 400)

    def eventFilter(self, source, event):
        if source in (self.button1, self.button2):
            if event.type() == QEvent.MouseButtonPress and event.button() == Qt.RightButton:
                self.movingButton = source
                self.startPos = event.pos()
            # uncomment the following lines if you want to move the button while
            # moving the mouse
            # elif event.type() == QEvent.MouseMove and self.movingButton:
            #     self.movingButton.move(source.pos() + event.pos() - self.startPos)
            elif event.type() == QEvent.MouseButtonRelease and self.movingButton:
                self.movingButton.move(source.pos() + event.pos() - self.startPos)
                self.movingButton = None
        return super().eventFilter(source, event)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM