【问题标题】:QSortFilterProxyModel and the slot setFilterFixedStringQSortFilterProxyModel 和槽 setFilterFixedString
【发布时间】:2013-11-28 08:06:15
【问题描述】:

我目前有一个搜索按钮,我想搜索我的模型的特定列。因此我只想在我的表格视图中显示匹配的行。 我已将 QSortFilterProxyModel* 对象作为源附加到表视图,并将 QStandardItemModel* 设置为其源。然后用我的搜索按钮,我建立了以下连接

QObject::connect(ui.lineEditSearch,SIGNAL(textChanged(QString)),proxyModelFilter,SLOT(setFilterFixedString(QString)));

现在我的印象是,在输入相关行时会返回。然后我意识到我没有指定过滤器代理模型要搜索哪些列。 我知道我可以实现一个继承自 QSortFilterProxyModel 的类并重新实现它的 filterAcceptsRow。我想知道是否有办法让我避免创建一个继承自 QSortFilterProxyModel 的类,而只使用 QSortFilterProxyModel 类来告诉在调用 Slot setFilterFixedString 时要搜索哪些列?

【问题讨论】:

  • filterKeyColumn 允许您搜索单个列,或全部 (-1) (qt-project.org/doc/qt-5.1/qtcore/…) 这是您需要的,还是您需要多个但不是所有列?然后你必须继承,我认为。
  • 我只需要搜索一列,如果找到该关键字,则显示匹配的行
  • 成功了,你能不能把它作为答案。所以我可以标记它

标签: c++ qt


【解决方案1】:

可以通过QSortFilterProxyModel::filterKeyColumn 设置要过滤的列。 它允许指定单个列或所有列(-1,默认值)。

或者,可以定义一个自定义过滤器角色,返回所有要搜索的字符串的串联,并通过setFilterRole() 设置它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-18
    • 2019-07-14
    • 1970-01-01
    • 1970-01-01
    • 2019-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多