Rudi Werner
Rudi Werner

Reputation: 109

QTableview specify standard search field

I have a QTableView with the following code below. It runs fine, but when I am typing the search field is always the first column, which is the number and primary key.

How can I change that the standard search field is another field, second (name) for example, even with select column on the name column or with setsort on name the standard search when typing is the first number field?

When the code is running, I can change the search column by left-clicking in the second column, but I want to achieve this programmatically.

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""")

        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)
        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) 

Upvotes: 2

Views: 830

Answers (1)

László Papp
László Papp

Reputation: 53155

You need to change the index as follows:

# index: the index of a cell in the desired column
index = tableView.model().index(0, 1)
tableView.selectionModel().setCurrentIndex(index, QItemSelectionModel.NoUpdate);

Upvotes: 2

Related Questions