0
votes

I have a QTreeView which populates from a QAbstractItemModel via a QSortFilterProxyModel.

I've implemented a DataStructure class that sets Parent / Child relationships for the data and a TreeView(QAbstractItemModel) class - this works well and populates the QTreeView as I want.

The treeView is set like:

from PyQt4 import QtCore, QtGui
import data_Model

#DataStructure(data, parent=None)
rootNode = data_Model.DataStructure('_Holder')

for i in mainData:
    mainRow = data_Model.DataStructure(i, rootNode)
    for j in subData:
        data_Model.DataStructure(j, mainRow)

self._proxyModel = QtGui.QSortFilterProxyModel()
self._model = data_Model.TreeModel(rootNode)
self._proxyModel.setSourceModel(self._model)

self.treeView.setModel(self._proxyModel)

I want to change this so that subData is populated in a QTableWidget that is a child of the relevant mainRow for a specific column index

My attempt is to remove:

    for j in subData:
        data_Model.DataStructure(j, mainRow)

from the above, and call the below each time the main model is set:

def addChild(self, subData):

    rowCount = self._proxyModel.rowCount()
    widgetDict = {}

    for i in range(rowCount):

        for j in range(len(subData)):

            #set model index as current row and column 5 onwards
            modelIndex = self._proxyModel.mapToSource(self._proxyModel.index(i, 5 + j))
            widgetDict[str(i) + str(j)] = QtGui.QListWidget().addItems(subData[j])

            self.treeView.setIndexWidget(modelIndex, widgetDict[str(i) + str(j)])

The setIndexWidget call seems to have no effect. I'm sure this is a completely backward way to go about this but my attempts to include the QListWidget within the data_Model have failed miserably. Any direction on how to go about this (or if possible!) or specific advice greatly appreciated.

2

2 Answers

1
votes

Not sure about the rest of the code, but the line:

    widgetDict[str(i) + str(j)] = QtGui.QListWidget().addItems(subData[j])

will create and populate a list-widget, then throw it away and add None to widgetDict.

You probably want something more like:

    widget = QtGui.QListWidget()
    widget.addItems(subData[j])
    widgetDict[str(i) + str(j)] = widget
    self.treeView.setIndexWidget(modelIndex, widget)
0
votes

As pointed out by @ekhumoro modelIndex.model() and treeView.model() need to be the same. I was trying to map the _proxyModel back to the _model and that was the issue

Changing this:

modelIndex = self._proxyModel.mapToSource(self._proxyModel.index(i, 5 + j))

to this:

modelIndex = self._proxyModel.index(i, 5 + j))

solved the problem