我有一个嵌套数据结构,我想用 QTreeView 显示。
假设我有这样的东西:
class Image
{
public:
...
std::vector<Filter> filter_;
};
typedef std::vector<Image> Gallery;
typedef std::vector<Gallery> Galleries;
QTreeView 应该像这样显示 MultiGallery:
Gallery1
|_____Image1
|_____Image2
|_____Image3
Gallery2
|_____Image1
| |_____Filter1
| |_____Filter2
|_____Image2
我阅读了 Qt 模型视图示例,我知道我必须从 QAbstractItemModel 派生来创建树模型并实现成员函数:
QVariant data(const QModelIndex &index, int role) const;
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const;
QModelIndex parent(const QModelIndex &index) const;
int columnCount(const QModelIndex &parent=QModelIndex()) const;
int rowCount(const QModelIndex &parent=QModelIndex()) const;
我只是不知道实现这些的最佳方法是什么,尤其是索引功能。