QTableview 指定标准搜索字段 [英] QTableview specify standard search field

查看:40
本文介绍了QTableview 指定标准搜索字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有以下代码的 QTableView.它运行良好,但当我输入时,搜索字段始终是第一列,即数字和主键.

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.

如何将标准搜索字段更改为另一个字段,例如第二个(名称),即使在名称列上选择列或在名称上使用 setsort 在键入时标准搜索是第一个数字字段?

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) 

推荐答案

您需要按如下方式更改索引:

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

这篇关于QTableview 指定标准搜索字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆