PDA

View Full Version : better way to set data in a QCombobox



juliano.gomes
21st November 2015, 01:03
Hello,
I'm implementing my "QCombobox set current data" and I’m in doubt about what is the better way?

1) using variable and QMapIterarot to manipulate data, and QCombobox.setCurrentText()


QMapIterator<QString, int> i(_cbNames);
while (i.hasNext()) {
i.next();

if (query2.value(1) == i.key())
{
ui->qcomboboxNames->setCurrentText(i.key());
}



2) using directly combobox to manipulate data and QCombobox.setCurrentIndex()


for (int i=0;i<=ui->qcomboboxNames->count();i++)
{
ui->qcomboboxNames->setCurrentIndex(i);
if (query2.value(1) == ui->qcomboboxNames->currentText())
{
break;
}
}


Thanks!
Juliano

jefftee
21st November 2015, 03:50
Have you tried QComboBox::addItem and QComboBox::addItems?

juliano.gomes
21st November 2015, 03:59
Have you tried QComboBox::addItem and QComboBox::addItems?

Hello, thanks for your reply, but I dont have doubts about add item. My doubt is What of 2 methods (or another) are the Best way to set the current data in a combobox witch Already have items.

Thanks
Juliano

jefftee
21st November 2015, 04:47
If that's all you're asking, surely you can time both methods and decide for yourself which is better, right?

Lesiok
21st November 2015, 09:49
Why not like this :
int index = ui->qcomboboxNames->findText(query2.value(1));
if( index >= 0 )
ui->qcomboboxNames->setCurrentIndex(index);

juliano.gomes
21st November 2015, 17:46
Why not like this :
int index = ui->qcomboboxNames->findText(query2.value(1));
if( index >= 0 )
ui->qcomboboxNames->setCurrentIndex(index);

Yeah! This code is more simple and clear, thanks bro!