Qt World Summit: Register Today!

Drag and drop between two QTreeWidgets

  • I have the following code and I can't figure out how to get it into the dropEvent() method. I can click and drag and it enters the dragEnterEvent() and when I move the cursor to the second QTreeWidget, TreeTwo) it enters the dragMoveEvent(). The Item clicked on moves between QTreeWidgets, but the program never enters the dropEvent().

    import sys
    import PySide

    from PySide.QtGui import *
    from PySide import QtCore, QtGui
    from PySide.QtGui import QMainWindow
    from PySide.QtGui import QApplication
    from PySide.QtCore import *
    from mainwindow_ui import Ui_MainWindow
    import platform

    class MainWindow(QMainWindow, Ui_MainWindow, QDialog):
    def init(self, parent=None):
    super(MainWindow, self).init(parent)

    self.com = communicate()


    def somefunc():
    print 'Helloooooooooooooooooooooooo'

    def whollycrap():
    print 'Suzannnnnnnnna'

    QtGui.QTreeWidget.connect(self.TreeTwo, QtCore.SIGNAL('dropEvent()'),somefunc)
    QtGui.QTreeWidget.connect(self.TreeTwo, QtCore.SIGNAL('dragMoveEvent()'), whollycrap)
    cities = QtGui.QTreeWidgetItem(self.TreeOne)
    cities.setText(0, ('cities'))
    osloItem = QTreeWidgetItem(cities)
    osloItem.setText(0, ('Oslo'))
    osloItem.setText(1, ('Yes'))

    towns = QtGui.QTreeWidgetItem(self.TreeTwo)
    towns.setText(0, ('towns'))
    vegasItem = QTreeWidgetItem(towns)
    vegasItem.setText(0, ('Las Vegas'))
    vegasItem.setText(1, ('Yes'))

    def dragEnterEvent(self, event):
    print 'In dragEnterEvent'

    def dragMoveEvent(self, event):
    print 'In dragMoveEvent'

    def dropEvent(self):
    print 'In dropEvent'


    if item: self.addHere(item)


    class communicate(QtCore.QObject):
    dropinfo = QtCore.Signal()

    def buildTree(self):

    cities = QTreeWidgetItem(mainwindow_ui.TreeOne)

    cities.setText(0, tr('cities'))

    osloItem = QTreeWidgetItem(cities)

    osloItem.setText(0, tr('Oslo'))

    osloItem.setText(1, tr('Yes'))

    app = QApplication(sys.argv)
    frame = MainWindow()



  • Moderators

    aren't you missing the "event" parameter in your drop event handler?

  • Yes, sorry about that, I put that in and I still can't get to the dropEvent() when I drop on the second QTreeWidget.
    def dropEvent(self, event):
    print 'In dropEvent'

  • Lifetime Qt Champion


    Since you're using QTreeWidget did you check this "document":http://qt-project.org/doc/qt-4.8/model-view-programming.html#using-drag-and-drop-with-item-views

  • The documentation is quite intense for a newbie. Ok, so I have to use a mime-encoded datatype to transfer/import from one QtreeWidget to another?

    Still not sure how to do that. Any further assistance?

  • Just in case, because I didn't state, I am using python and PySide.

  • Lifetime Qt Champion

    Technically you just need the "Using convenience views" part of the document.

    [quote author="litlbear" date="1376093381"]Just in case, because I didn't state, I am using python and PySide.[/quote]

    The document still applies, you only have to translate the calls from C++ to python

Log in to reply