⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

PyQt5 How to get data from current row of QTableView

  • if i write :

     name = self.tableView.model().data(index)

    it back me a string of the current cell so i want the specific cell which is column 1

        def contextMenu(self,pos):
            index = self.tableView.currentIndex()
            row = index.row
            name = self.tableView.model().data(self.tableView.model().index(row, 1)) #when it comes to this line program execution stop without sending error

  • Banned

    To help you trouble shoot this I would suggest you do the following so that you are sure of what your are getting:

    NewIndex = self.tableView.model().index(row, 1)
    print('Index is :',NewIndex)
    Name = self.tableView.model().data(NewIndex)

    Then you can see what your NewIndex is and/or see if it is working correctly as I would be pretty confident that giving the latter function a valid Index gets the value I would be expecting in the Name variable since it does this fine already. Thus this means perhaps what that internal piece is returning is not what you are expecting.

  • @denni-0

            index = self.tableView.currentIndex()
            NewIndex = self.tableView.model().index(index.row(), 0)

    I must write index.row() with parenthesis.
    Now it works.Thanks

  • for tableWidget

    index = self.tableWidget_2.currentIndex()
    NewIndex = self.tableWidget_2.model().index(index.row(), 0)
    print('Index is :',NewIndex)
    Name = self.tableWidget_2.model().data(NewIndex)

  • @phoenix_mashhad
    To save from keep repeating self.tableWidget_2.model(), one can shortcut getting at a column in the same row via

    NewIndex = self.tableWidget_2.currentIndex().siblingAtColumn(0)

    One can shortcut getting the data for an index via

    Name = NexIndex.data()

  • THANK YOU SO MUCH @denni-0. I dont could FIND THIS anywere!!!, I lost 3 days serching

Log in to reply