PDA

View Full Version : QComboBox currentItem



Project25
28th April 2008, 06:00
I have a question about QComboBox setCurrentText. I have already two default values in QComboBox Item1, Item2.

Slot

void CMainClass::readOut()
{

QString str = process->readLineStdOut();
comboBox->setCurrentText(str);
}

The return value for str will be Item1 and if slot will triggerred again then str will be Item2. So return value will either Item1 or Item2 .I have problem if I use setCurrentText() method then it is overwriting my current value means ..the index 1 current value.

How do I handle this through looping or through index such as setCurrentItem(int index) method..I want to do something like.. if str value is Item1 then I want to display that value on comboBox index 1 and if return value is Item2 then I want to display on index2.

Thanks.

jacek
29th April 2008, 00:47
setCurrentText() changes the current item's text, not the current item. Either use QMap to map item texts to indices or go through the items until you find matching one and use setCurrentItem().

mcosta
29th April 2008, 08:27
How do I handle this through looping or through index such as setCurrentItem(int index) method..I want to do something like.. if str value is Item1 then I want to display that value on comboBox index 1 and if return value is Item2 then I want to display on index2.

Thanks.


Try this


int index = comboBox->findText(str);
if(index != -1)
{
comboBox->setCurrentIndex(index);
}