Skip to content Skip to sidebar Skip to footer

Qtreeview With Drag And Drop Support In Pyqt

In PyQt 4 I would like to create a QTreeView with possibility to reorganize its structure with drag and drop manipulation. I have implemented my own model(QAbstractItemModel) for Q

Solution 1:

You can enable drag and drop support for tree view items by setting QtGui.QAbstractItemView.InternalMove into the dragDropMode property of the treeview control. Also take a look at the documentation here Using drag & drop with item views. Below is a small example of a treeview with internal drag and drop enabled for its items.

import sys
from PyQt4 import QtGui, QtCore

class MainForm(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainForm, self).__init__(parent)

        self.model = QtGui.QStandardItemModel()

        for k in range(0, 4):
            parentItem = self.model.invisibleRootItem()
            for i in range(0, 4):
                item = QtGui.QStandardItem(QtCore.QString("item %0 %1").arg(k).arg(i))
                parentItem.appendRow(item)
                parentItem = item

        self.view = QtGui.QTreeView()
        self.view.setModel(self.model)
        self.view.setDragDropMode(QtGui.QAbstractItemView.InternalMove)

        self.setCentralWidget(self.view)

def main():
    app = QtGui.QApplication(sys.argv)
    form = MainForm()
    form.show()
    app.exec_()

if __name__ == '__main__':
    main()

Edit0: treeview + abstract model with drag and drop support

import sys
from PyQt4 import QtGui, QtCore

classTreeModel(QtCore.QAbstractItemModel):
    def__init__(self):
        QtCore.QAbstractItemModel.__init__(self)
        self.nodes = ['node0', 'node1', 'node2']

    defindex(self, row, column, parent):
        return self.createIndex(row, column, self.nodes[row])

    defparent(self, index):
        return QtCore.QModelIndex()

    defrowCount(self, index):
        if index.internalPointer() in self.nodes:
            return0returnlen(self.nodes)

    defcolumnCount(self, index):
        return1defdata(self, index, role):
        if role == 0: 
            return index.internalPointer()
        else:
            returnNonedefsupportedDropActions(self): 
        return QtCore.Qt.CopyAction | QtCore.Qt.MoveAction         

    defflags(self, index):
        ifnot index.isValid():
            return QtCore.Qt.ItemIsEnabled
        return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable | \
               QtCore.Qt.ItemIsDragEnabled | QtCore.Qt.ItemIsDropEnabled        

    defmimeTypes(self):
        return ['text/xml']

    defmimeData(self, indexes):
        mimedata = QtCore.QMimeData()
        mimedata.setData('text/xml', 'mimeData')
        return mimedata

    defdropMimeData(self, data, action, row, column, parent):
        print'dropMimeData %s %s %s %s' % (data.data('text/xml'), action, row, parent) 
        returnTrueclassMainForm(QtGui.QMainWindow):
    def__init__(self, parent=None):
        super(MainForm, self).__init__(parent)

        self.treeModel = TreeModel()

        self.view = QtGui.QTreeView()
        self.view.setModel(self.treeModel)
        self.view.setDragDropMode(QtGui.QAbstractItemView.InternalMove)

        self.setCentralWidget(self.view)

defmain():
    app = QtGui.QApplication(sys.argv)
    form = MainForm()
    form.show()
    app.exec_()

if __name__ == '__main__':
    main()

hope this helps, regards

Post a Comment for "Qtreeview With Drag And Drop Support In Pyqt"