基於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);
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM