Qt:c ++:如何使用QStringList填充QComboBox

has*_*ine 5 c++ qt qcombobox qt5

我正在尝试QComboBox使用以下insertItems功能添加项目:

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);
Run Code Online (Sandbox Code Playgroud)

但是没有用,给我以下错误信息:

error: no matching function for call to 'QComboBox::insertItem(int, QStringList*)'
Run Code Online (Sandbox Code Playgroud)

实际上,当我ui->QComboBox->insertItem(在课堂上写作查看Qt-creator的建议时,选项:(int index, const QStringList & list)似乎不存在.所以,起初,我认为这是因为我的QT创建者不支持此功能.然而,令人惊讶的是,在创建QComboBox小部件后直接从Qt-creartor中的"Design"选项卡填充QComboBox时,ui_mainwindow.h正在使用相同的函数!

为什么会发生这种情况,是否有办法将此功能添加到我的班级?

Zla*_*mir 5

使用QComboBox的addItems成员函数.

LE:不传递QStringList的地址,该函数接受对QStringList对象的引用,而不是指针,使用: ui->QComboBox->insertItems(0, sequence_len); //no & before sequence_len

填写QComboBox的完整示例(考虑到tr()正确设置):

QStringList sequence_len = QStringList() << tr("1") << tr("2") << tr("3") << tr("4") << tr("5");
ui->QComboBox->addItems(sequence_len);
Run Code Online (Sandbox Code Playgroud)