QSortFilterProxyModel filterAcceptsColumn无法设置新的索引数据

时间:2012-04-02 11:03:20

标签: c++ qt

我正在使用QSortFilterProxyModel来过滤来自QSqlQueryModel模型的列 但是因为filterAcceptsColumn方法是const“一直” 我有问题在右侧索引中设置Qt :: UserRole数据。我怎么能克服这个?

    bool MiniSortFilterProxyModel::filterAcceptsColumn(int source_column, const QModelIndex& index) const
    {
        QVariant tmp ;
        if(source_column ==0)
        {

            setRowid(index.data(Qt::DisplayRole)); 
// here im getting compilation error 
//: error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const QString' (or there is no acceptable conversion)
            m_rowId = index.model()->data(index,Qt::DisplayRole).toString();
            return false;
        }
        else if(source_column ==1)
        {
            setYTid(index.data(Qt::DisplayRole)); 
            return false;
        }
        else if(source_column ==2)
        {

            setNewData(index);
            return QSortFilterProxyModel::filterAcceptsColumn(source_column,index);
        }
        return false;
    }


    void MiniSortFilterProxyModel::setRowid(QVariant rowId) const 
    {
// here also compilation error:
        m_rowId = rowId.toString();
    }

    void MiniSortFilterProxyModel::setYTid(QVariant ytId) const 
    {
/ here also compilation error:      
m_ytId = ytId.toString();
    }

    void MiniSortFilterProxyModel::setNewData(QModelIndex& index) const 
    {
// here also compilation error:
//error C2511: 'void MiniSortFilterProxyModel::setNewData(QModelIndex &) const' : //overloaded member function not found in 'MiniSortFilterProxyModel'             
 QVariant data = m_rowId+"_"+m_ytId;
            index.model()->setData(index,data,Qt::UserRole);
    }

1 个答案:

答案 0 :(得分:1)

快速而肮脏的修复方法:将m_rowId声明为mutable

mutable QString m_rowId;

无论如何,如果该方法在Qt API中被声明为const,那么有一个原因,所以请确保一个是唯一可以分配该值的地方。