Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt: c++: how to fill QComboBox using QStringList

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?

like image 739
hashDefine Avatar asked Jun 11 '13 12:06

hashDefine


Video Answer


2 Answers

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);
like image 100
Zlatomir Avatar answered Sep 19 '22 03:09

Zlatomir


don't pass the sting list as pointer

ui->QComboBox->insertItem(0, sequence_len);
like image 39
WoJo Avatar answered Sep 20 '22 03:09

WoJo