Qt model tree view and table view

I have a list of Product objects (in the form of rows), each object has several data fields (for example, product identifier, product family, etc.) - like columns. I managed to subclass the QAbstractTableModel table model and display the data in a QTableView.

What I want is a "grouped" view of the model in a tree structure. For example, I want to group products by their family id, so the tree should contain groups at the first level (family identifier) ​​and their children are the product that has this family identifier.

Any ideas?

PS: The need for several groupings (group by family ID and grouping by another column, etc.) shows me that the model should be a tree. Root nodes indicate groups, and then the children are members of the group. Multiple grouping can be achieved by tiered trees.

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

I can use setRootIndex in the table view, but it does not solve anything, other sheets are not displayed.

I dug a little and found that in QTableView :: paintEvent, when displaying each row and column, the actual element is selected like this:

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

d- > root() - node, setRootIndex(). , . paintEvent?

+3
1

, , QTreeWidget , , , . , , . , , "QModelIndex" "parent", .

, , , QStandardItem QStandardItemModel. , , , .

, QAbstractItemModel, .

, , QT - , QAbstractItemModel (, , / )

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);
}

: ...

, , , , .

+1

Source: https://habr.com/ru/post/1716425/


All Articles