PDA

View Full Version : QTreeWidget or QTreeView ?



vieraci
7th November 2007, 04:03
I'm designing a mainMenu form a bit like Qt Assistant and other GUI forms I've seen with a tree on the left and a stackedWidget on the right.
I want to react to a tree item selection and flip the stackedWidget to the corresponding page. The cleanest and less-coding method I can think of is obtaining an integer of the selected menu item row so in my code I can simply code

stackedWidget->setCurrentIndex(row)
I've experimented with both QTreeWidget or QTreeView and in both cases, I have to extract the parent and child and write a long if...else if...endif block to see what was selected. If there's no row() function available in either QTreeWidget or QTreeView I would probably add an invisible column that holds an int row.
I also want to dynamically be able to insert or remove pages to the stackedWidget and update the tree at runtime via double-click and/or context menu on the tree.

Which is more suitable...QTreeWidget or QTreeView ?
All comments and criticism cheerfully accepted.
Thanks,

wysota
7th November 2007, 06:20
I don't think you need any long if statements for either QTreeWidget or QTreeView. The most straightforward way is to connect to currentItemChanged() or selectionModel()->currentChanged() signal (depending on the tree class you choose). Then you'll be able to use QModelIndex::row() to obtain the integer you want.

vieraci
7th November 2007, 07:17
Then you'll be able to use QModelIndex::row() to obtain the integer you want.
This only returns the row for the parent. What's needed is this:
Parent 1
child 1
child 2
Parent 2
child 1
child 2
so the last item should return 5, not 1.

mchara
7th November 2007, 10:31
hi,

QTreeView is meant to use with ItemModels i.e. when you have single data model and several views that shows it's contents in a different way or if you are viewing dbase contents(via QSql*Model which deals with dbase for you).
QTreeWidget is in deed QTreeView with internal model & methods that deals with model in context of indexes.

I think QTreeWidget is better for you, because you need single view, so providing model isn't necessary and QTreeWidget gives you some simplifications like mentioned row calculation.