Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. [SOLVED]QAbstractProxyModel and mapToSource

[SOLVED]QAbstractProxyModel and mapToSource

Scheduled Pinned Locked Moved Language Bindings
2 Posts 1 Posters 2.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    ivica
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • I Offline
      I Offline
      ivica
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved