我正在尝试使用 insertItems
函数将项目添加到 QComboBox
,如下所示:
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);
但不工作,给我以下错误信息:
error: no matching function for call to 'QComboBox::insertItem(int, QStringList*)'
实际上,当我在我的类中编写ui->QComboBox->insertItem(
以查看Qt-Creator 的建议时,选项:(int index, const QStringList & list)
似乎不存在。所以,起初,我认为这是因为我的 Qt-Creator 不支持此功能。但是,令人惊讶的是,当直接从创建 QComboBox
小部件后,Qt-Creator 中的“设计”选项卡,ui_mainwindow.h
使用了相同的功能。
为什么会发生这种情况,有没有办法将此功能也添加到我的类(class)中?
最佳答案
使用addItems或 insertItems QComboBox的成员函数。//注意在接受 QStringList 参数的函数的末尾有一个 s:它是 add/insert Items
LE:不要传递 QStringList 的地址,该函数采用对 QStringList 对象的引用,而不是指针,使用:ui->QComboBox->insertItems(0, sequence_len);//没有 & 在 sequence_len 之前
填充 QComboBox 的完整示例(考虑到 tr()
已正确设置):
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);
关于c++ - Qt: c++: 如何使用 QStringList 填充 QComboBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17043790/