【问题标题】:QTreeView QFileSystemModel - limit expandable to only rootQTreeView QFileSystemModel - 限制只能扩展为根
【发布时间】:2015-06-23 08:02:35
【问题描述】:

我正在使用 QTreeView 和 QFileSystemModel。我希望 only 根是可扩展的,显示 1 级子目录,仅此而已,子目录应该只能选择但不可扩展。请指导我如何存档?

谢谢。

【问题讨论】:

  • 您可以创建一个过滤器代理模型来完成它。视图无需更改。

标签: qt pyqt pyside qtreeview qfilesystemmodel


【解决方案1】:

如前所述,您可以创建代理模型来修改模型的行为:

class Proxy : public QSortFilterProxyModel {
public:
  static int indexLevel(QModelIndex index) {
    int level = 0;
    while(index.parent().isValid()) {
      level++;
      index = index.parent();
    }
    return level;
  }

  int rowCount(const QModelIndex& parent) const {
    if (indexLevel(parent) > 0) {
      return 0;
    }
    return QSortFilterProxyModel::rowCount(parent);
  }

  bool hasChildren(const QModelIndex& parent) const {
    if (indexLevel(parent) > 0) {
      return false;
    }
    return QSortFilterProxyModel::hasChildren(parent);
  }
};

//...
QFileSystemModel model;
model.setRootPath(QString());
Proxy proxy;
proxy.setSourceModel(&model);
QTreeView view;
view.setModel(&proxy);
view.show();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多