Question

I'm sub classing a QSortFilterProxyModel in pyqt 4.8.

I'm trying to map a proxy header column to the source column and have been using this method I created.

def mapProxyColtoSourceCol(proxy, col):        
    #Get the first Valid Row on the Proxy and create and index on that column
    #Then map the index to the source and return the source column
    col = proxy.mapToSource(proxy.index(0,col)).column()
    return col 

This is straightforward and works fine. But I have run into a problem when the row filter has removed all the proxy's rows. In this case the proxy index created at (0,col) is invalid and can not map to the source and the returned source column is -1. Even though all the rows are removed I would still like to be able to map the header columns from the proxy to the source and get the source header info. Looking at the C source code it seems like the mapping uses some private functions I don't know how to access in pyQt. Is there a way to do this?

Was it helpful?

Solution

  1. If you know how the header named and if the naming is unique, then you can iterate through all columns of proxy.sourceModel() using headerData() and compares with expected name.

  2. If you have control over implementation of source model, then there you can reimplement headerData() to return column number on Qt::UserRole, then call on proxy.headerData(proxy_col, Qt.UserRole).toInt() will give you column of source model.

  3. Another trick which may work (or may be not) if sort model subclassed: you create "forbidden" non-existing index by calling createIndex(0,col).

way 3: is very low chances to work. I usually do something similar to way 2.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top