pyqt QTreeWidget 项目掉落信号

问题描述 投票:0回答:2

每当有东西被拖放到我的自定义 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 

      ...
python pyqt drag-and-drop signals-slots qtreewidget
2个回答
2
投票

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

dropEvent
:

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

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

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()
© www.soinside.com 2019 - 2024. All rights reserved.