python3+PyQt5重新实现自定义数据拖放处理

 更新时间:2018年04月19日 15:50:05   作者:basisworker   我要评论
这篇文章主要为大家详细介绍了python3+PyQt5重新实现自定义数据拖放处理,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文分成两部分,第一部分通过python3+PyQt5实现自定义数据的拖放操作。第二部分则对第一部分的程序进行修改,增加拖放操作时,菜单提示是否移动或拷贝,还有可以通过ctrl键盘来设置移动过程中拷贝源而非会将源删除。

自定义数据MIME数据类型QMimeData,MIME是一种用于处理具有多个组成部分的自定义数据的标准化格式。MIME数据由一个数据类型和一个子类型构成–例如,text/plain,text/html,image/png,要处理自定义MIME数据,就必须要选用一种自定义数据类型和一种子类型,然后将数据封装到QMimeData对象中。本例子中,我们创建端为application/x-icon-and-text类型的新MIME数据。

注:

dragEnterEvent这是一个拖拽事件的函数,我们把文件拖拽进程序界面打开,之前必须setAcceptDrops(true)了以后拖拽,但是只设置acceptDrops还不够,还需要在dragEnterEvent事件中对拖入的对象进行筛选,判断mimeData的类型是否是你能处理的,如果是,则调用event.acceptProposedAction()放行。拖放结束后会产生dropEvent事件,在那里进行最后的放置操作。总之这是拖拽事件函数的一个筛选事件并放置的函数。

第一部分:

#!/usr/bin/env python3
import os
import sys
from PyQt5.QtCore import (QByteArray, QDataStream, QIODevice, QMimeData,
    QPoint, QSize, Qt)
from PyQt5.QtWidgets import (QApplication, QDialog,QGridLayout,
               QLineEdit, QListWidget,QListWidgetItem, QWidget)
from PyQt5.QtGui import QIcon,QColor,QPainter,QFontMetricsF,QDrag
class DropLineEdit(QLineEdit):
  def __init__(self, parent=None):
    super(DropLineEdit, self).__init__(parent)
    self.setAcceptDrops(True)

  def dragEnterEvent(self, event):
    if event.mimeData().hasFormat("application/x-icon-and-text"):
      event.accept()
    else:
      event.ignore()

  def dragMoveEvent(self, event):
    if event.mimeData().hasFormat("application/x-icon-and-text"):
      event.setDropAction(Qt.CopyAction)
      event.accept()
    else:
      event.ignore()

  def dropEvent(self, event):
    if event.mimeData().hasFormat("application/x-icon-and-text"):
      data = event.mimeData().data("application/x-icon-and-text")
      stream = QDataStream(data, QIODevice.ReadOnly)
      text = ""
      #stream >> text
      text=stream.readQString()
      self.setText(text)
      event.setDropAction(Qt.CopyAction)
      event.accept()
    else:
      event.ignore()

class DnDListWidget(QListWidget):
  def __init__(self, parent=None):
    super(DnDListWidget, self).__init__(parent)
    self.setAcceptDrops(True)
    self.setDragEnabled(True)

  def dragEnterEvent(self, event):
    if event.mimeData().hasFormat("application/x-icon-and-text"):
      event.accept()
    else:
      event.ignore()

  def dragMoveEvent(self, event):
    if event.mimeData().hasFormat("application/x-icon-and-text"):
      event.setDropAction(Qt.MoveAction)
      event.accept()
    else:
      event.ignore()

  def dropEvent(self, event):
    if event.mimeData().hasFormat("application/x-icon-and-text"):
      data = event.mimeData().data("application/x-icon-and-text")
      stream = QDataStream(data, QIODevice.ReadOnly)
      text = ""
      icon = QIcon()
      #stream >> text >> icon
      text=stream.readQString()
      stream >> icon
      item = QListWidgetItem(text, self)
      item.setIcon(icon)
      event.setDropAction(Qt.MoveAction)
      event.accept()
    else:
      event.ignore()

  def startDrag(self, dropActions):
    item = self.currentItem()
    icon = item.icon()
    data = QByteArray()
    stream = QDataStream(data, QIODevice.WriteOnly)
    #stream << item.text() << icon
    stream.writeQString(item.text())
    stream << icon
    mimeData = QMimeData()
    mimeData.setData("application/x-icon-and-text", data)
    drag = QDrag(self)
    drag.setMimeData(mimeData)
    pixmap = icon.pixmap(24, 24)
    drag.setHotSpot(QPoint(12, 12))
    drag.setPixmap(pixmap)
    if drag.exec(Qt.MoveAction) == Qt.MoveAction:
      self.takeItem(self.row(item))

class DnDWidget(QWidget):
  def __init__(self, text, icon=QIcon(), parent=None):
    super(DnDWidget, self).__init__(parent)
    self.setAcceptDrops(True)
    self.text = text
    self.icon = icon

  def minimumSizeHint(self):
    fm = QFontMetricsF(self.font())
    if self.icon.isNull():
      return QSize(fm.width(self.text), fm.height() * 1.5)
    return QSize(34 + fm.width(self.text), max(34, fm.height() * 1.5))

  def paintEvent(self, event):
    height = QFontMetricsF(self.font()).height()
    painter = QPainter(self)
    painter.setRenderHint(QPainter.Antialiasing)
    painter.setRenderHint(QPainter.TextAntialiasing)
    painter.fillRect(self.rect(), QColor(Qt.yellow).lighter())
    if self.icon.isNull():
      painter.drawText(10, height, self.text)
    else:
      pixmap = self.icon.pixmap(24, 24)
      painter.drawPixmap(0, 5, pixmap)
      painter.drawText(34, height,
               self.text + " (Drag to or from me!)")

  def dragEnterEvent(self, event):
    if event.mimeData().hasFormat("application/x-icon-and-text"):
      event.accept()
    else:
      event.ignore()

  def dragMoveEvent(self, event):
    if event.mimeData().hasFormat("application/x-icon-and-text"):
      event.setDropAction(Qt.CopyAction)
      event.accept()
    else:
      event.ignore()

  def dropEvent(self, event):
    if event.mimeData().hasFormat("application/x-icon-and-text"):
      data = event.mimeData().data("application/x-icon-and-text")
      stream = QDataStream(data, QIODevice.ReadOnly)
      self.text = ""
      self.icon = QIcon()
      #stream >> self.text >> self.icon
      self.text=stream.readQString()
      stream>>self.icon
      event.setDropAction(Qt.CopyAction)
      event.accept()
      self.updateGeometry()
      self.update()
    else:
      event.ignore()

  def mouseMoveEvent(self, event):
    self.startDrag()
    QWidget.mouseMoveEvent(self, event)

  def startDrag(self):
    icon = self.icon
    if icon.isNull():
      return
    data = QByteArray()
    stream = QDataStream(data, QIODevice.WriteOnly)
    #stream << self.text << icon
    stream.writeQString(self.text)
    stream<<icon
    mimeData = QMimeData()
    mimeData.setData("application/x-icon-and-text", data)
    drag = QDrag(self)
    drag.setMimeData(mimeData)
    pixmap = icon.pixmap(24, 24)
    drag.setHotSpot(QPoint(12, 12))
    drag.setPixmap(pixmap)
    drag.exec(Qt.CopyAction)

class Form(QDialog):
  def __init__(self, parent=None):
    super(Form, self).__init__(parent)
    dndListWidget = DnDListWidget()
    path = os.path.dirname(__file__)
    for image in sorted(os.listdir(os.path.join(path, "images"))):
      if image.endswith(".png"):
        item = QListWidgetItem(image.split(".")[0].capitalize())
        item.setIcon(QIcon(os.path.join(path,
                  "images/{0}".format(image))))
        dndListWidget.addItem(item)
    dndIconListWidget = DnDListWidget()
    dndIconListWidget.setViewMode(QListWidget.IconMode)
    dndWidget = DnDWidget("Drag to me!")
    dropLineEdit = DropLineEdit()
    layout = QGridLayout()
    layout.addWidget(dndListWidget, 0, 0)
    layout.addWidget(dndIconListWidget, 0, 1)
    layout.addWidget(dndWidget, 1, 0)
    layout.addWidget(dropLineEdit, 1, 1)
    self.setLayout(layout)
    self.setWindowTitle("Custom Drag and Drop")
if __name__ == "__main__":
  app = QApplication(sys.argv)
  form = Form()
  form.show()
  app.exec_()

运行结果:

第二部分:

#!/usr/bin/env python3
import os
import sys
from PyQt5.QtCore import (QByteArray, QDataStream, QIODevice, QMimeData,
    QPoint, QSize, Qt)
from PyQt5.QtWidgets import (QApplication, QDialog,QGridLayout,
               QLineEdit, QListWidget,QListWidgetItem, QWidget,QMenu)
from PyQt5.QtGui import QIcon,QColor,QPainter,QFontMetricsF,QDrag,QCursor
class DropLineEdit(QLineEdit):
  def __init__(self, parent=None):
    super(DropLineEdit, self).__init__(parent)
    self.setAcceptDrops(True)

  def dragEnterEvent(self, event):
    if event.mimeData().hasFormat("application/x-icon-and-text"):
      event.accept()
    else:
      event.ignore()

  def dragMoveEvent(self, event):
    if event.mimeData().hasFormat("application/x-icon-and-text"):
      event.setDropAction(Qt.CopyAction)
      event.accept()
    else:
      event.ignore()

  def dropEvent(self, event):
    if event.mimeData().hasFormat("application/x-icon-and-text"):
      data = event.mimeData().data("application/x-icon-and-text")
      stream = QDataStream(data, QIODevice.ReadOnly)
      text = ""
      text=stream.readQString()
      self.setText(text)
      event.setDropAction(Qt.CopyAction)
      event.accept()
    else:
      event.ignore()

class DnDMenuListWidget(QListWidget):
  def __init__(self, parent=None):
    super(DnDMenuListWidget, self).__init__(parent)
    self.setAcceptDrops(True)
    self.setDragEnabled(True)
    self.dropAction = Qt.CopyAction

  def dragEnterEvent(self, event):
    if event.mimeData().hasFormat("application/x-icon-and-text"):
      event.accept()
    else:
      event.ignore()

  def dragMoveEvent(self, event):
    if event.mimeData().hasFormat("application/x-icon-and-text"):
      event.setDropAction(Qt.MoveAction)
      event.accept()
    else:
      event.ignore()

  def dropEvent(self, event):
    if event.mimeData().hasFormat("application/x-icon-and-text"):
      data = event.mimeData().data("application/x-icon-and-text")
      stream = QDataStream(data, QIODevice.ReadOnly)
      text = ""
      icon = QIcon()
      text=stream.readQString()
      stream>>icon
      menu = QMenu(self)
      menu.addAction("&Copy", self.setCopyAction)
      menu.addAction("&Move", self.setMoveAction)
      if menu.exec_(QCursor.pos()):
        item = QListWidgetItem(text, self)
        item.setIcon(icon)
        event.setDropAction(self.dropAction)
        event.accept()
        return
      else:
        event.setDropAction(Qt.IgnoreAction)
    event.ignore()

  def setCopyAction(self):
    self.dropAction = Qt.CopyAction

  def setMoveAction(self):
    self.dropAction = Qt.MoveAction

  def startDrag(self, dropActions):
    item = self.currentItem()
    icon = item.icon()
    data = QByteArray()
    stream = QDataStream(data, QIODevice.WriteOnly)
    stream.writeQString(item.text())
    stream<<icon
    mimeData = QMimeData()
    mimeData.setData("application/x-icon-and-text", data)
    drag = QDrag(self)
    drag.setMimeData(mimeData)
    pixmap = icon.pixmap(24, 24)
    drag.setHotSpot(QPoint(12, 12))
    drag.setPixmap(pixmap)
    if (drag.exec(Qt.MoveAction|Qt.CopyAction) == Qt.MoveAction):
      self.takeItem(self.row(item))

class DnDCtrlListWidget(QListWidget):
  def __init__(self, parent=None):
    super(DnDCtrlListWidget, self).__init__(parent)
    self.setAcceptDrops(True)
    self.setDragEnabled(True)

  def dragEnterEvent(self, event):
    if event.mimeData().hasFormat("application/x-icon-and-text"):
      event.accept()
    else:
      event.ignore()

  def dragMoveEvent(self, event):
    if event.mimeData().hasFormat("application/x-icon-and-text"):
      action = Qt.MoveAction
      if event.keyboardModifiers() & Qt.ControlModifier:
        action = Qt.CopyAction
      event.setDropAction(action)
      event.accept()
    else:
      event.ignore()

  def dropEvent(self, event):
    if event.mimeData().hasFormat("application/x-icon-and-text"):
      data = event.mimeData().data("application/x-icon-and-text")
      stream = QDataStream(data, QIODevice.ReadOnly)
      text = ""
      icon = QIcon()
      text=stream.readQString()
      stream>>icon
      item = QListWidgetItem(text, self)
      item.setIcon(icon)
      action = Qt.MoveAction
      if event.keyboardModifiers() & Qt.ControlModifier:
        action = Qt.CopyAction
      event.setDropAction(action)
      event.accept()
    else:
      event.ignore()

  def startDrag(self, dropActions):
    item = self.currentItem()
    icon = item.icon()
    data = QByteArray()
    stream = QDataStream(data, QIODevice.WriteOnly)
    stream.writeQString(item.text())
    stream<<icon
    mimeData = QMimeData()
    mimeData.setData("application/x-icon-and-text", data)
    drag = QDrag(self)
    drag.setMimeData(mimeData)
    pixmap = icon.pixmap(24, 24)
    drag.setHotSpot(QPoint(12, 12))
    drag.setPixmap(pixmap)
    if (drag.exec(Qt.MoveAction|Qt.CopyAction) == Qt.MoveAction):
      self.takeItem(self.row(item))

class DnDWidget(QWidget):
  def __init__(self, text, icon=QIcon(), parent=None):
    super(DnDWidget, self).__init__(parent)
    self.setAcceptDrops(True)
    self.text = text
    self.icon = icon

  def minimumSizeHint(self):
    fm = QFontMetricsF(self.font())
    if self.icon.isNull():
      return QSize(fm.width(self.text), fm.height() * 1.5)
    return QSize(34 + fm.width(self.text), max(34, fm.height() * 1.5))

  def paintEvent(self, event):
    height = QFontMetricsF(self.font()).height()
    painter = QPainter(self)
    painter.setRenderHint(QPainter.Antialiasing)
    painter.setRenderHint(QPainter.TextAntialiasing)
    painter.fillRect(self.rect(), QColor(Qt.yellow).lighter())
    if self.icon.isNull():
      painter.drawText(10, height, self.text)
    else:
      pixmap = self.icon.pixmap(24, 24)
      painter.drawPixmap(0, 5, pixmap)
      painter.drawText(34, height,
               self.text + " (Drag to or from me!)")

  def dragEnterEvent(self, event):
    if event.mimeData().hasFormat("application/x-icon-and-text"):
      event.accept()
    else:
      event.ignore()

  def dragMoveEvent(self, event):
    if event.mimeData().hasFormat("application/x-icon-and-text"):
      event.setDropAction(Qt.CopyAction)
      event.accept()
    else:
      event.ignore()

  def dropEvent(self, event):
    if event.mimeData().hasFormat("application/x-icon-and-text"):
      data = event.mimeData().data("application/x-icon-and-text")
      stream = QDataStream(data, QIODevice.ReadOnly)
      self.text = ""
      self.icon = QIcon()
      self.text=stream.readQString()
      stream>>self.icon
      event.setDropAction(Qt.CopyAction)
      event.accept()
      self.updateGeometry()
      self.update()
    else:
      event.ignore()

  def mouseMoveEvent(self, event):
    self.startDrag()
    QWidget.mouseMoveEvent(self, event)

  def startDrag(self):
    icon = self.icon
    if icon.isNull():
      return
    data = QByteArray()
    stream = QDataStream(data, QIODevice.WriteOnly)
    stream.writeQString(self.text)
    stream<<icon
    mimeData = QMimeData()
    mimeData.setData("application/x-icon-and-text", data)
    drag = QDrag(self)
    drag.setMimeData(mimeData)
    pixmap = icon.pixmap(24, 24)
    drag.setHotSpot(QPoint(12, 12))
    drag.setPixmap(pixmap)
    drag.exec(Qt.CopyAction)

class Form(QDialog):
  def __init__(self, parent=None):
    super(Form, self).__init__(parent)
    dndListWidget = DnDMenuListWidget()
    path = os.path.dirname(__file__)
    for image in sorted(os.listdir(os.path.join(path, "images"))):
      if image.endswith(".png"):
        item = QListWidgetItem(image.split(".")[0].capitalize())
        item.setIcon(QIcon(os.path.join(path,
                  "images/{0}".format(image))))
        dndListWidget.addItem(item)
    dndIconListWidget = DnDCtrlListWidget()
    dndIconListWidget.setViewMode(QListWidget.IconMode)
    dndWidget = DnDWidget("Drag to me!")
    dropLineEdit = DropLineEdit()
    layout = QGridLayout()
    layout.addWidget(dndListWidget, 0, 0)
    layout.addWidget(dndIconListWidget, 0, 1)
    layout.addWidget(dndWidget, 1, 0)
    layout.addWidget(dropLineEdit, 1, 1)
    self.setLayout(layout)
    self.setWindowTitle("Custom Drag and Drop")
if __name__ == "__main__":
  app = QApplication(sys.argv)
  form = Form()
  form.show()
  app.exec_()

运行结果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持澳门金沙网上娱乐。

相关文章

  • python仿抖音表白神器

    python仿抖音表白神器

    这篇文章主要教大家制作python抖音表白神器,仿制抖音表白小软件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-04-04
  • Python 中Django安装和使用教程详解

    Python 中Django安装和使用教程详解

    这篇文章主要介绍了python中Django安装和使用教程,本文图文并茂给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-07-07
  • 浅谈python中的__init__、__new__和__call__方法

    浅谈python中的__init__、__new__和__call__方法

    这篇文章主要给大家介绍了关于python中__init__、__new__和__call__方法的相关资料,文中通过示例代码介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友可以参考学习,下面来跟着小编一起看看吧。
    2017-07-07
  • 在Pycharm terminal中字体大小设置的方法

    在Pycharm terminal中字体大小设置的方法

    今天小编就为大家分享一篇在Pycharm terminal中字体大小设置的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-01-01
  • python如何通过twisted实现金沙国际官网异步插入

    python如何通过twisted实现金沙国际官网异步插入

    这篇文章主要为大家详细介绍了python如何通过twisted实现金沙国际官网异步插入,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-03-03
  • Python求两个list的差集、交集与并集的方法

    Python求两个list的差集、交集与并集的方法

    这篇文章主要介绍了Python求两个list的差集、交集与并集的方法,是Python集合数组操作中常用的技巧,需要的朋友可以参考下
    2014-11-11
  • Django中的静态文件管理过程解析

    Django中的静态文件管理过程解析

    这篇文章主要介绍了Django中的静态文件管理过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • Python3 单行多行万能正则匹配方法

    Python3 单行多行万能正则匹配方法

    今天小编就为大家分享一篇Python3 单行多行万能正则匹配方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-01-01
  • Python调用C语言的实现

    Python调用C语言的实现

    这篇文章主要介绍了Python调用C语言的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-07-07
  • 使用python在本地电脑上快速处理数据

    使用python在本地电脑上快速处理数据

    这篇文章主要介绍了使用python在本地电脑上快速处理数据的相关资料,需要的朋友可以参考下
    2017-06-06

最新评论