Why does my QStandardItemModel itemFromIndex method return None? (index invalid)
<p>I am trying to make a listview with checkboxes that checks the selected boxes when the <strong>enter/return key</strong> is pressed. I do this with an override of the eventfilter for my MainWindow (yes I ought to subclass it, but I couldn't get that working)</p>

<p>In the eventfilter i get a <strong>None</strong> value returned from the <strong>itemFromIndex</strong> method even though I just passed the index through a <strong>.isValid()</strong> without problems. Obviously i am missing something, but i can't figure it out - is it looking at completely different indices? is the model not updated? </p>

<p>Any advice on alternate approaches are welcome</p>

<p>This is the method I use to fill the model (<strong>QStandardItemModel</strong>) with items, it's only called when i load a file.</p>

<pre><code>    def update_siNLV(self,names,model):

        model.clear()

        for name in names:            
            item = Qg.QStandardItem(name)
            item.setCheckState(Qc.Qt.Unchecked)
            item.setCheckable(True)
            model.appendRow(item)
</code></pre>

<p>This is from the <strong>init</strong> method where I create a variable for the selectionmodel and install the eventfilter on my <strong>QListView</strong></p>

<pre><code>    self.sigInSelection = self.siNLV.selectionModel()
    self.siNLV.installEventFilter(self)
</code></pre>

<p>The <strong>eventFilter</strong> method looks like this and the filtering part of the method works (I've made it print the selected indices with press on the enter key)</p>

<pre><code>    def eventFilter(self,receiver,event):
        if event.type() == QtCore.QEvent.KeyPress:
            if event.key() == QtCore.Qt.Key_Return or event.key() == Qc.Qt.Key_Enter:

                indexes = self.sigInSelection.selectedIndexes()

                for index in indexes:
                    if index.isValid():                        
                        print(str(index.row())+" "+str(index.column()))                                   
                        item = self.sigInModel.itemFromIndex(index)
                        item.setCheckState(qtCore.Qt.Checked)                     
                return True

    return super(form,self).eventFilter(receiver,event)
</code></pre>

<p>As discussed in the comments:</p>

<p>The indices returned by <code>QItemSelectionModel.selectedIndexes()</code> come from the view and relate to the connection between the view and its immediate model.  The identity of that model can be found by calling <code>QModelIndex.model()</code> and in this case it is not the model that you want: it is instead a proxy model that is in-between your desired <code>QStandardItemModel</code> and the view.</p>

<p>To get to the model you want you need to use <code>QAbstractProxyModel.mapToSource()</code>.  So you might use code something like this:</p>

<pre><code>source_index = self.proxy.mapToSource(index)
item = self.sigInModel.itemFromIndex(source_index)
</code></pre>

<p>More generally you could traverse an arbitrary proxy structure and avoid this hard-coded usage of a single known proxy by code something like:</p>

<pre><code>proxy_model = index.model()
while proxy_model != self.sigInModel:
    index = proxy_model.mapToSource(index)
    proxy_model = index.model()
item = self.sigInModel.itemFromIndex(index)
</code></pre>

<p>But this is probably overkill in this case where you know there is a simple single proxy.</p>

python qt pyqt qlistview qstandarditemmodel