PDA

View Full Version : can't select item in a QTreeWidget via code



zack
14th July 2009, 12:50
I have a QCombox and in the combobo a TreeWidget


comboBox->setModel(treeWidget->model());
comboBox->setView(treeWidget);


Now I want to pre select an item in this treeWidget


treeWidget->scrollToItem (Item);
treeWidget->setCurrentItem(Item);


But for some reason, it doesn't select the Item.
In the combobox is still the first toplevelItem visible and when I click on the combobox he opens the correct toplevelitem, where Item is placed, but it's not selected.

Is it a bug, or am I on the wrong way?

calhal
14th July 2009, 21:31
Instead of this:

treeWidget->scrollToItem (Item);
treeWidget->setCurrentItem(Item);

You should set correct index on your QComboBox:

comboBox->setCurrentIndex(treeWidget->indexOfTopLevelItem(Item));

Anyway, why do you want to put a QTreeWidget into a combo box? IMHO if you have a tree structure it won't be nice. And if you don't have a tree structure, what's the point of using QTreeWidget here?