PDA

View Full Version : pyQt drag and drop between two TreeWidgets?



redpaw
7th January 2009, 22:45
I'm a newbie to QT but have hacked my way thru a lot of different stuff in the past, and this is no exception, but I'm hitting a wall..

firstly I'm working with pyQT so hopefully someone can help me in that aspect as well..

I have a form I want to create with 1 or 2 QTreeWidgets and I want to be able to drag and drop between them.

currently I have a test file with one treewidget that works fine for normal clicking (and in qtDesigner where I built it, the preview of drag and drop actually works) but as soon as I try to drag, it immediately hard crashes with no signal.

I'm assuming I have to subclass the Treewidget to add some drag and drop functionality, but I haven't been able to find any simple examples of that in pyQT..

any pointers, tutorials or help would be greatly appreciated

thanks
john c

redpaw
8th January 2009, 17:40
Ok so I hacked around, and I got something working but its just that.. a hack.. can anybody give me some more tips on how to make this not use a bad hack?

thanks in advance

-john



#!/usr/local/bin/python

# dragdrop.py

import sys
from PyQt4 import QtGui,QtCore

class treeWidgetClass(QtGui.QTreeWidget):

def mimeTypes(self):
types = QtCore.QStringList()
types.append("text/plain")
return types

def fmimeData(text):
customMimeData = QtCore.QMimeData()
customText = QtCore.QString()
customMimeData.setText(customText)
return customMimeData

def mousePressEvent(self, event):

#list = QtCore.QStringList()
#list = self.mimeTypes()
#for a in list:
# print a

button = event.button()
item = self.itemAt(event.x(), event.y())

if item:
seqBrowserMimeData = self.fmimeData()
seqBrowserMimeData.setText(item.text(0))
print item.text(0)
# select the item we clicked
self.setCurrentItem(item)
if button == 1:
print 'LEFT CLICK - DRAG'
if button == 2:
print 'RIGHT CLICK'
else:
print "select something you goober"
pass

def dragEnterEvent(self, event):
if event.mimeData().hasFormat('text/plain'):
event.accept()

def dropEvent(self, event):

list = QtCore.QStringList()
list = self.mimeTypes()

##### THIS IS A COMPLETE HACK but for now 25 characters is what I need to strip off to make the string proper?
### its putting in a data structure array somehow still

text = event.mimeData().text()
text = text[25:]
#print "dropped"
print (text)
#print "here"


class DragDrop(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QDialog.__init__(self, parent)

self.resize(280, 150)
self.setWindowTitle('Simple Drag & Drop')

treeWidget = treeWidgetClass(self)
treeWidget.setDragEnabled(True)
treeWidget.setAcceptDrops(True)
treeWidget.move(30, 65)

treeWidget2 = treeWidgetClass(self)
treeWidget2.setDragEnabled(True)
treeWidget2.setAcceptDrops(True)
treeWidget2.move(300, 65)


#button = Button("Button", self)
#button.move(170, 65)
item = QtGui.QTreeWidgetItem(treeWidget)
item.setText(0,QtGui.QApplication.translate("", "jhgjhgljhgk hjgjhgjk jhgjhguyuy jhjhg jhgjhgjhg jhgjhgk ", None, QtGui.QApplication.UnicodeUTF8))
item2 = QtGui.QTreeWidgetItem(treeWidget)
item2.setText(0,QtGui.QApplication.translate("", "jhjhg jhgjhgjhg jhgjhgk ", None, QtGui.QApplication.UnicodeUTF8))


screen = QtGui.QDesktopWidget().screenGeometry()
size = self.geometry()
self.move((screen.width()-size.width())/2,(screen.height()-size.height())/2)

app = QtGui.QApplication(sys.argv)
icon = DragDrop()
icon.show()
app.exec_()