QStandardItem containing multiple children not shown
-
Hello!
I got the following problem:
I try adding some child-items to a QStandardItem inside a QStandardItemModel as model for a QTableView.
Items without children behave as expected and are shown in the QTableView, items WITH children (also QStandardItems) won't show. I can access and alter them, but they remain invisible.Here some "pseude-code" of how i tried to do it:
@
QTableView tableView;
QStandardItemModel itemModel;tableView.setModel(itemModel);
QStandardItem item;
item.appendRow(QStandardItem("GrandChild 1"));
item.appendRow(QStandardItem("GrandChild 2"));
item.appendRow(QStandardItem("GrandChild 3"));itemModel.appendRow(item);
@Could anybody help me?
-
In the example you give here, you haven't given the parent any text content: in your real case does it have some string associated with it? A QTableView is not going to traverse a hierarchy the way a QTreeView does, so it won't show any children, since it doesn't know anything about that kind of relationship between items. But I think it should still show the parent. If, however, the parent doesn't have any data associated with it except for its children, then there is nothing to show.
-
In my case, there is no text for the parent-item. What im trying to achieve should look a little something like this:
@
Header 1 | Header 2 | Header 3TextText | TextText | TextText
| TextText | TextText
| | TextTextTextText | TextText | TextText
| TextText | TextText
| | TextText@
So there are multiple rows as items in one cell. I first tried it with one item and multiple strings divided by a "\n" to have each shown in a new row. It actually worked but its rather complicated to manipulate the data since you have to parse the strings etc.
Is there any other solution for this? -
It is very simple: a QTableView only shows a single level of a hierarchical model. So, it is the wrong view for what you are after.
There is no standard view that does what you need directly. You would have to make it, and doing that is quite tricky I think. I don't think the UI you propose is very good though. I at least don't understand the relationships between the different items you are displaying in your sample above.
-
It's about representing a "1 to n" relationship in a list-like table. So one item has multiple attributes.
I guess i stick to the string-solution i had :/ -
So, the items on the 1 side of your 1-n relation are under Header 1, and on the n site under Header 2? And what is under Header 3 then? Is that another 1-n relation to whatever is under Header 1, or are these related to what is under Header 2?
In principle, a QTreeView can display the structure you are after. However, your current data structure would result in this:
@
TextText
TextText
TextText
TextText
TextText
TextText
@
Even if you were put the contents of whats in the cild items in the second column, these would still not start at the same row as the parent item.I think you could create a proxy model to create what you are after. I think mapping the indexes would be doable. That way, you get to keep both your tree-based data structure and you can present it in a table-like structure. That approach would however make every item a separate cell, so the blocks you draw in your table above would not be just one row, but three.
-
is it maybe possible to have for example a QListView for each cell of a QTableView ?