Differenciate between item being dropped 'onto' and 'above/underneath' another item



  • Hi I'm trying to do a custom implementation of the drag and drop methods of the QTreeView.

    It's all working pretty good so far, except for the fact that I'm not sure how to differentiate between an item being dropped onto another item, and an item being dropped underneath/above an other item.

    For sake of demonstration, a super stripped down verison of my current code:

    
    
    from PySide import QtGui, QtCore
    
    class QTreeViewCustom(QtGui.QTreeView):
        def __init__(self):
    
            super(QTreeViewCustom,self).__init__()
    
            self.setDragDropMode(QtGui.QAbstractItemView.InternalMove)
            self.setDragEnabled(True)
            self.setDropIndicatorShown(True)
    
            self.dataModel = QtGui.QStandardItemModel()
            
            self.root = self.dataModel.invisibleRootItem()
            self.root.setDropEnabled(True)
    
            self.setModel(self.dataModel)
            self.setSelectionMode(QtGui.QAbstractItemView.SelectionMode.ContiguousSelection)
    
            #fill the tree with items
            for i in range(10):
                self.root.appendRow(testItem(str(i)))
    
        def dragEnterEvent(self,event):
            index =  self.indexAt(event.pos())
            item = self.dataModel.itemFromIndex(index)
            self.draggedFrom = item.path
            event.acceptProposedAction()
    
        def dropEvent(self,event):
            index =  self.indexAt(event.pos())
            item = self.dataModel.itemFromIndex(index)
            self.draggedTo = item.path
    
            print 'source: ', self.draggedFrom
    
            '''
            would like be to able to do something like this here:
            if DROPPED_ON_TOP:
                print 'target (dropped on top): ', self.draggedTo
            else:
                print 'target: ', self.draggedTo
            ''''
    class testItem(QtGui.QStandardItem):
        def __init__(self,name):
    
            super(testItem,self).__init__()
            self.path = 'test_%s'%name
            self.setText(name)
    

    Thanks!


  • Qt Champions 2016

    Hi
    Maybe you can calcualte it with
    http://doc.qt.io/qt-5/qabstractitemview.html#visualRect
    http://doc.qt.io/qt-5/qabstractitemview.html#indexAt

    You get the drop point from the event
    Then take the rect of the item dropped on
    and see if u think it was top or bottom of it.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.