20

I have a simple QComboBox widget, which has 2 values inside: True and False. And I have a QString variable currValue, which is one of those values. I want to set my widget's current value with currValue.

I thought that solution is the following: first lets initialize currValue; QString currValue = "False";

QComboBox* combo = new QComboBox();
combo->addItem("True");
combo->addItem("False");
combo->setCurrentIndex(combo->findData(currValue));

But it doesn't work. Am I doing something wrong ? And Why QComboBox has no member setCurrentItem() or smth like that ?

Karen Tsirunyan
  • 1,938
  • 6
  • 19
  • 30

2 Answers2

35

You actually need to write it in the following way:

QComboBox* combo = new QComboBox();
combo->addItem("True", "True");
combo->addItem("False", "False");
combo->setCurrentIndex(combo->findData("False"));

The problem in your implementation was that you did not set the items' userData, but only text. In the same time you tried to find item by its userData which was empty. With the given implementation, I just use the second argument of QComboBox::addItem(const QString &text, const QVariant &userData = QVariant())) function that sets the item's userData (QVariant).

UPDATE:

The alternative way to find the combo box item is setting the specific role as the second argument for QComboBox::findData() function. If you don't want to explicitly set the user data, you can refer to the items texts with Qt::DisplayRole flag, i.e.:

QComboBox* combo = new QComboBox();
combo->addItem("True");
combo->addItem("False");
combo->setCurrentIndex(combo->findData("False", Qt::DisplayRole)); // <- refers to the item text

UPDATE 2:

Another alternative could be using text based lookup function QComboBox::findText():

QComboBox* combo = new QComboBox();
combo->addItem("True");
combo->addItem("False");
combo->setCurrentIndex(combo->findText("False"));
vahancho
  • 20,808
  • 3
  • 47
  • 55
  • Why ?! What is the meaning of 2 equal arguments ?? – Karen Tsirunyan Oct 17 '13 at 14:41
  • I see. But In this case I think that it is confusing that if I have just texts and I don't need data, I have to set also data when adding new items. I think there MUST be easier way to set selected, the text I have. – Karen Tsirunyan Oct 17 '13 at 14:46
  • @KarenTsirunyan: well, if you don't want to set the user data, you can refer to the combo box items' texts. See another update of my answer. – vahancho Oct 17 '13 at 14:52
  • I understand that you suggest real solutions, but as You can see there are a little bit weird, as I want to find the text, but I work with data. I just found out that `QComboBox` has also `findText()` method, which is the very thing I wanted to. :) – Karen Tsirunyan Oct 17 '13 at 14:59
  • @KarenTsirunyan, I proposed three different ways to get the same result - you can use either of them. – vahancho Oct 17 '13 at 15:01
  • Thank you, vahancho, for a well-written exploration of the ways to use a QComboBox. I was looking at the manual and thinking "I need to make a QVariant object, whatever that is... how do I store my object in that? Ooh, so it says here I need to write some kind of custom subclass of QVariant for user data? Just to populate a combobox? WTF?" - and then your examples showed me just how easy it really is. Now I just have all my data in an array, and pass the index as the userData, and life is delightfully easy for me. So, thank you! – Dewi Morgan Nov 26 '14 at 00:46
6

I have got answer to my own question.

combo->setCurrentIndex(combo->findText(currValue));
Karen Tsirunyan
  • 1,938
  • 6
  • 19
  • 30