PDA

View Full Version : How to use QTreeView in QComboBox?



Tamara
15th June 2007, 10:32
I try to do something like this (original code was written in Python, so may be it has some syntax errors):


QTreeView *tvGroups = new QTreeView()
MyModel *groupModel = new MyModel()
cb->setView(&tvGroups)
cb->setModel(&groupModel)
cb->setModelColumn(0)

cb is QComboBox.
After it usual integer combobox indexes become very strange.
In function which is connected to signal setCurrentIndex(int index) I print index and see that it has value -1 for items group2 and something_else, and 0 for others in the tree example below.:eek:
tree has such items:


All
group1
group1.1
group1.2
group2
something_else

What is wrong?

Sid
15th June 2007, 18:12
You need to set the model first on the treeview.
treeview->setModel(model)
and then tell the combobox to use that model.

Or you can just use QTreeWidget directly using default model :



QTreeWidget *tree = new QTreeWidget();
cb->setModel( tree->model());
cb->setView ( tree);

add a few items (QTreeWidgetItem) to you tree and see if that works for you


Sid

dblevitan
20th June 2007, 19:03
I am also trying to use a QTreeWidget inside of a QComboBox (although using PyQt). I did what was suggested (although in my case, I subclass QComboBox), but my issue is that when I click on the combo box, it opens up with only one line that is not scrollable except using the mouse wheel. Clicking on any of the other entries results in the first entry being displayed or no entry being displayed.

Any idea on how to fix this?

Thanks.