I did this answer rather to show you how to solve your problem. (I've the feeling that I even didn't understand what your actual problem is.)
When asking a question the chances to get a helpful answer increase if an MCVE is provided. (Please, follow this link. It teachs you really basic skills every S/W developer shouldmust have. I would also recommend to follow-up to How to debug small programs.)
As I did understand your problem I made such an MCVE. This is the code testQComboBox
:
#include <QtWidgets>
int main(int argc, char **argv)
{
// build appl.
qDebug() << "Qt Version:" << QT_VERSION_STR;
QApplication app(argc, argv);
// a QStringList with items
QStringList items;
items
<< QString::fromUtf8("first item")
<< QString::fromUtf8("second item")
<< QString::fromUtf8("third item");
// build GUI
QDialog dlg;
QVBoxLayout vBox;
QComboBox cmbBox;
cmbBox.addItems(items);
vBox.addWidget(&cmbBox);
dlg.setLayout(&vBox);
dlg.show();
app.exec();
// done
return 0;
}
I compiled it in VS2013 with Qt 5.9.2 on Windows 10 (64 bit). This is how it looks:
As you see, the usage of combobox is rather easy – no secret trap doors to use it. The actual code which is directly related to QComboBox
is exactly 4 lines of code:
QVBoxLayout vBox;
QComboBox cmbBox;
cmbBox.addItems(items);
vBox.addWidget(&cmbBox);
And there is exactly one line of code where items are added to the QComboBox
:
cmbBox.addItems(items);
Note:
I used QComboBox::addItems()
instead of QComboBox::addItem()
as the former has already a loop built-in to add a complete QStringList
. It doesn't make any difference to the loop you used in your code Dialog::Dialog()
.
So, finally I dare to do the following statement:
If your combobox doesn't show items then:
You added items from an empty list.
Or, you forgot to add the items from list.
Or, something very weird happend.
I would always bet for 1. or 2. reason – the 3. reason is for real emergency cases only (e.g. broken Qt installation).
Concerning 3. reason:
I saw many questions where some lines of innocent looking code were presented which looked exactly as they should but were claimed to fail. And finally almost everytimes it showed that these lines worked fine when isolated in an MCVE but they didn't in the original code. How can this happen? Either there is some context which changes the behavior of the code in your original program or there is UB – undefined behavior. Something else does bad things but instead of crashing your process immediately (which would mean you're lucky) it goes on for a while corrupting the data more and more until finally everything breaks completely. Looking into the core-dump doesn't help at all. Therefore my recommendation of How to debug small programs.