Найти - Пользователи
Полная версия: QTreeWidget Drag&Drop
Начало » GUI » QTreeWidget Drag&Drop
1
Ashedu
Доброго дня.

Хочу сделать перетаскивание элементов внутри виджета мышью, а при нажатой кнопке Ctrl - копирование.
не получается, вот что есть:

class CategoriesTreeWidget(QtGui.QTreeWidget):
    def __init__(self, parent, func):
        QtGui.QTreeWidget.__init__(self, parent)
        self.func_ = func
        self.setDragDropMode(QtGui.QAbstractItemView.InternalMove)     
        self.setDropIndicatorShown(True)
            
    def dropEvent(self, event):
        if event.keyboardModifiers () == QtCore.Qt.ControlModifier:
            event.setDropAction(QtCore.Qt.CopyAction)
        else:
            event.setDropAction(QtCore.Qt.MoveAction)            
        event.acceptProposedAction ()

Сначала пытался сделать через
def dragMoveEvent(self, event):         
        if event.keyboardModifiers () == QtCore.Qt.ShiftModifier:
            self.setDragDropMode(QtGui.QAbstractItemView.DragDrop)
        else:
            self.setDragDropMode(QtGui.QAbstractItemView.InternalMove)
               QtGui.QTreeWidget.dragMoveEvent(self, event)
 
Тоже не вышло.
Что я делаю не так? В чем соль и суть?
Ashedu
def dropEvent(self, event):
        if event.keyboardModifiers () == QtCore.Qt.ControlModifier:
            event.setDropAction(QtCore.Qt.CopyAction)
            self.setDragDropMode(QtGui.QAbstractItemView.DragDrop)
        else:
            event.setDropAction(QtCore.Qt.MoveAction)
            self.setDragDropMode(QtGui.QAbstractItemView.InternalMove)     
            
        QtGui.QTreeWidget.dropEvent(self, event)
        
        item = self.itemAt(event.pos())
        self.emit(QtCore.SIGNAL("dropAccepted"), item)

C перемещением разобрался.
Теперь другой вопрос, как получить инфо о том какой item был перетащен и куда он был перетащен или скопирован?
Gennady
Используйте QAbstractItemView.indexAt(event.pos()) — в начале, и в конце. По индексу получите item.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB