将Value设置为QStringList中的QComboBox
Set Value to QComboBox from QStringList
在我的Qt c++ GUI应用程序中,我有一个QDialog窗口,在那里我有一些行编辑,我通过函数调用和setText()设置显示文本。我已经将值存储在QStringList(我通过数据库查询填充的QStringList)中,并设置文本如下——
void MyDialog::setDataToForm(QStringList sl)
{
ui->nameLineEdit->setText(sl[0]);
ui->emailLineEdit->setText(sl[1]);
}
现在,我也有一个QComboBox
(GenderComboBox)。我在那里设置了三个项目-男性,女性,其他(通过QT创建布局编辑器)。在我的QStringList sl
中,这个值被保存在sl[2]
中。
如何将sl[2]
的值设置为QComboBox
??
需要设置QComboBox
的currentIndex
:
QStringList genderList;
genderList << "Male" << Female" << "Other";
ui->genderComboBox->setCurrentIndex(genderList.indexOf(sl[2]));
虽然这适用于您的示例,但我建议查看Qt文档中提供的示例(Books示例,SQL Widget Mapper示例),该示例使用模型根据SQL表自动填充小部件内容。
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- .cpp和.h文件中的模板专用化声明
- 反向给定链表中的K节点
- 正在查找文档以获得PS4平台的C++中的设备信息
- enum是C++中的宏变量还是整数变量
- 如何在QGridLayout中获取QComboBox的当前文本?
- 使用QTREEVIEW中的Qcombobox委托项目
- 如何在qdialog中使用螺纹填充Qcombobox中的数据
- 始终在QTableView的单元格中显示Qcombobox
- 当我按Enter或在其区域外单击鼠标时,如何从可编辑的Qcombobox中删除光标
- QComboBox 作为 QT 中的函数输入参数
- 从视图中获取QComboBox的信号
- 使用Qt中的QComboBox隐藏并显示lineEdit
- 更改委托中的QComboBox项目文本
- 如何将隐藏的数据发送到 Pyqt 中的 QComboBOX
- 在mac和linux的QComboBox中交替行颜色
- QCombobox中的1索引模型
- 在QTableView中用作编辑器的QComboBox中刷新选项
- 是否可以禁用QComboBox中的条目?
- 将Value设置为QStringList中的QComboBox