Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QListView IconMode DragDropMode InternalMove not working for me
Forum Updated to NodeBB v4.3 + New Features

QListView IconMode DragDropMode InternalMove not working for me

Scheduled Pinned Locked Moved Solved Qt for Python
qt for pythonpython
3 Posts 2 Posters 442 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lucifer9683
    wrote on last edited by lucifer9683
    #1

    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.
    brush.jpg

    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.

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lucifer9683
      wrote on last edited by
      #3

      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
      
      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        From the top of my head, you might need to also implement the dragMoveEvent.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lucifer9683
          wrote on last edited by
          #3

          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
          
          1 Reply Last reply
          0
          • L lucifer9683 has marked this topic as solved on

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved