Rudi Werner
Rudi Werner

Reputation: 109

PyQt 5 QTableView selection bar issue

I have a QTableView with a button on it to update the selected row the update button calls a Form to update the record (Qdialog in other class)

After udpating the record i gove focus back to the Qtableview but i cant find it to give the focus bakc ot the selectionbar ?

The Table comes back but i always need to click in the table to bring the selectionbar back ?

Anybody knows how to do that?

I want also that the selction bar is placed on the record that just was updated

I tryied it already with setfocus / activatewindow ... But nothing gives me the desired result:

Here is some example code:

class KL_browse(QDialog, ui_kl_browse.Ui_kl_browse):       
    def __init__(self):
        super(KL_browse, self).__init__()
       # query = QSqlQuery()

       # query.exec_("""SELECT * FROM klanten ORDER BY klantnaam""")

        self.setupUi(self)

        self.model = QSqlTableModel(self)
        self.model.setTable("KLANTEN")
      #  self.model.setSort(1, Qt.AscendingOrder)
        self.model.select()
        self.tableView.setModel(self.model)
        self.tableView.setSelectionMode(QTableView.SingleSelection)
        self.tableView.setSelectionBehavior(QTableView.SelectRows)
     #   self.view.setColumnHidden(ID, True)
     #   index = self.tableView.model().index(0, 1)
     #   self.tableView.selectionModel().setCurrentIndex(index, QItemSelectionModel.NoUpdate)      

        self.tableView.horizontalHeader().setSectionsMovable(True)
        self.tableView.horizontalHeader().setDragEnabled(True)
        self.tableView.horizontalHeader().setDragDropMode(QAbstractItemView.InternalMove)
        self.tableView.horizontalHeader().setSortIndicator(1,0)
        self.tableView.selectRow(0)
        self.tableView.selectColumn(1) 

        index = self.tableView.model().index(0, 1)
        self.tableView.selectionModel().setCurrentIndex(index, QItemSelectionModel.NoUpdate)      

   # Volgende code is om de actie op knoppen en dergelijke op te vangen
        self.add_button.clicked.connect(self.test_win_start)
        self.Del_button.clicked.connect(self.del_record)

    def del_record(self):
         index = self.tableView.currentIndex()
         self.model.removeRow(index.row())
         self.model.submitAll()
         self.model.select()
         self.tableView.activateWindow()
         self.tableView.setFocus()

    def test_win_start(self):
         self.test_win = Test_win()
         self.test_win.exec_() 
         self.model.select() 
         self.tableView.activateWindow()

del_record deletes a record but also there the focus is not brining back to the selectionbar! test_win_start calls the update form

Upvotes: 0

Views: 1553

Answers (1)

László Papp
László Papp

Reputation: 53173

I think the issue is that you were just pasting these two lines from my other answer:

index = self.tableView.model().index(0, 1)
self.tableView.selectionModel().setCurrentIndex(index, QItemSelectionModel.NoUpdate)

This was meant to change the search field, but it is not meant to give the selection back to you.

For that, you need a very similar, but different mechanism, namely: change the current index on the view, rather than just on the background data model. Here goes the code that ought to work:

index = self.tableView.model().index(0, 1)
self.tableView.setCurrentIndex(index)

Upvotes: 1

Related Questions