user2354422
user2354422

Reputation: 179

ComboBox default value in Qt / C++

I'm having a combobox that I have called as follow:

QComboBox *comboBox_test ;
comboBox_test = new QComboBox(this);
comboBox_test ->setGeometry(QRect(10, 10, 50, 20));
comboBox_test ->insertItems(0, QStringList() << "A" << "B");

What I would like to do is to set the "B" as the default value.

I didn't find the way to add that code of line which allows me to do so.

Upvotes: 2

Views: 7947

Answers (2)

UmNyobe
UmNyobe

Reputation: 22890

You have two alternatives given the example you provided. You can use directly setCurrentIndex() given you know the index, or retrieve the index first using findText

Thus initially you can use

comboBox_test->setCurrentIndex(1);

Later if you want to reset to "B" on screen

int index = comboBox_test->findText("B"); //use default exact match
if(index >= 0) 
     comboBox_test->setCurrentIndex(index);

Upvotes: 6

Xsmael
Xsmael

Reputation: 3932

Here is an easier way call setCurrentText() instead of setCurrentIndex()

comboBox_test->findText("B");

you can so it in just one line! and it is safe, if "B" do not exists in the list nothing will happen.

Upvotes: 0

Related Questions