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 PySidefrom 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 platformclass MainWindow(QMainWindow, Ui_MainWindow, QDialog):
def init(self, parent=None):
super(MainWindow, self).init(parent)self.setupUi(self)
self.com = communicate()self.com.dropinfo.connect(self.TreeTwo.dropEvent(QtCore.Qt.MoveAction))
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'
event.accept()def dragMoveEvent(self, event):
print 'In dragMoveEvent'
event.setDropAction(QtCore.Qt.MoveAction)
event.accept()def dropEvent(self):
print 'In dropEvent'item=self.itemAt(event.pos())
if item: self.addHere(item)
event.accept()
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()
frame.show()
app.exec_()@
David
-
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'
@ -
Hi,
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.
-
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