Important: Please read the Qt Code of Conduct -

Drag and Drop hidden columns workaround in treeview?

  • I have a treeview that is using a proxy to hide one of the columns. The tree view has an drag and drop mode set to InternalMove. When I drag and Drop an item the hidden row's item becomes null.

    This looks to be a known issue with the standardItemModel and the devs dont want to change it:

    They did give a workaround as to override the selectedIndexes() in the view

    class MyTreeView : public QTreeView
        QModelIndexList selectedIndexes() const
            return selectionModel()->selectedIndexes();

    However in my view it does not help the hidden column issue at all

    class HiddenTreeView(QtWidgets.QTreeView):
        def __init__(self, parent):
            super(HiddenTreeView, self).__init__(parent)
        def selectedIndexes(self):
            return self.selectionModel().selectedIndexes()

    Am I missing something here? The self.selectionModel().selectedIndexes() still only returns the visible only items.


  • Banned

    So what exactly is the purpose for you drag and drop feature -- what are you striving to achieve with it? I ask because perhaps at least there might be a way to work around this issue based on what it is you are trying to do.

    Also we might be able to help you a lot more easier and quicker if instead of supplying incomplete code snippets you supply and MRE (Minimal Reproducible Example) so that all we have to do is copy/paste and run to see the issue you are experiencing and than look at that to see what might need to be fixed or tweaked to maybe get what you want to occur to actually happen.

  • The purpose of the internal move drag and drop is to allow the user to create any hierarchy they need. I've allowed a custom context menu to create a parent on any item.

    The hidden column was used in conjunction with a qdatawidgetmapper to manage a filter property in the proxy model. For now I can get around the issue by putting that data in a custom UserRole and making a itemDelegate for the qdatawidgetmapper. I was just hoping to not have to make the delegate to reduce the complexly of the code, but it seems to work atm.

    I'd still like to know what the devs where referring too with their provided example.

  • Banned

    Ah okay then instead of trying to figure out a work-around for the hidden column just trash the hidden column and replace it by using something like the following -- note you will have to adjust it for what you are doing

    # tmpItemDict = { Col1 : [ Col2, Col3, etc... ] }
    def SetContent(self, tmpItemDict):
        for Item in tmpItemList:
            SpecInfo = GetThatDataToAssociatedWithRow()
            ItemCol1 = QStandardItem(Item)
            ItemCol2 = QStandardItem(tmpItmList[Item][0])
            ItemCol2.setData(SpecInfo, Qt.UserRole)

    This allows you to add something associated to a row without using a Hidden Column -- I found this when I was experiencing a different issue with the Hidden Column thing and this cleared up that issue. Oh also I would denote that I store all my data within a Dictionary and then I update the view by simply reloading it based on what is contained within the Dictionary rather than try to work with the model directly as that proved much more complicated than was necessary. This allowed for a much more interactive implementation using a RowIdToItemIndex and an ItemIndexToRowId XRefs Dictionaries thus not needing to get bogged down in the more complex usage of the Model concept as it is rather poorly explained and it took me a while to actually get a handle on it and I felt it would be a disservice to propagate that upon another programmer (or myself) when the code needs to be adjusted later on -- so I basically simplified what it does and use it for displaying the View and nothing more.

Log in to reply