tags:

views:

2095

answers:

1

I have a list of Product objects ( as rows ), each object has several data fields ( like product id, product family, etc ) - as columns. I managed to create a table model subclassing QAbstractTableModel, and display the datas in a QTableView.

What i want is provide a "grouped" view of the model in a tree-like structure. For example I want to group the products by their family id, so the tree should contain in the first level the groups ( family id ) and their childs are the product's having that family id.

Any ideas?

PS: The need of multiple groupings ( group by family id, and group them by another column, and so on ) shows me that the model should be a tree. The root nodes indicates the groups, and then the childs are the group members. The multiple grouping can be achieved by multi-level trees.

Then my transformed question is the following: How can I implement a special QTableView class that displays only the tree's leafs ( at a given level )? ( Because actually leafs are the real objects we would like to see in a grid )

I can use setRootIndex in the tableview, but it doesn't solve anything, other leafs are not displayed.

I was digging a little bit, and found that in QTableView::paintEvent, when displaying each row and column, the actual item is fetched like :

const QModelIndex index = d->model->index(row, col, d->root);

where d->root() is the root node given by setRootIndex(). This should be embedded in a tree-traversal code, and everything would be fine. But how could I avoid to reimplement the whole paintEvent method ?

A: 

Depending on how much customisation you want the QTreeWidget might be the easiest route, but if you already have a model, it depends on which one you derived from. To get to the functionality you would like you need to add child structures to the rows that you have already built up. You might have missed it but the 'QModelIndex' class has a 'parent' field, this helps build up an hierarchic structure.

You will probably well served by taking the QStandardItem and QStandardItemModel classes. These have a decent interface to build a tree structure, depending on what data you will need to display each level in the hierarchy can have a different number of columns and there is no limit to the depth.

If you feel adventuresome you can take it from the top and just implement the QAbstractItemModel interface, but that can be hard to get right sometimes.

Overall I think that this is one of the strangest concepts in QT due to the amount of dimenionalities that are in the QAbstractItemModel (rows, columns, parent/child and roles)

For Example

for (int i = 0; i < 3; ++i) {
    QStandardItem *parent= new QStandardItem("Family " + QString::number(i),this);
    item->setRowCount(3);
    for (int j = 0; j < 3; ++j) {
        QStandardItem *child = new QStandardItem("Child " + QString::number(i*3+j), this);
        parent->setChild(j,child);
    }
    model->appendRow(parent);
}

Disclaimer: this has not been compiled ...

But as you can see you need to add the children to the item, and not the model, each item can contain a hierarchy of items.

Harald Scheirich