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

运行结果:

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

您可能感兴趣的文章:

  • python3+PyQt5实现自定义窗口部件Counters
  • python3+PyQt5实现支持多线程的页面索引器应用程序
  • python3+PyQt5+Qt Designer实现扩展对话框
  • python3+PyQt5+Qt Designer实现堆叠窗口部件
  • python3+PyQt5重新实现QT事件处理程序
  • python3使用pyqt5制作一个超简单浏览器的实例
  • Python3使用PyQt5制作简单的画板/手写板实例
  • python3.5 + PyQt5 +Eric6 实现的一个计算器代码
  • python3+PyQt5实现使用剪贴板做复制与粘帖示例
  • python3+PyQt5图形项的自定义和交互 python3实现page Designer应用程序
(0)

相关推荐

  • python3+PyQt5+Qt Designer实现堆叠窗口部件

    本文是对<Python Qt GUI快速编程>的第9章的堆叠窗口例子Vehicle Rental用Python3+PyQt5+Qt Designer进行改写. 第一部分无借用Qt Designer,完全用代码实现. 第二部分则借用Qt Designer,快速实现. 第一部分: import sys from PyQt5.QtCore import (Qt) from PyQt5.QtWidgets import (QApplication, QComboBox, QDialog, QDialo

  • python3.5 + PyQt5 +Eric6 实现的一个计算器代码

    目前可以实现简单的计算.计算前请重置,设计的时候默认数字是0,学了半天就做出来个这么个结果,bug不少. python3.5 + PyQt5 +Eric6 在windows7 32位系统可以完美运行 计算器,简单学了半天就画个图实现的存在bug,部分按钮还未实现,后续优化. 代码结构如图: jisuan.py import re #匹配整数或小数的乘除法,包括了开头存在减号的情况 mul_div=re.compile("(-?\d+)(\.\d+)?(\*|/)(-?\d+)(\.\d+)?&q

  • python3+PyQt5实现支持多线程的页面索引器应用程序

    本文通过Python3+pyqt5实现了python Qt GUI 快速编程的19章的页面索引器应用程序例子. /home/yrd/eric_workspace/chap19/walker_ans.py #!/usr/bin/env python3 import codecs import html.entities import re import sys from PyQt5.QtCore import (QMutex, QThread,pyqtSignal,Qt) class Walker

  • Python3使用PyQt5制作简单的画板/手写板实例

    1.前言 版本:Python3.6.1 + PyQt5 写一个程序的时候需要用到画板/手写板,只需要最简单的那种.原以为网上到处都是,结果找了好几天,都没有找到想要的结果. 网上的要么是非python版的qt程序(要知道qt版本之间差异巨大,还是非同一语言的),改写难度太大.要么是PyQt4的老程序,很多都已经不能在PyQt5上运行了.要么是大神写的特别复杂的程序,简直是直接做出了一个Windows自带的画图版,只能膜拜~ 于是我只能在众多代码中慢慢寻找自己需要的那一小部分,然后不断地拼凑,不断

  • python3+PyQt5重新实现QT事件处理程序

    本文是对<Python Qt GUI快速编程>的第10章的例子events用Python3+PyQt5进行改写,涉及到重新实现QWidget的事件处理程序.本例子涉及到上下文菜单,鼠标事件,键盘事件,可作为重新实现事件处理程序的参考. 注:在创建上下文菜单最简单的方式使用Qwidget.addAction()把动作添加到窗口部件中,再把窗口部件的上下文菜单策略设置为Qt.ActionsContextMenu即可,但是如果像本例子一样要根据不同的状态来提供不同的选项,则要重新实现上下文菜单事件处

  • python3+PyQt5图形项的自定义和交互 python3实现page Designer应用程序

    本文通过Python3+PyQt5实现<python Qt Gui 快速编程>这本书的page Designer应用程序,采用QGraphicsView,QGraphicsScene,QGraphicsItem,这个程序包含有多个文本,图片和框的页面.有些图形类在PyQt5已过时,所以本代码改动幅度比较大.主要的类或方法的改变如下: QMatrix==>QTransform setMatrix==>setTransform rotate ==> setRotation 本例中

  • python3使用pyqt5制作一个超简单浏览器的实例

    我们使用的是QWebview模块,这里也主要是展示下QWebview的用法. 之前在网上找了半天的解析网页的内容,都不是很清楚. 这是核心代码: webview = Qwebview() webview.load(Qurl('http://www.cnblogs.com/Blaxon/')) webview.show() 完整代码(代码是拿的别的代码改得): from PyQt5.QtCore import * from PyQt5.QtWidgets import * from PyQt5.Q

  • python3+PyQt5+Qt Designer实现扩展对话框

    本文是对<Python Qt GUI快速编程>的第9章的扩展对话框例子Find and replace用Python3+PyQt5+Qt Designer进行改写. 第一部分无借用Qt Designer,完全用代码实现. 第二部分则借用Qt Designer,快速实现. 第一部分: import sys from PyQt5.QtCore import Qt,pyqtSignal from PyQt5.QtWidgets import (QApplication, QCheckBox, QDi

  • python3+PyQt5实现使用剪贴板做复制与粘帖示例

    本文是对<Python Qt GUI快速编程>的第10章的例子剪贴板用Python3+PyQt5进行改写,分别对文本,图片和html文本的复制与粘帖,三种做法大同小异. #!/usr/bin/env python3 import os import sys from PyQt5.QtCore import (QMimeData, Qt) from PyQt5.QtWidgets import (QApplication, QDialog, QGridLayout, QLabel, QPushB

  • python3+PyQt5实现自定义窗口部件Counters

    本文通过Python3+PyQt5实现自定义部件–Counters自定 窗口部件.这个窗口是3*3的网格.本文有两个例子如下: /home/yrd/eric_workspace/chap11/counters.py. /home/yrd/eric_workspace/chap11/counters_dnd.py 第二个例子在第一个例子的基础上实现能通过鼠标拖拽球到不同的网格中. /home/yrd/eric_workspace/chap11/counters.py #!/usr/bin/env

随机推荐