pyqt QTreeWidget 项目下降信号

pyqt QTreeWidget signal on item drop

每当有东西被拖放到我的自定义 QTreeWidget 时,我需要在我的应用程序中启用一个按钮。

我将 QTreeWidget 子类化以实现自定义数据的拖放。但是我无法找到一种方法来在将某些内容放入我的自定义 QTreeWidget 时得到通知。我找不到 QTreeWidget signal 来执行此操作。当然,QTreeWidget 的 dropEvent() 将在每次删除某些内容时被调用,但这对实现我正在尝试做的事情没有多大帮助。

这里是我实例化自定义 QTreeWidget 的地方,它接受来自另一个小部件的放置,

from PyQt4 import QtCore, QtGui
import MyTreeWidget

class TestWindow(QtGui.QDialog):
  def __init__(self, parent=None):
    super(TestWindow, self).__init__(parent)
    self.myTreeWidget = MyTreeWidget.MyTreeWidget()
    ...
    #self.myTreeWidget.onItemDropped.connect(self.doSomethingOnItemDropped) <== I am looking for something like this

  def doSomethingOnItemDropped(self):
    # Enable certain button 

  ...

然后,这是我如何对 QTreeWidget 进行子分类,

import sys
from PyQt4 import QtGui, QtCore

class MyTreeWidget(QtGui.QTreeWidget):
  def __init__(self, parent=None):
    super(MyTreeWidget, self).__init__(parent)
    self.setAcceptDrops(True)

  def dropEvent(self, event): 
    if (event.mimeData().hasFormat('application/x-icon-and-text')):
      event.acceptProposedAction()
      data = event.mimeData().data("application/x-icon-and-text")
      stream= QtCore.QDataStream(data, QtCore.QIODevice.ReadOnly)
      text = QtCore.QString()
      icon = QtGui.QIcon()
      stream >> text >> icon
      item = QtGui.QTreeWidgetItem(self)
      item.setText(0, text)
      item.setIcon(0, icon)
      self.addTopLevelItem(item)      
    else:
      event.ignore() 

  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(QtCore.Qt.CopyAction)
      event.accept()
    else:
      event.ignore()

有什么想法吗?谢谢!

更新这对我有用

根据@ekhumoro 的评论,我为我的自定义 QTreeWidget 定义了一个自定义信号 itemDropped,它在 dropEvent() 事件处理程序中发出。

    import sys
    from PyQt4 import QtGui, QtCore

    class MyTreeWidget(QtGui.QTreeWidget):

      itemDropped = QtCore.pyqtSignal()

      def __init__(self, parent=None):
        super(MyTreeWidget, self).__init__(parent)
        self.setAcceptDrops(True)

      def dropEvent(self, event): 
        if (event.mimeData().hasFormat('application/x-icon-and-text')):
          event.acceptProposedAction()
          data = event.mimeData().data("application/x-icon-and-text")
          stream= QtCore.QDataStream(data, QtCore.QIODevice.ReadOnly)
          text = QtCore.QString()
          icon = QtGui.QIcon()
          stream >> text >> icon
          item = QtGui.QTreeWidgetItem(self)
          item.setText(0, text)
          item.setIcon(0, icon)
          self.addTopLevelItem(item)
          self.itemDropped.emit()
        else:
          event.ignore() 

在我的应用中,

    from PyQt4 import QtCore, QtGui
    import MyTreeWidget

    class TestWindow(QtGui.QDialog):
      def __init__(self, parent=None):
        super(TestWindow, self).__init__(parent)
        self.myTreeWidget = MyTreeWidget.MyTreeWidget()
        self.myTreeWidget.itemDropped.connect(self.doSomethingOnItemDropped) 
        ...

      def doSomethingOnItemDropped(self):
        # Enable certain button 

      ...

您可以定义自定义信号并从 dropEvent:

发射它
class MyTreeWidget(QtGui.QTreeWidget):
    itemDropped = QtCore.pyqtSignal()

    def dropEvent(self, event): 
        if (event.mimeData().hasFormat('application/x-icon-and-text')):
            ...
            self.itemDropped.emit()

在 PySide2 中你可以使用这个:

class MyTreeWidget(QtGui.QTreeWidget):

    itemDropped = QtCore.Signal()

    def dropEvent(self, event): 
        if (event.mimeData().hasFormat('application/x-icon-and-text')):

            self.itemDropped.emit()