• 653阅读
  • 2回复

[讨论]如何重写QFileSystemModel [复制链接]

上一主题 下一主题
离线lwei24
 

只看楼主 倒序阅读 楼主  发表于: 2023-02-13
各位大佬,请问如何重写QFileSystemModel,使得QFileSystemModel的内容以表头为名称、修改日期、类型、大小的形式显示出来呢?在线等,欢迎各位大佬们指点一二,谢谢!
在线liudianwu

只看该作者 1楼 发表于: 2023-02-13
继承就好
欢迎关注微信公众号:Qt实战/Qt入门和进阶(各种开源作品、经验整理、项目实战技巧,专注Qt/C++软件开发,视频监控、物联网、工业控制、嵌入式软件、国产化系统应用软件开发) QQ:517216493  WX:feiyangqingyun  QQ群:751439350
离线lwei24

只看该作者 2楼 发表于: 2023-02-14
回 liudianwu 的帖子
liudianwu:
继承就好

嗯嗯,继承后,我是想重写里面的data,例如源码中的
  1. QVariant QFileSystemModel::data(const QModelIndex &index, int role) const
  2. {
  3.     Q_D(const QFileSystemModel);
  4.     if (!index.isValid() || index.model() != this)
  5.         return QVariant();
  6.     switch (role) {
  7.     case Qt::EditRole:
  8.     case Qt::DisplayRole:
  9.         switch (index.column()) {
  10.         case 0: return d->displayName(index);
  11.         case 1: return d->size(index);
  12.         case 2: return d->type(index);
  13.         case 3: return d->time(index);
  14.         default:
  15.             qWarning("data: invalid display value column %d", index.column());
  16.             break;
  17.         }
  18.         break;
  19.     case FilePathRole:
  20.         return filePath(index);
  21.     case FileNameRole:
  22.         return d->name(index);
  23.     case Qt::DecorationRole:
  24.         if (index.column() == 0) {
  25.             QIcon icon = d->icon(index);
  26. #if QT_CONFIG(filesystemwatcher)
  27.             if (icon.isNull()) {
  28.                 if (d->node(index)->isDir())
  29.                     icon = d->fileInfoGatherer.iconProvider()->icon(QFileIconProvider::Folder);
  30.                 else
  31.                     icon = d->fileInfoGatherer.iconProvider()->icon(QFileIconProvider::File);
  32.             }
  33. #endif // filesystemwatcher
  34.             return icon;
  35.         }
  36.         break;
  37.     case Qt::TextAlignmentRole:
  38.         if (index.column() == 1)
  39.             return QVariant(Qt::AlignTrailing | Qt::AlignVCenter);
  40.         break;
  41.     case FilePermissions:
  42.         int p = permissions(index);
  43.         return p;
  44.     }
  45.     return QVariant();
  46. }
如上,第一行的数据变成第三行的数据。
快速回复
限100 字节
 
上一个 下一个