Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    [SOLVED]QAbstractProxyModel and mapToSource

    Language Bindings
    1
    2
    2137
    Loading More Posts
    • 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
      ivica last edited by

      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 Reply Quote 0
      • I
        ivica last edited by

        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 Reply Quote 0
        • First post
          Last post