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

标签 c++ qt qt5 qcombobox

我正在尝试使用 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)中?

最佳答案

使用addItemsinsertItems 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/

相关文章:

c++ - HANDLE 类似于 Linux 中的文件描述符吗?

c++ - 有没有办法从 `std::function` 返回到指针?

c++ - Windows 上的 gmp 库 - 链接错误 LNK2001 未解析的外部符号运算符 <<

没有复制构造函数的 C++ 赋值运算符

c++ - QPainter 和 QOpenGLWidget 类中的 OpenGL native 代码

c++ - 如何在 QtQuick2Con​​trolsApplicationViewer 上绑定(bind) QML/C++ 类

c++ - QList表现奇怪

c++ - QTableWidget 中的 Star Delegate

c++ - 在 Qt 中更改 show() 上的窗口位置

c++ - Qt 与 OpenCv 不执行任何操作