拖拽

在GUI里,拖放是指⽤户点击⼀个虚拟的对象,拖动,然后放置到另外⼀个对象上面的动作。⼀般 情况下,需要调用很多动作和方法,创建很多变量。

拖放能让用户很直观的操作很复杂的逻辑。 ⼀般情况下,我们可以拖放两种东西:数据和图形界面。把⼀个图像从⼀个应用拖放到另外⼀个应用上的实质是操作⼆进制数据。把⼀个表格从Firefox上拖放到另外⼀个位置的实质是操作一个图形组。

简单的拖放

本例使用了 QLineEdit 和 QPushButton 。把⼀个⽂本从编辑框⾥拖到按钮上,更新按钮上的标签 (文字)。


from PyQt5.QtWidgets import (QPushButton, QWidget,
 QLineEdit, QApplication)
import sys


class Button(QPushButton):
    def __init__(self, title, parent):
        super().__init__(title, parent)
        self.setAcceptDrops(True)

    def dragEnterEvent(self, e):
        if e.mimeData().hasFormat('text/plain'):
            e.accept()
        else:
            e.ignore()

    def dropEvent(self, e):
        self.setText(e.mimeData().text())


class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        edit = QLineEdit('', self)
        edit.setDragEnabled(True)
        edit.move(30, 65)
        button = Button("Button", self)
        button.move(290, 65)
        self.setWindowTitle('Simple drag and drop')
        self.setGeometry(300, 300, 400, 250)


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


class Button(QPushButton):
    def __init__(self, title, parent):
        super().__init__(title, parent)
        self.setAcceptDrops(True)

为了完成预定目标,我们要重构⼀些方法。首先用 QPushButton 上构造⼀个按钮实例。

        self.setAcceptDrops(True)

激活组件的拖拽事件。def dragEnterEvent(self, e):
        if e.mimeData().hasFormat(‘text/plain’):
            e.accept()
        else:
            e.ignore()

⾸先,我们重构了 dragEnterEvent() 方法。设定好接受拖拽的数据类型(plain text)。

        def dropEvent(self, e):

                self.setText(e.mimeData().text())

然后重构 dropEvent() 方法,更改按钮接受鼠标的释放事件的默认行为。

        edit = QLineEdit(”, self)

        edit.setDragEnabled(True)

QLineEdit 默认支持拖拽操作,所以我们只要调用 setDragEnabled() 方法使用就行了。

程序展示:

拖放按钮组件

这个例子展示怎么拖放⼀个button组件。

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())
        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.button = Button('Button', self)
        self.button.move(100, 65)
        self.setWindowTitle('Click or Move')
        self.setGeometry(300, 300, 380, 250)
    def dragEnterEvent(self, e):
        e.accept()
    def dropEvent(self, e):
        position = e.pos()
        self.button.move(position)
        e.setDropAction(Qt.MoveAction)
        e.accept()


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

上面的例⼦中,窗口上有⼀个 QPushButton 组件。左键点击按钮,控制台就会输出 press 。右键可以点击然后拖动按钮。

class Button(QPushButton):

        def __init__(self, title, parent):

                super().__init__(title, parent)

从 QPushButton 继承⼀个 Button 类,然后重构 QPushButton 的两个⽅法: mouseMoveEvent() 和 mousePressEvent() . mouseMoveEvent() 是拖拽开始的事件。

        if e.buttons() != Qt.RightButton:

                return

我们只劫持按钮的右键事件,左键的操作还是默认行为。

        mimeData = QMimeData()

        drag = QDrag(self)

        drag.setMimeData(mimeData)

        drag.setHotSpot(e.pos() – self.rect().topLeft())

创建⼀个 QDrag 对象,用来传输MIME-based数据。

        drag.exec_(Qt.MoveAction)

拖放事件开始时,用到的处理函数式 start() .

def mousePressEvent(self, e):

        if e.button() == Qt.LeftButton:

                print(‘press’)

左键点击按钮,会在控制台输出“press”。

position = e.pos()

        self.button.move(position)

在 dropEvent() ⽅法里,我们定义了按钮按下后和释放后的行为,获得鼠标移动的位置,然后把按 钮放到这个地方。

        e.setDropAction(Qt.MoveAction)

        e.accept()

指定放下的动作类型为moveAction。

程序展示:

 

 


版权声明:本文为weixin_64338372原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/weixin_64338372/article/details/128219690