[SOLVED]QAbstractProxyModel and mapToSource



  • I've setup my QTableView as view, model for that view is QSqlQueryModel. "Screenshot":http://imgur.com/4okrm - 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.setModel(proxy)
    view.clicked.connect(self.clickedSlot)@
    

    "Screenshot 2":http://imgur.com/64dO6, 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:

    I forgot to include clickedSlot() function, which caused the problem after all. This is the messy one:

    @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))@

    It worked well without QSortFilterProxyModel, but with one it was unable to resolve the original row and column. Function that works is in a post underneath.



  • This function correctly fetches row and column numbers from the model, through a proxy:

    @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()@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.