【问题标题】:QSortFilterProxyModel not filtering correctlyQSortFilterProxyModel 未正确过滤
【发布时间】:2017-08-17 04:32:59
【问题描述】:

我正在尝试使用 QSortFilterProxyModel 根据 QLineEdit 中的文本过滤 QTableView。它正在工作,虽然不正确 - 它没有显示“完全匹配”的项目。一个例子可以更好地说明我的意思。

示例: 在 QLineEdit 中键入“22”将显示第一列中包含“22”的行,但不会显示列等于 22 的行。将显示“229”,将显示“2224”,但不显示“22”

下面是我的代码的精简版

self.model = QSqlTableModel()
self.model.setTable("products")
self.model.select()

self.proxy = QSortFilterProxyModel()
self.proxy.setSourceModel(self.model)
self.proxy.setFilterKeyColumn(0)

self.ui.table_products.setModel(self.proxy)
self.ui.line_input.textChanged.connect(self._filter_products)

def _filter_products(self, text):
    search = QRegExp(
        text,
        Qt.CaseInsensitive,
        QRegExp.RegExp
    )

    self.proxy.setFilterRegExp(search)

【问题讨论】:

    标签: python pyqt pyqt5


    【解决方案1】:

    在我看来您正在过滤第 0 列...

    self.proxy.setFilterKeyColumn(0)
    

    ...并且该列中只有 1 行 22。

    【讨论】:

      猜你喜欢
      • 2015-06-16
      • 1970-01-01
      • 2015-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多