溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

python3+PyQt5重新實(shí)現(xiàn)自定義數(shù)據(jù)拖放處理

發(fā)布時(shí)間:2020-10-18 08:52:33 來(lái)源:腳本之家 閱讀:188 作者:basisworker 欄目:開(kāi)發(fā)技術(shù)

本文分成兩部分,第一部分通過(guò)python3+PyQt5實(shí)現(xiàn)自定義數(shù)據(jù)的拖放操作。第二部分則對(duì)第一部分的程序進(jìn)行修改,增加拖放操作時(shí),菜單提示是否移動(dòng)或拷貝,還有可以通過(guò)ctrl鍵盤來(lái)設(shè)置移動(dòng)過(guò)程中拷貝源而非會(huì)將源刪除。

自定義數(shù)據(jù)MIME數(shù)據(jù)類型QMimeData,MIME是一種用于處理具有多個(gè)組成部分的自定義數(shù)據(jù)的標(biāo)準(zhǔn)化格式。MIME數(shù)據(jù)由一個(gè)數(shù)據(jù)類型和一個(gè)子類型構(gòu)成–例如,text/plain,text/html,image/png,要處理自定義MIME數(shù)據(jù),就必須要選用一種自定義數(shù)據(jù)類型和一種子類型,然后將數(shù)據(jù)封裝到QMimeData對(duì)象中。本例子中,我們創(chuàng)建端為application/x-icon-and-text類型的新MIME數(shù)據(jù)。

注:

dragEnterEvent這是一個(gè)拖拽事件的函數(shù),我們把文件拖拽進(jìn)程序界面打開(kāi),之前必須setAcceptDrops(true)了以后拖拽,但是只設(shè)置acceptDrops還不夠,還需要在dragEnterEvent事件中對(duì)拖入的對(duì)象進(jìn)行篩選,判斷mimeData的類型是否是你能處理的,如果是,則調(diào)用event.acceptProposedAction()放行。拖放結(jié)束后會(huì)產(chǎn)生dropEvent事件,在那里進(jìn)行最后的放置操作??傊@是拖拽事件函數(shù)的一個(gè)篩選事件并放置的函數(shù)。

第一部分:

#!/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_()

運(yùn)行結(jié)果:

python3+PyQt5重新實(shí)現(xiàn)自定義數(shù)據(jù)拖放處理

第二部分:

#!/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_()

運(yùn)行結(jié)果:

python3+PyQt5重新實(shí)現(xiàn)自定義數(shù)據(jù)拖放處理

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI