3

I've got a nested data structure I'd like to display with a QTreeView.

Let's say I've got something like this:

class Image
{
public:
  ...
  std::vector<Filter> filter_;
};

typedef std::vector<Image> Gallery;
typedef std::vector<Gallery> Galleries;

The QTreeView should display the MultiGallery like this:

Gallery1   
  |_____Image1   
  |_____Image2   
  |_____Image3 
Gallery2  
  |_____Image1
  |       |_____Filter1
  |       |_____Filter2  
  |_____Image2

I read the Qt Model View examples, i know I have to derive from QAbstractItemModel to create a treemodel and implement the member functions:

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;

I just don't know whats the best way to implement these, especially the index function.

P3trus
  • 6,747
  • 8
  • 40
  • 54

1 Answers1

1

The main idea is that having an Index (that is row, column and internalId or internalPointer) you should be able to identify both item and its parent.

You data structure does not fit this requirement. You should add links to parent objects to your objects, or use some auxiliary structure to store this information.

Then, you can store pointers to your items (or pointer to auxiliary structure, or better index of auxiliary in array of structures) in indices.

Lol4t0
  • 12,444
  • 4
  • 29
  • 65