基于Qt的QFileSystemModel做一个带checkbox的文件树

做之前看了一下QFileSystemModel和QDirModel,发现Qt官方是推荐使用QFileSysteModel的,因为QDirModel是一次性加载所有的文件目录,包括所有的子目录的,这样性能上就会很慢,而QFileSystemModel则是异步载入的,那样只有你点开一个节点,只会加载它的下一级目录的节点。所以我们用QFileSystemModel来做复选框的时候,就会出现这么一个问题,选中了某一个文件夹,其实他的目录下的文件都是没有被选中的,因为他们还没有被加入到QFileSystemModel中去。

这里我的思路是派生了一个QFileSystemModelImpl类,用一个m_indexMap来记录文件的选中状态,m_checkedFileList来记录选中的文件路径。

mutable QMap<QModelIndex, bool> m_indexMap;
    QSet<QString> m_checkedFileList;

 

重载了QFileSystemModel的几个实现方法,每一个节点的创建会调用到data()方法(创建只会调用一次这个方法),在此方法里面向上递归他的父节点,如果他有父节点为选中状态的,则将此节点设为选中。代码如下:

QVariant QFileSystemModelImpl::data( const QModelIndex &index, int role /*= Qt::DisplayRole*/ ) const
{
    if (!index.isValid())
    {
        return QVariant();
    }

    //first column is checkbox
    if (index.column() == 0 && role == Qt::CheckStateRole)
    {
        if (m_indexMap.contains(index))
        {
            return m_indexMap[index] ? Qt::Checked : Qt::Unchecked;
        }
        else
        {
            int iChecked = Qt::Unchecked;
            QModelIndex _parentIndex = index.parent();

            //check if this node's parent is checked
            while(_parentIndex.isValid())
            {
                if (m_indexMap[_parentIndex])
                {
                    iChecked = Qt::Checked;
                    break;
                }
                else
                {
                    _parentIndex = _parentIndex.parent();
                }
            }

            if (iChecked == Qt::Checked)
            {
                m_indexMap[index] = true;
            }
            else
            {
                m_indexMap[index] = false;
            }

            return iChecked;
        }
    }

    if (role != Qt::DisplayRole)
    {
        return QVariant();
    }

    return QFileSystemModel::data(index, role);
}

每次改变一个节点的状态,调用setdata()方法,去遍历在QFileSystemModel中的它的子节点将他们的状态与他设为一致。代码如下:

bool QFileSystemModelImpl::setData( const QModelIndex &index, const QVariant &value, int role /*= Qt::EditRole*/ )
{
    if (role == Qt::CheckStateRole && index.column() == 0)
    {
        if (value == Qt::Unchecked)
        {
            m_indexMap[index] = false;
            //refresh it's child node
            emit dataChanged(index, index);
        }
        else if (value == Qt::Checked)
        {
            m_indexMap[index] = true;
            //refresh it's child node
            emit dataChanged(index, index);
        }

        if (hasChildren(index))
        {
            QString strFilePath = filePath(index);
            setFileCheckState(strFilePath, value);

            int iChildCount = rowCount(index);

            if (iChildCount > 0)
            {
                for (int i = 0; i < iChildCount; i++)
                {
                    QModelIndex _child = this->index(i, 0, index);
                    setData(_child, value,Qt::CheckStateRole);
                }
            }
        }
    }

    return QFileSystemModel::setData(index, value, role);
}
posted @ 2018-03-10 21:51  MasterWuGUI  阅读(2228)  评论(1编辑  收藏  举报