项目丢弃时的pyqt QTreeWidget信号

时间:2015-03-19 20:24:27

标签: python qt pyqt pyqt4 qtreewidget

我需要在应用程序中启用一个按钮,只要将某些内容放到我的自定义QTreeWidget中。

我对QTreeWidget进行了细分,以实现自定义数据的拖放。但是,当我的自定义QTreeWidget中有内容被删除时,我无法找到通知的方法。我无法找到QTreeWidget signal来执行此操作。当然,每次删除某些内容时都会调用QTreeWidget的dropEvent(),但这并不能帮助我实现我想要做的事情。

这里我实例化了接受来自另一个小部件的drop的自定义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 

      ...

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:0)

在PySide2中,您可以使用此功能:

class MyTreeWidget(QtGui.QTreeWidget):

    itemDropped = QtCore.Signal()

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

            self.itemDropped.emit()