QListView IconMode DragDropMode InternalMove not working for me
-
Hi, I'm using PyQt5 to develop the GUI for a brush switching plugin in Krita.
My current setup is to load the brushes as QStandardItem in several one column QStandardItemModel which are connect to each respective QListView.I am able to drag and drop the brushes from one QListView to another and it updates the model without problem. But when I'm using drag and drop for internal move/reordering, it just doesn't seem to work.
Drop action is blocked when dragging and dropping an item above the first. I can drop the first item below the second if I have an empty space in the view, but the model never updates the order when moved that way.
Here is what the views look like right now.
Here is my current implementation of the classes.
class SlotView(QListView): def __init__(self, parent=None): super().__init__(parent) self.setFlow(QListView.Flow.TopToBottom) self.setMovement(QListView.Movement.Snap) self.setDefaultDropAction(Qt.DropAction.MoveAction) self.setWrapping(False) self.setViewMode(QListView.ViewMode.IconMode) self.setSelectionMode(QListView.SelectionMode.ExtendedSelection) self.setVerticalScrollMode(QListView.ScrollMode.ScrollPerPixel) self.horizontalScrollBar().setEnabled(False) self.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff) self.setIconSize(ICON_SIZE) self.setGridSize(ICON_SIZE) self.setFixedSize(SLOT_SIZE) def dragEnterEvent(self, event): if event.source() is self: self.setDragDropMode(QListView.DragDropMode.InternalMove) else: self.setDragDropMode(QListView.DragDropMode.DragDrop) super().dragEnterEvent(event)
In the class that inherits QDialog is the function to load the slot views.
def loadSlots(self): allPresets = Application.resources('preset') kit = settings.kits[settings.activeKit] slotLayout = QHBoxLayout() for slot in kit: view = SlotView() model = QStandardItemModel() for group in slot: for name in group: preset = QStandardItem(QIcon(QPixmap.fromImage(allPresets[name].image())), None) preset.setToolTip(name) preset.setEditable(False) preset.setDropEnabled(False) model.appendRow(preset) if len(slot) - slot.index(group) > 1: divider = QStandardItem(Application.icon('curve-preset-linear'), None) divider.setToolTip('Group Divider') divider.setEditable(False) divider.setDropEnabled(False) model.appendRow(divider) view.setModel(model) self.models.append(model) slotLayout.addWidget(view) self.views.append(view) self.mainLayout.addLayout(slotLayout)
Thanks for looking at my problem.
-
I tried using list mode instead and it worked! I have no idea why icon mode doesn't. I don't even need to set DragDropMode to InternalMove in list mode for reordering, as I have set my DefaultDropAction to MoveAction.
What I've done instead is to change viewOptions to icon mode settings.
class SlotView(QListView): def __init__(self, parent=None): super().__init__(parent) self.setMovement(QListView.Movement.Snap) self.setDefaultDropAction(Qt.DropAction.MoveAction) self.setSelectionMode(QListView.SelectionMode.ExtendedSelection) self.setVerticalScrollMode(QListView.ScrollMode.ScrollPerPixel) self.horizontalScrollBar().setEnabled(False) self.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff) self.setIconSize(ICON_SIZE) self.setGridSize(ICON_SIZE) self.setFixedSize(SLOT_SIZE) def viewOptions(self): option = super().viewOptions() option.showDecorationSelected = True option.decorationPosition = QStyleOptionViewItem.Position.Top option.displayAlignment = Qt.AlignmentFlag.AlignCenter return option
-
Hi,
From the top of my head, you might need to also implement the dragMoveEvent.
-
I tried using list mode instead and it worked! I have no idea why icon mode doesn't. I don't even need to set DragDropMode to InternalMove in list mode for reordering, as I have set my DefaultDropAction to MoveAction.
What I've done instead is to change viewOptions to icon mode settings.
class SlotView(QListView): def __init__(self, parent=None): super().__init__(parent) self.setMovement(QListView.Movement.Snap) self.setDefaultDropAction(Qt.DropAction.MoveAction) self.setSelectionMode(QListView.SelectionMode.ExtendedSelection) self.setVerticalScrollMode(QListView.ScrollMode.ScrollPerPixel) self.horizontalScrollBar().setEnabled(False) self.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff) self.setIconSize(ICON_SIZE) self.setGridSize(ICON_SIZE) self.setFixedSize(SLOT_SIZE) def viewOptions(self): option = super().viewOptions() option.showDecorationSelected = True option.decorationPosition = QStyleOptionViewItem.Position.Top option.displayAlignment = Qt.AlignmentFlag.AlignCenter return option
-