Question

I've setup my QTableView as view, model for that view is QSqlQueryModel.

enter image description here

Excuse me for Serbian letters, you will understand what I am saying nevertheless.

self.model = QSqlQueryModel(self)
db = QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName("database.db")
db.open()

view = self.ui.myView
view.setVisible(True)
view.setSortingEnabled(True)
view.sortByColumn(2,Qt.AscendingOrder)
view.setModel(self.model)

view.clicked.connect(self.clickedSlot) #this function clickedSlot returns row and column numbers, which I use for extracting data from SQLite database.

def searchName(self): #this function searches the model for some name,lets say JOHN DOE`

    name = (str(self.ui.inputName.text()).upper())

    proxy = QSortFilterProxyModel()
    proxy.setSourceModel(self.model)
    proksi.setDynamicSortFilter(True)

    proxy.setFilterRegExp(QRegExp(name, Qt.CaseInsensitive))
    proxy.setFilterKeyColumn(1)

    view = self.ui.myView
    view.setVisible(False)
    view.resizeColumnsToContents()
    view.setVisible(True)
    view.setSortingEnabled(True)
    #view.sortByColumn(2,Qt.AscendingOrder)
    view.setModel(proxy)
    view.clicked.connect(self.clickedSlot)

Screenshot 2, after the searchName function. Again, excuse me for Serbian letters. Now the problem;

If I clicked on a row in first screenshot, I would get row number and column number printed in console, like 8,2. If I click on a row in SECOND screenshot (one with the proxy filter ON) I get row -1, column -1.

How would I implement QAbstractProxyModel and mapToSource method to get REAL row and column numbers?

:edit:

def clickedSlot(self,index):
 rownumber = index.row()
 colnumber = index.column()
 self.model.setQuery("select name from cases")
 tempname = self.model.data(self.model.index(rownumber, 1))
 print("row " +str(rownumber))
 print("column " +str(colnumber))
 print("name " +str(tempname))

output I get with this:

row 9 column 1 name JOHN DOE

Was it helpful?

Solution

Let me preface by reassuring you... The Serbian letters were no trouble at all :-)

I would need to see your code for the SLOT self.clickedSlot to be sure, but my guess is that you are resolving the row column wrong at that point.

Assuming self.ui.myView is the QTableView and you are using the clicked() signal, what you should be receiving in your clickedSlot is the QModelIndex for the proxy model. At that point you should be able to do the following:

def clickedSlot(self, modelIndex):
    model = modelIndex.model()
    if hasattr(model, 'mapToSource'):
        # We are a proxy model
        modelIndex = model.mapToSource(modelIndex)
    print modelIndex.row(), modelIndex.column() 

This is just one way to check for the proxy. Maybe you have a flag set and you already know its a proxy. But here I just check if the model reference on the index has a 'mapToSource' method, and if so then we know its a proxy. So we just call the mapToSource with that ProxyIndex to get the real index.

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