Tianchi  v0.0.2 build 20130701
C++ library for Qt with VC & mingW
Public 槽 | Public 成员函数 | Protected 成员函数 | 所有成员列表
TcFunctionalSortFilterProxyModel类 参考

依所设过滤参数(QVariantMap)来使用已设滤函数进行过滤的 SortFilterProxyModel,使用时分三部分: 更多...

#include <tcfunctionalsortfilterproxymodel.h>

类 TcFunctionalSortFilterProxyModel 继承关系图:

Public 槽

void setFilterParam (const QVariantMap &param)
 设定过滤参数
 

Public 成员函数

 TcFunctionalSortFilterProxyModel (QObject *parent=0)
 
void setFilterFunction (TcFunctionalSortFilterProxyModelFilterFunction func)
 设定过滤函数
 

Protected 成员函数

virtual bool filterAcceptsRow (int source_row, const QModelIndex &source_parent) const
 

详细描述

依所设过滤参数(QVariantMap)来使用已设滤函数进行过滤的 SortFilterProxyModel,使用时分三部分:

        1. 定义过滤函数
        2. 创建TcFunctionalSortFilterProxyModel实例,并设定过滤函数
        3. 条件改变时,调用setFilterParam()应用过滤参数并使其生效

1.先定义过滤函数(示例为MyFilter)

bool MyFilter(const QVariantMap &param, const QModelIndex &index)
{
QModelIndex idx1 = index; // 第一列
QModelIndex idx2 = idx1.sibling(idx1.row(), 1); // 第二列
QString str1 = param["col1"].toString(); // 第一列过滤内容
QString str2 = param["col2"].toString(); // 第二列过滤内容
// 如果第一个过滤参数不为空,那么要求第一列显示字符串必须以str1开头
if (!str1.isEmpty())
{
if (idx1.data(Qt::DisplayRole).toString().indexOf(str1) != 0)
{
return false;
}
}
// 如果第二个过滤参数不为空,那么要求第二列显示字符串必须包含str2
if (!str2.isEmpty())
{
if (idx2.data(Qt::DisplayRole).toString().indexOf(str1) == -1)
{
return false;
}
}
return true;
}

2.创建模型时,设置过滤函数

model = new QStandardItemModel(this);
......
proxy->setFilterFunction(&MyFilter);
proxy->setSourceModel(model);

3.设置完过滤条件后,调用setFilterParam()来应用过滤参数

QVariantMap param;
param["col1"] = "0102"; // 假设要求过滤以"0102"开头的字符串
param["col2"] = "HFX"; // 假设要求过滤包含"HFX"的字符串
proxy->setFilterParam(param);

该类的文档由以下文件生成: