Search code examples
qt4pyqt

How do I get data out of a QTableView?


I do have a QTableView widget with QtGui.QTableView.SelectRows behaviour.

  1. How can I get the current selected row?
  2. Can I get a specific column from that row back, and not only the id of the row?

Thanks.


Solution

  • QTableView inherits signals from QAbstractItemView. To get current selected row you have to connect your slots to one of

     void activated ( const QModelIndex & index )
     void clicked ( const QModelIndex & index )
     void pressed ( const QModelIndex& index )
    

    EDIT1: QModelIndex has methods row() and column() to know exactly which cell has been clicked/selected.

     self.table.clicked.connect(self.clickedSlot)
     def clickedSlot(self,index):
          print "Column is " + str(index.column())
          print "Row is " + str(index.row())
    

    If you are new to Qt/PyQt , You might want to see how to use signals and slots.

    EDIT2: If you want to get indexes from the widget itself

    self.table.selectionModel.currentIndex()