Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
样式树视图Qt显示元素数_Qt - Fatal编程技术网

样式树视图Qt显示元素数

样式树视图Qt显示元素数,qt,Qt,是否可以在QT中自定义TreeView?我想在文件名中添加文件数(我自己计算)?我必须使用代理模型吗?沙盒风格的代码片段会很棒 您需要使用并覆盖数据方法。伪代码: int MyProxyModel::myFileCount(const QModelIndex &index) { return 42; // You code here } QVariant MyProxyModel::data(const QModelIndex &index, int role) { c

是否可以在QT中自定义TreeView?我想在文件名中添加文件数(我自己计算)?我必须使用代理模型吗?沙盒风格的代码片段会很棒

您需要使用并覆盖
数据方法。伪代码:

int MyProxyModel::myFileCount(const QModelIndex &index)
{
  return 42; // You code here
}

QVariant MyProxyModel::data(const QModelIndex &index, int role)
{
  const QVariant orig = QIdentityProxyModel::data(index, role);

  switch (role)
  {
  case Qt::DisplayRole:
    return QString("%1 (%2)").arg( orig.toString() ).arg( myFileCount( index ) );
  default:
    break;
  }

  return orig;
}

// Usage:
/* yourModel = new YourOriginalFileSystemModel(); */
QAbstractItemModel* proxy = new MyProxyModel(this);
proxy->setSource( yourModel );
view->setModel( proxy );

它是。代理模型可能是最好的方法。检查文档中是否有代理模型示例。也许有一个索引是一个好主意,保持一个快速重复的模型。可以使用QtConcurrent()完成索引刷新。请确认,您是否继承了QabstracteModel,然后重写了名为“data”的虚拟函数?否,您应该拥有您的模型。你不需要改变它。但您应该创建另一个类,它interhits
QIdentityProxyModel
(如我的回答中所述)。您应该理解,代理模型不收集任何数据。当需要数据时,代理模型从原始(源)模型请求数据,并在必要时覆盖数据。