6

I am trying to add items to QComboBox using insertItems function as follow:

QStringList sequence_len = (QStringList()
<< QApplication::translate("MainWindow", "1", 0, QApplication::UnicodeUTF8)
<< QApplication::translate("MainWindow", "2", 0, QApplication::UnicodeUTF8)
<< QApplication::translate("MainWindow", "3", 0, QApplication::UnicodeUTF8)
<< QApplication::translate("MainWindow", "4", 0, QApplication::UnicodeUTF8)
<< QApplication::translate("MainWindow", "5", 0, QApplication::UnicodeUTF8)
);

ui->QComboBox->insertItem(0, &sequence_len);

but is not working, giving me the following error message:

error: no matching function for call to 'QComboBox::insertItem(int, QStringList*)'

Actually, when I write ui->QComboBox->insertItem( in my class to see the Qt-Creator's suggestions, the option:(int index, const QStringList & list) doesn't appear to be exist. So, at first, I thought it is because my Qt-Creator doesn't support this function. However, surprisingly, when filling the QComboBox directly from the "Design" tab in Qt-Creator after creating the QComboBox widget, the same function is being used by ui_mainwindow.h.

why is this happening and is there is a way to add this function to my class as well?

Joel Bodenmann
  • 2,152
  • 2
  • 17
  • 44
hashDefine
  • 1,491
  • 6
  • 23
  • 33

3 Answers3

7

Use addItems or insertItems member function of QComboBox. //notice there is an s in the end for the functions that takes a QStringList argument: it's add/insert Items

LE: don't pass the address of your QStringList, the function takes a reference to a QStringList object, not a pointer, use: ui->QComboBox->insertItems(0, sequence_len); //no & before sequence_len

Complete example of filling QComboBox (considering that tr() is properly setup):

QStringList sequence_len = QStringList() << tr("1") << tr("2") << tr("3") << tr("4") << tr("5");
//add items:
ui->QComboBox->addItems(sequence_len);
//insert items into the position you need
//ui->QComboBox->insertItems(0, sequence_len);
Zlatomir
  • 6,964
  • 3
  • 26
  • 32
  • Thanks. Removing the pointer and replace insertItem by addItem make it work. – hashDefine Jun 11 '13 at 13:42
  • 1
    insertItems should work too, it was my hurry read that made me point to addItems - and than i saw the error you have... LE: error again - it also has insertItem_s_ (that should work) – Zlatomir Jun 11 '13 at 13:46
  • 1
    You correctly said _insertItems_ in problem description, but in code you use _insertItem_ (without s at the end) see documentation here: http://qt-project.org/doc/qt-4.8/qcombobox.html#insertItems – Zlatomir Jun 11 '13 at 13:48
  • thank you .. I did not notice the missing "s" at all. that is the reason why it was not working. – hashDefine Jun 11 '13 at 13:59
  • @Zlatomir I intentionally formatted code without creating additional variable to show how to fill QComboBox "in one line of code". Sure, final decision about code formatting is up to you. – rutsky Dec 24 '14 at 14:34
  • @rutsky thank you for the edit suggestion, but i think it's clearer to separate the QStringList add items, from the QComboBox add items. – Zlatomir Dec 24 '14 at 17:26
2

don't pass the sting list as pointer

ui->QComboBox->insertItem(0, sequence_len);
WoJo
  • 360
  • 2
  • 10
2

Try this:

//Text that you want to QStringList
QStringList list;
list << "a" << "b" << "c";

//Instance of model type to QStringList
QStringListModel *model = new QStringListModel();
model->setStringList(list);

ui->QComboBox->setModel(model);

In this case, QStringList list can be your list in sequence_len.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Sergio Cabral
  • 6,490
  • 2
  • 35
  • 37