Qt自定义嵌套向量的树模型
Qt custom tree model of nested vector
我有一个嵌套的数据结构,我想用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;
我只是不知道实现这些功能的最佳方法是什么,尤其是索引函数。
主要思想是,有了索引(即行、列和internalId或internalPointer),您应该能够识别项及其父项。
您的数据结构不符合此要求。您应该向对象添加指向父对象的链接,或者使用一些辅助结构来存储这些信息。
然后,您可以将指向项的指针(或指向辅助结构的指针,或结构数组中辅助结构的更好索引)存储在索引中。
相关文章:
- 写入向量<向量<bool>>
- 函数向量_指针有不同的原型,我可以构建一个吗
- std::向量与传递值的动态数组
- 将值指定给向量(2D)的向量中的某个位置
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 如何使用向量的template_back函数
- 尝试通过多个向量访问变量时,向量下标超出范围
- 如何通过派生类函数更改基类中的向量
- C++从另一个类访问公共静态向量的正确方法是什么
- 如何将ampl中的集合表示为c++中的向量
- 变量没有改变?通过向量的函数调用
- 迭代时从向量和内存中删除对象
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- 如何为模板化对象创建模板向量?VS正在投掷C3203
- 计算排序向量的向量中唯一值的计数
- 如何使用 C++ 向量追加列表模型
- QML ListView将新行添加到模型中的子向量
- opencv:使用旋转和平移向量设置模型位置
- Qt自定义嵌套向量的树模型
- 模型视图控制器- c++向量指针,太多的传递