Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Drag/drop item without replacing it with a fresh instance
Forum Updated to NodeBB v4.3 + New Features

Drag/drop item without replacing it with a fresh instance

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 1.4k 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.
  • WoGiW Offline
    WoGiW Offline
    WoGi
    wrote on last edited by
    #1

    Hi all,

    I have a QTreeView, filled with a bunch custom version of the QStandardItem class (gave it some extra attributes i need).

    I need to be able to reorder the items in the QTreeView by dragging them around. This already works perfectly. However, whenever I make a modification to an item, that item will be replaced by a fresh instance of a QStandardItem, therefore losing all the custom stuff I added to my custom version of the QStandardItem.

    Is there a way to be able to reorder stuff by dragging, but instead of replacing it with a new item, just keeping the original? Below is a very basic version of my code.

    Thanks!

    Wolfgang

    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)))
    
    class testItem(QtGui.QStandardItem):
        def __init__(self,name):
    
            super(testItem,self).__init__()
            self.customAttr = 'test'
            self.setText(name)
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Isn't that the same question you posted here ?

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

      WoGiW 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Isn't that the same question you posted here ?

        WoGiW Offline
        WoGiW Offline
        WoGi
        wrote on last edited by
        #3

        @SGaist Correct, sorry about that.

        As this is such an active forum, that thread got buried pretty quickly, without getting an answer. Therefore I figured modifying my opening post and title a little bit and trying it again. Hoping someone who can give me a psuh in the right direction would see it.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Doing so you are essentially fragmenting the information. If the original thread doesn't suit then you should rather delete it to start fresh but you'll also lose the original content.

          In any case, since you are using custom items, you should also handle the drop yourself.

          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
          • WoGiW Offline
            WoGiW Offline
            WoGi
            wrote on last edited by
            #5

            Could you elaborate further on what you mean by 'you should also handle the drop yourself'?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Take a look at the Drag and Drop with item views in Qt's documentation.

              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

              • Login

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