kiriloff
kiriloff

Reputation: 26335

Qt QComboBox - change current index not as expected

I would like to have a given item in comboBox selected when opening editor, and when loading given data that should update in editor. Combobox is not working as expected!

I have these lines for having given item selected when I open the window:

normBox = new QComboBox(page1);
gridBox->addWidget(normBox, 2, 1, 1, 1);
QStringList normsLst;
normsLst.append(tr("sum"));
normsLst.append(tr("maxF"));
normsLst.append(tr("sumF"));
setComboBoxItems(normsLst, m_normBox);
m_normBox->setCurrentIndex(0);

but first item 'Sum' is not displayed in combobox when I create editor.

Then, I implemented my 'load' function, which should load the saved data in interface ending with a refresher:

normBox->setCurrentIndex(model->getNormIdx());

where model has accurate member normIdx.

How can I achieve the right selection in combo box with Qt?

Upvotes: 1

Views: 3476

Answers (1)

hank
hank

Reputation: 9853

Maybe you create a combobox and then set the current index of another one?

normBox = new QComboBox(page1);
...
m_normBox->setCurrentIndex(0);

And what is setComboBoxItems(normsLst, m_normBox)? I didn't find this function (with two arguments) in the Qt documentation.

This code should work:

QComboBox* combo = new QComboBox;

QStringList list;
list << "sum" << "maxF" << "sumF";

combo->addItems(list);
combo->setCurrentIndex(0);

Upvotes: 6

Related Questions