title: How to select QTableView index or row from inside of Model
body: <p>The posted code creates a singe <code>Model/Proxy</code> <code>QTableView</code>. The multi-selection feature has been enabled for it.</p>
  
  <p><img src="https://i.stack.imgur.com/HwIyI.png" alt="enter image description here"></p>
  
  <p>There are four items total. Two of them include a character "A". Other two include character "B" in their "item" names.</p>
  
  <p><code>QPushButton</code> when pressed calls for the <code>clicked()</code> method.
  When called this method first queries a <code>Proxy Model</code> connected to the <code>QTableView</code>:</p>
  
  <pre><code>proxyModel=self.tableview.model()
  </code></pre>
  
  <p>Then the method asks a <code>proxyModel</code> to return a total number of rows:</p>
  
  <pre><code>rows=proxyModel.rowCount()
  </code></pre>
  
  <p>Knowing how many rows in a <code>QTabelView</code>'s model it iterates each row. First it is querying a row index:</p>
  
  <pre><code>index=proxyModel.index(row, 0)
  </code></pre>
  
  <p>Knowing <code>index</code> it proceeds with asking for a value stored in <code>self.items</code> variable by calling <code>data()</code> method supplying it with a queried in a previous step a <code>QModelIndex</code> (a variable <code>index</code> here) and a <code>Role</code> flag. </p>
  
  <pre><code>item=proxyModel.data(index, Qt.DisplayRole).toPyObject()
  </code></pre>
  
  <p>'toPyObject()' is used to convert the data received from <code>.data()</code> method to a "regular" Python variable.</p>
  
  <p>Lastly it checks if the characters "<em>B</em>" in a received string. If so it selects QTableView row using:</p>
  
  <pre><code>self.tableview.selectRow(row)
  </code></pre>
  
  <p>Now what I want is to get the same selection functionality from inside of the scope of Proxy Model's <code>filterAcceptsRow()</code> if that is possible.</p>
  
  <p>If it is not possible I would like to know if there is any other way of doing it... should be I using <code>QItemSelectionModel</code>? Then how? </p>
  
  <pre><code>from PyQt4.QtCore import *
  from PyQt4.QtGui import *
  import sys
  
  class Model(QAbstractTableModel):
      def __init__(self, parent=None, *args):
          QAbstractTableModel.__init__(self, parent, *args)
          self.items = ['Item_A_001','Item_A_002','Item_B_001','Item_B_002']
  
      def rowCount(self, parent=QModelIndex()):
          return len(self.items)       
      def columnCount(self, parent=QModelIndex()):
          return 1
  
      def data(self, index, role):
          if not index.isValid(): return QVariant()
          elif role != Qt.DisplayRole:
              return QVariant()
  
          row=index.row()
          if row&lt;len(self.items):
              return QVariant(self.items[row])
          else:
              return QVariant()
  
  class Proxy(QSortFilterProxyModel):
      def __init__(self):
          super(Proxy, self).__init__()
  
      def filterAcceptsRow(self, row, parent):
          return True
  
  class MyWindow(QWidget):
      def __init__(self, *args):
          QWidget.__init__(self, *args)
  
          tableModel=Model(self)               
  
          proxyModel=Proxy()
          proxyModel.setSourceModel(tableModel)
  
          self.tableview=QTableView(self) 
          self.tableview.setModel(proxyModel)
          self.tableview.horizontalHeader().setStretchLastSection(True)
          self.tableview.setSelectionMode(QAbstractItemView.MultiSelection)
  
          button=QPushButton(self)
          button.setText('Select Items with B')
          button.clicked.connect(self.clicked)
  
          layout = QVBoxLayout(self)
          layout.addWidget(self.tableview)
          layout.addWidget(button)
          self.setLayout(layout)
  
      def clicked(self, arg):
          proxyModel=self.tableview.model()
  
          self.tableview.clearSelection()
          rows=proxyModel.rowCount()
          for row in range(rows):
              index=proxyModel.index(row, 0)
              item=proxyModel.data(index, Qt.DisplayRole).toPyObject()
              if '_B_' in item:
                  self.tableview.selectRow(row)
  
  if __name__ == "__main__":
      app = QApplication(sys.argv)
      w = MyWindow()
      w.show()
      sys.exit(app.exec_())
  </code></pre>