Qt: c++:如何使用QStringList填充QComboBox
Qt: c++: how to fill QComboBox using QStringList
我正在尝试使用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中的"设计"选项卡填充QComboBox
时,ui_mainwindow.h
正在使用相同的功能。
为什么会发生这种情况,是否有一种方法可以将此函数添加到我的类?
使用QComboBox的addItems或insertItems成员函数。//注意,对于接受QStringList参数的函数,最后有一个s:它是add/insert Items
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");
//add items:
ui->QComboBox->addItems(sequence_len);
//insert items into the position you need
//ui->QComboBox->insertItems(0, sequence_len);
不要将字符串列表作为指针传递
ui->QComboBox->insertItem(0, sequence_len);
试试这个:
//Text that you want to QStringList
QStringList list;
list << "a" << "b" << "c";
//Instance of model type to QStringList
QStringListModel *model = new QStringListModel();
model->setStringList(list);
ui->QComboBox->setModel(model);
在这种情况下,QStringList list
可以是sequence_len
中的列表。
相关文章:
- 在c++中用vector填充一个简单的动态数组
- 如何使用用户输入在C++中正确填充2D数组
- 如何找到大小'x'数组是否完全填充,在C++?
- Cuda C++:设备上的Malloc类,并用来自主机的数据填充它
- 通过for循环使用用户输入填充列表
- 根据用户输入用字母填充矢量,并将"开始"和"结束"放在四肢
- 如何正确填充在堆上分配的二维数组?
- 将数字转换为填充字符串
- 有没有办法在一行中填充矢量图
- 用C++中的数字和条件填充向量
- 用真值填充矢量
- 使用结构成员指针在C++中填充结构
- 流填充字符的默认定位
- 使用不同算法的 PKCS1v15 填充进行加密 ++ 签名
- C++:使用缓冲区中的数据填充结构
- 如何将零填充的多维数组传递给 C++ 中的函数?
- Cryptopp:获取密码输入的填充字符串
- 如何在qdialog中使用螺纹填充Qcombobox中的数据
- 鼠标按下QComboBox的插槽-如果用户单击组合框,则动态填充项目
- Qt: c++:如何使用QStringList填充QComboBox