PDA

View Full Version : QCombobox with QTreeView/QTreeWidget



swiety
8th July 2008, 21:42
I have problem with QComboBox::setCurrentIndex( int ). I have siply tree model, like this ( QComboBox::setModel() and QComboBox::setView() ) :
---- A // index = 0
----------- B // index = 0
------------ C // index = 1
----------- D // index = 2
----- E // index = 1
----- F // index = 2
----- G // index = 3
B,C and D have parent A. So, when i call QComboBox::setCurrentIndex( 1 ), then currentIndex is for E. But how can i set currentIndex for C ? SetCurrentIndex / SetCurrentItem in model / view doesn't work. Any idea?

wysota
8th July 2008, 22:19
From what I know the combobox doesn't work well with hierarchical models. You can use setRootModelIndex() but then you won't see the indexes higher in the hierarchy.

timatti
30th August 2011, 20:27
I have been digging for this for a while and just figured out a solution. Just going to put it here in case anyone ever needs something similar. The approach is to set the root model index to the parent of the element you want, set the current index to the element index within the element's siblings, and then to set the root model index back to the invisiblerootitem of the tree. For my code, it was something like this (loDeviceTree was the QTreeWidget, and loDeviceComboBox was the comboBox that contained loDeviceTree as its model/view):



loDeviceTree->setCurrentItem(DeviceIWant->parent(),0);
loDeviceComboBox->setRootModelIndex(loDeviceTree->currentIndex());
loDeviceComboBox->setCurrentIndex(IndexOfTheDeviceIWant)
loDeviceTree->setCurrentItem(loDeviceTree->invisibleRootItem(),0);
loDeviceComboBox->setRootModelIndex(loDeviceTree->currentIndex());