custom treeview / model
-
Hello ,
When the relevant parent is clicked as in the picture above, the information of the parent is opened as a separate node on the same line.
When that node is clicked, the information of the node is opened in the same way.
how should i do this? Can you give me an idea? -
Hi,
If I understand you correctly, you want to implement some sort of horizontal QTreeView ?
From the looks of it, you will have to build your own QAbstractItemView.
-
@SGaist said in custom treeview / model:
From the looks of it, you will have to build your own QAbstractItemView.
Thanks a lot for your answer. Sorry but I am really inexperienced with this treeview. How exactly do I need to change this horizontal line opening event compared to a normal Qtreeview?
-
@Nevez
What @SGaist is suggesting --- and it seemed to me that way too, plus he really know what he is talking about! --- is that aQTreeView
really is not going to do your "horizontal" requirement. It is not going to do "create new columns on node expansion".QTreeView
is designed to expnad nodes such that sub-nodes/children appear on a new row, and that is fundamental. Same if you look at aQTableView
.Which means you are likely to need to "roll your own" tree view starting out from
QAbstractItemView
. Probably quite a bit of work --- think how much aQTreeView
adds on top of this --- but not impossible.BTW, it is not evident to me what your "rules" are for horizontal vs vertical expansion. A cursory glance might indicate that your nodes are arranged vertically in rows while your leaves are expanded out horizontally in columns (and if more than one child, subsequent ones on a new row). However, that does not apply in your diagram to the
Parent6 -> child1 -> child1:1
case.child1
is also a node (parent
), so why should it not have been given its own row like theparent
s? Whatever your algorithm for this is you will have to code it. -
Thanks for the comments, I'll try them out.
The rule will be determined by the data from the db.
In other words, the data (nodes) in the same row in the db will be opened in the same horizontal (row) again.Example:
table in my db:
the view should be like this;
jhon->wick->20->mercedes->black(black is last child and not parent).
bella->chrown->33->>bmw->red(red is last child and not parent) -
@Nevez
OK, but this is rather different from your first diagram. For example, it is not clear where 2 rows withchild1
andchild2
would come from....Your table is just straight rows with columns. There is no "tree structure" here. If this is all you want you might be able to do with just a straight
QTableView
, where each individual column in each row (i.e. each cell) is allocated to the table view but is initially hidden. Then when the user does some sort of "click to expand" that causes the column in row (i.e. cell) to now show its content.That would probably be a lot easier then having to write a lot of stuff yourself. But whether it's enough for what you really want I cannot say.
-
@Nevez
Certainly you would get the best "effect" by subclassingQAbstractItemView
and doing your work, because that is the most flexible. All the other views are derived from that, so by definition you can do anything they do, plus more. But the question is how much work that is: if you can leverage an existing table or tree view that saves you work.I'm not sure a
QTreeView
would work for you, though it would be nice if it did. Again, so far I could more or less do what you just showed in aQTableView
: the rows would be sorted by first name, then by last name, .... Given the same value in first column (John
) you want to display that cell only the first row, then show it blank in all subsequent, consecutive rows where value is the same. And similarly on second column if you have multiple, adjacent rows forJohn
followed byWick
.I still think you're going to have to do a bit of work, to include the "click to reveal subsequent columns". Honestly I'm not sure you will know what you can achieve till you play with trying to do something...
-
One important thing: you should build a proxy model that packs the data the way you want it. It's not the role of the view to rearrange data for painting.
-
No, globally. You start from table based data to finish with a three like representation. Hence, use a model to have your data structured the way you need it.