Results 1 to 2 of 2

Thread: pyQt drag and drop between two TreeWidgets?

  1. #1
    Join Date
    Jan 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default pyQt drag and drop between two TreeWidgets?

    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

  2. #2
    Join Date
    Jan 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: pyQt drag and drop between two TreeWidgets?

    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

    Qt Code:
    1. #!/usr/local/bin/python
    2.  
    3. # dragdrop.py
    4.  
    5. import sys
    6. from PyQt4 import QtGui,QtCore
    7.  
    8. class treeWidgetClass(QtGui.QTreeWidget):
    9.  
    10. def mimeTypes(self):
    11. types = QtCore.QStringList()
    12. types.append("text/plain")
    13. return types
    14.  
    15. def fmimeData(text):
    16. customMimeData = QtCore.QMimeData()
    17. customText = QtCore.QString()
    18. customMimeData.setText(customText)
    19. return customMimeData
    20.  
    21. def mousePressEvent(self, event):
    22.  
    23. #list = QtCore.QStringList()
    24. #list = self.mimeTypes()
    25. #for a in list:
    26. # print a
    27.  
    28. button = event.button()
    29. item = self.itemAt(event.x(), event.y())
    30.  
    31. if item:
    32. seqBrowserMimeData = self.fmimeData()
    33. seqBrowserMimeData.setText(item.text(0))
    34. print item.text(0)
    35. # select the item we clicked
    36. self.setCurrentItem(item)
    37. if button == 1:
    38. print 'LEFT CLICK - DRAG'
    39. if button == 2:
    40. print 'RIGHT CLICK'
    41. else:
    42. print "select something you goober"
    43. pass
    44.  
    45. def dragEnterEvent(self, event):
    46. if event.mimeData().hasFormat('text/plain'):
    47. event.accept()
    48.  
    49. def dropEvent(self, event):
    50.  
    51. list = QtCore.QStringList()
    52. list = self.mimeTypes()
    53.  
    54. ##### THIS IS A COMPLETE HACK but for now 25 characters is what I need to strip off to make the string proper?
    55. ### its putting in a data structure array somehow still
    56.  
    57. text = event.mimeData().text()
    58. text = text[25:]
    59. #print "dropped"
    60. print (text)
    61. #print "here"
    62.  
    63.  
    64. class DragDrop(QtGui.QDialog):
    65. def __init__(self, parent=None):
    66. QtGui.QDialog.__init__(self, parent)
    67.  
    68. self.resize(280, 150)
    69. self.setWindowTitle('Simple Drag & Drop')
    70.  
    71. treeWidget = treeWidgetClass(self)
    72. treeWidget.setDragEnabled(True)
    73. treeWidget.setAcceptDrops(True)
    74. treeWidget.move(30, 65)
    75.  
    76. treeWidget2 = treeWidgetClass(self)
    77. treeWidget2.setDragEnabled(True)
    78. treeWidget2.setAcceptDrops(True)
    79. treeWidget2.move(300, 65)
    80.  
    81.  
    82. #button = Button("Button", self)
    83. #button.move(170, 65)
    84. item = QtGui.QTreeWidgetItem(treeWidget)
    85. item.setText(0,QtGui.QApplication.translate("", "jhgjhgljhgk hjgjhgjk jhgjhguyuy jhjhg jhgjhgjhg jhgjhgk ", None, QtGui.QApplication.UnicodeUTF8))
    86. item2 = QtGui.QTreeWidgetItem(treeWidget)
    87. item2.setText(0,QtGui.QApplication.translate("", "jhjhg jhgjhgjhg jhgjhgk ", None, QtGui.QApplication.UnicodeUTF8))
    88.  
    89.  
    90. screen = QtGui.QDesktopWidget().screenGeometry()
    91. size = self.geometry()
    92. self.move((screen.width()-size.width())/2,(screen.height()-size.height())/2)
    93.  
    94. app = QtGui.QApplication(sys.argv)
    95. icon = DragDrop()
    96. icon.show()
    97. app.exec_()
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. drag and drop QToolButton in QToolBar
    By NBilal in forum Qt Programming
    Replies: 1
    Last Post: 28th December 2008, 21:11
  2. Change cursor & status during Drag & Drop
    By ronlongo in forum Qt Programming
    Replies: 0
    Last Post: 1st December 2008, 17:56
  3. Drag & Drop when parent and childs accept drops?
    By gri in forum Qt Programming
    Replies: 0
    Last Post: 17th October 2008, 10:00
  4. Replies: 7
    Last Post: 8th September 2006, 17:19
  5. Drag and drop revisited
    By Big Duck in forum Newbie
    Replies: 2
    Last Post: 30th June 2006, 17:41

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.