1

I'm learning Python and Pyside. Especially Qt is Very hard to handle it. My tool accept draged and droped Files. and Sort by Natural order. Qlistview is filled by File name. It work OK till this point. But I want to move the position of Qlistview item. So Drag item and drop other position but listview don't any change or Delete a item.

Here is My code

class VideolistView (QtWidgets.QListView):
    def __init__(self, parent):
        super(VideolistView, self).__init__(parent)
        self.setObjectName("VideolistView")
        self.setGeometry(QtCore.QRect(8, 30, 250, 301))
        self.setAcceptDrops(True)
        self.setDragEnabled(True)
        self.setDragDropMode(QtWidgets.QAbstractItemView.InternalMove)
        self.Fdir=None
        self.Model = QtGui.QStandardItemModel(self)
        self.ProxyModel = NaturalProxyModel(self)
        self.ProxyModel.setSourceModel(self.Model)
        self.setModel(self.ProxyModel)

    def dragEnterEvent(self, event):
        #if event.mimeData().hasUrls():
            event.accept()

    def dragMoveEvent(self, event):
        #if event.mimeData().hasUrls():
            event.accept()

    def dropEvent(self, event):
        if event.mimeData().hasUrls():
            event.setDropAction(QtCore.Qt.LinkAction)
            event.accept()
            for url in event.mimeData().urls():
                dropitem = str(url.toLocalFile())
                Fname = os.path.split(dropitem)
                if not self.Fdir :
                    self.Fdir = Fname[0]
                elif self.Fdir == Fname[0]:
                    if not self.Model.findItems(Fname[1]):
                        listitem = QtGui.QStandardItem(Fname[1])
                        listitem.setFlags(listitem.flags() & ~QtCore.Qt.ItemIsDropEnabled)
                        self.Model.appendRow(listitem)
                else :
                    print ("Not Same Folder")
            self.ProxyModel.setSourceModel(self.Model)
            self.ProxyModel.sort(0)
            self.setModel(self.ProxyModel)
        else:
            event.setDropAction(QtCore.Qt.MoveAction)
            event.accept()
 

With event.setDropAction(QtCore.Qt.MoveAction) Releasing Item delete own I change it to event.setDropAction(QtCore.Qt.CopyAction). But Drag and Drop don't Change anything at all. How can I work properly Drag and Drop item?

ref : Qt Drag and Drop QListView removing the item on which it is released

According to ref, I add Code listitem.setFlags(listitem.flags() & ~QtCore.Qt.ItemIsDropEnabled) But It didn't work. Give me a Some Advice. Thank You for your Reading!

Dmitry
  • 6,716
  • 14
  • 37
  • 39
abrakad
  • 23
  • 5
  • What happens is that you have applied the policy of ordering that is to say every time you try to modify the QListView it will be rearranged. When you move an item is applying the policy of natural order so you do not observe the change. You could explain to us when you want the law to be applied and when not. – eyllanesc Oct 07 '17 at 01:50
  • Again Good Advice, Thank You!. the Policy of Ordering is always applied? I want to sort when File drop at that time, then off the ordering to move item freely. – abrakad Oct 07 '17 at 02:06
  • I put the following case, assume that we add items and are sorted, then move items internally and not ordered, just as you want. If you add another term to the list, the list will be rearranged to eliminate the movement of items. – eyllanesc Oct 07 '17 at 02:26
  • @eyllanese In Yesterday Question, I think that idea. I make Item list before put it to QstandarditemModel. and sort it list-sort with Natural order. then put it to QstandarditemModel. It don't turn on Model sort. So Can I move item in Qlistview? Is this idea same to your advice? I'll some try then Add to my Question. – abrakad Oct 07 '17 at 02:52

0 Answers0