PyQt5实现拖放功能


Posted in Python onApril 25, 2018

在这节教程中,我们将探讨PyQt5中的拖放操作。

在计算机图形用户界面(GUI)中,拖放是在某个虚拟对象上点击并拖动到另一个位置或虚拟对象上的操作。它通常用于调用多个动作,或为两个抽象对象创建某些联系。

拖放是图形用户界面的一部分。拖放可以使用户直观地完成某些复杂的操作。

通常我们可以对两种事物进行拖放操作:数据或某些图形对象。如果我们将某个应用中的图片拖放到另一个应用,我们拖放的是二进制数据。如果将Firefox的某个标签页拖放到其他地方,我们拖放的是一个图形组件。

简单的拖放

在第一个示例中我们要创建一个QLineEdit和一个QPushButton,并通过将LineEdit中的文本拖放到按钮上来改变按钮的标签。

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


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(190, 65)

  self.setWindowTitle("Simple drag & drop")
  self.setGeometry(300, 300, 300, 150)
  self.show()


if __name__ == "__main__":
 app = QApplication(sys.argv)
 ex = Example()
 sys.exit(app.exec_())

这个示例演示了一个简单的拖放操作。

class Button(QPushButton):

 def __init__(self, title, parent):
  super().__init__(title, parent)

  self.setAcceptDrops(True)

我们需要重新实现某些方法才能使QPushButton接受拖放操作。因此我们创建了继承自QPushButton的Button类。

self.setAcceptDrops(True)

使该控件接受drop(放下)事件。

def dragEnterEvent(self, e):

 if e.mimeData().hasFormat('text/plain'):
  e.accept()
 else:
  e.ignore()

首先我们重新实现了dragEnterEvent()方法,并设置可接受的数据类型(在这里是普通文本)。

def dropEvent(self, e):

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

通过重新实现dropEvent()方法,我们定义了在drop事件发生时的行为。这里我们改变了按钮的文字。

edit = QLineEdit('', self)
edit.setDragEnabled(True)

QLineEdit内置了对drag(拖动)操作的支持。我们只需要调用setDragEnabled()方法就可以了。

PyQt5实现拖放功能

PyQt5实现拖放功能

PyQt5实现拖放功能

拖放一个按钮

在下面的示例中我们将演示如何对一个按钮控件进行拖放。

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


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())

  dropAcion = 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()


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类,并重新实现了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的拖放的数据传输的支持。

dropAction = drag.exec_(Qt.MoveAction)

Drag对象的exec_()方法用于启动拖放操作。

def mousePressEvent(self, e):

 QPushButton.mousePressEvent(self, e)

 if e.button() == Qt.LeftButton:
  print('press')

鼠标左击按钮时我们会在控制台打印‘press'。注意我们也调用了父按钮的mousePressEvent()方法。否则会看不到按钮的按下效果。

position = e.pos()
self.button.move(position)

在dropEvent()方法中,我们要为松开鼠标后的操作进行编码,并完成drop操作。即找出鼠标指针的当前位置,并将按钮移动过去。

e.setDropAction(Qt.MoveAction)
e.accept()

我们定义了drop动作的类型。这里是move动作。

PyQt5实现拖放功能

PyQt5实现拖放功能

本节教程讲解了拖放操作。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python迭代器实例简析
Sep 25 Python
用Python的Django框架完成视频处理任务的教程
Apr 02 Python
Python实现TCP协议下的端口映射功能的脚本程序示例
Jun 14 Python
TensorFLow用Saver保存和恢复变量
Mar 10 Python
Python可变参数*args和**kwargs用法实例小结
Apr 27 Python
使用python将图片格式转换为ico格式的示例
Oct 22 Python
简单了解python的break、continue、pass
Jul 08 Python
python中单下划线(_)和双下划线(__)的特殊用法
Aug 29 Python
学习Django知识点分享
Sep 11 Python
Python开发企业微信机器人每天定时发消息实例
Mar 17 Python
python 多线程中join()的作用
Oct 29 Python
python 实现有道翻译功能
Feb 26 Python
wx.CheckBox创建复选框控件并响应鼠标点击事件
Apr 25 #Python
wxPython实现窗口用图片做背景
Apr 25 #Python
django 发送手机验证码的示例代码
Apr 25 #Python
python3+PyQt5实现自定义分数滑块部件
Apr 24 #Python
详解tensorflow载入数据的三种方式
Apr 24 #Python
关于Tensorflow中的tf.train.batch函数的使用
Apr 24 #Python
TensorFlow入门使用 tf.train.Saver()保存模型
Apr 24 #Python
You might like
PHP中执行MYSQL事务解决数据写入不完整等情况
2014/01/07 PHP
php多个文件及图片上传实例详解
2014/11/10 PHP
Zend Framework开发入门经典教程
2016/03/23 PHP
详解PHP函数 strip_tags 处理字符串缺陷bug
2017/06/11 PHP
Laravel 5.4.36中session没有保存成功问题的解决
2018/02/19 PHP
JS实现浏览器菜单命令
2006/09/05 Javascript
不用写JS也能使用EXTJS视频演示
2008/12/29 Javascript
js动态为代码着色显示行号
2013/05/29 Javascript
jquery图片滚动放大代码分享(2)
2015/08/28 Javascript
JavaScript必知必会(三) String .的方法来自何方
2016/06/08 Javascript
学习使用bootstrap的modal和carousel
2016/12/09 Javascript
nodejs中art-template模板语法的引入及冲突解决方案
2017/11/07 NodeJs
ES6/JavaScript使用技巧分享
2017/12/14 Javascript
JS计算输出100元钱买100只鸡问题的解决方法
2018/01/04 Javascript
如何利用nodejs自动定时发送邮件提醒(超实用)
2020/12/01 NodeJs
使用python实现省市三级菜单效果
2016/01/20 Python
python安装oracle扩展及数据库连接方法
2017/02/21 Python
Python 实现简单的shell sed替换功能(实例讲解)
2017/09/29 Python
python+django加载静态网页模板解析
2017/12/12 Python
pyqt5 删除layout中的所有widget方法
2019/06/25 Python
如何基于python操作excel并获取内容
2019/12/24 Python
Python 3.9的到来到底是意味着什么
2020/10/14 Python
python中@property的作用和getter setter的解释
2020/12/22 Python
关键字throw与throws的用法差异
2016/11/22 面试题
2014村务公开实施方案
2014/02/25 职场文书
会计员岗位职责
2014/03/15 职场文书
4s店市场专员岗位职责
2014/04/09 职场文书
《风筝》教学反思
2014/04/10 职场文书
理想点亮人生演讲稿
2014/05/21 职场文书
优秀的应届生自荐信
2014/05/23 职场文书
经典团队口号
2014/06/06 职场文书
教师求职信
2014/06/17 职场文书
社区四风存在问题及整改措施
2014/10/26 职场文书
导游词之沈阳清昭陵
2019/12/28 职场文书
判断Python中的Nonetype类型
2021/05/25 Python
关于Spring配置文件加载方式变化引发的异常详解
2022/01/18 Java/Android