【问题标题】:How to add a custom role to QFileSystemModel如何向 QFileSystemModel 添加自定义角色
【发布时间】:2018-11-26 10:33:48
【问题描述】:

我想将自定义角色添加到 QFileSystemModel(可能添加到派生模型)。我想使用此角色来保存 CheckBox 的检查状态,该 CheckBox 显示在自定义委托中的文件名旁边。如何做到这一点?

【问题讨论】:

  • 我的解决方案有效吗?
  • 好的,非常感谢。我认为这是一个优雅而简单的解决方案。

标签: c++ qt qml qt5 qfilesystemmodel


【解决方案1】:

我使用示例Qt Quick Controls - File System Browser Example 删除选择的部分。

步骤如下:

  • roleNames中添加新角色:

    QHash<int,QByteArray> roleNames() const Q_DECL_OVERRIDE
    {
        QHash<int, QByteArray> result = QFileSystemModel::roleNames();
        result.insert(SizeRole, QByteArrayLiteral("size"));
        result.insert(DisplayableFilePermissionsRole, QByteArrayLiteral("displayableFilePermissions"));
        result.insert(LastModifiedRole, QByteArrayLiteral("lastModified"));
        result.insert(Qt::CheckStateRole, QByteArrayLiteral("checkRole"));
        return result;
    }
    
  • 创建一个存储选择信息的容器,本例我将使用QMap

    QMap<QPersistentModelIndex, Qt::CheckState> m_checks;
    
  • 覆盖data()方法,如果存储在容器中则返回状态,如果不返回Qt::UnChecked作为默认值:

    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE
    {
        if (index.isValid() && role >= SizeRole) {
            ...
        }
        else if (role == Qt::CheckStateRole) {
            QPersistentModelIndex pix(index);
            if(m_checks.contains(pix)){
                return m_checks[pix];
            }
            return Qt::Unchecked;
        }
        return QFileSystemModel::data(index, role);
    }
    
  • 覆盖setData() 方法,您必须在必要时对其进行修改并创建数据。

    bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole){
        if(role == Qt::CheckStateRole && index.isValid()){
    
            Qt::CheckState current = value.value<Qt::CheckState>();
            if(m_checks.contains(index)){
                Qt::CheckState last = m_checks[index];
                if(last == current)
                    return false;
                m_checks[index] = current;
            }
            else{
                m_checks.insert(index, current);
            }
            emit dataChanged(index, index, {role});
            return true;
        }
        return QFileSystemModel::setData(index, value, role);
    }
    
  • 我添加了一个新列,在该列中我建立了CheckBox 的委托,并使用onCheckedChanged 插槽使用setData() 方法设置值,传递QModelIndex,数据和角色,在这种情况下,传递 10,因为它是 Qt::CheckStateRole 的值数。

    TreeView {
        id: view
        model: fileSystemModel
        ...
    
        TableViewColumn {
            role: "checkRole"
            delegate: Component {
                CheckBox {
                    id: mycbx
                    checked: styleData.value
                    onCheckedChanged: view.model.setData(styleData.index, checked, 10)
                }
            }
        }
    ...
    

完整的例子可以在下面的link找到。

【讨论】:

    猜你喜欢
    • 2013-11-10
    • 2012-02-11
    • 2015-07-16
    • 1970-01-01
    • 2014-08-08
    • 1970-01-01
    • 1970-01-01
    • 2010-11-25
    • 1970-01-01
    相关资源
    最近更新 更多