QT QCombobox的问题,additem()函数的问题。。求解答

ui.combobox->clear();ui.combobox->addItem(tr("全部"),"(4,6,7)");ui.combobox->addItem(tr... ui.combobox->clear();
ui.combobox->addItem(tr("全部"), "(4, 6, 7)");
ui.combobox->addItem(tr("中国"), "(6, 7)");
ui.combobox->addItem(tr("美国"), "(4)");
后面的(4,6,7)不是索引啊,这是4代表美国,6和7都代表中国。可是为什么这么用呢
展开
 我来答
long210405
2014-11-20 · TA获得超过102个赞
知道小有建树答主
回答量:92
采纳率:0%
帮助的人:122万
展开全部
void QComboBox::addItem ( const QString & text, const QVariant & userData= QVariant() )
Adds an item to the combobox with the given text, and containing the specified userData (stored in the Qt::UserRole). The item is appended to the list of existing items.
所以后面都不是索引,使用户自定义的数据,具体意义看用户定义。
追问
怎么理解呢?我在combobox里添加了(美国,和4)这两个字符串,并且美国和4一一对应。那我如果读取的currenttext是美国,是不是也就读取了4这个标志。那么需要用split将其分开读取么???
追答
不是,使用currentText读取的就是“美国”,读后面的内容(4)需要用itemData ( int index, int role = Qt::UserRole )  来读,其中index是Item的索引。

QVariant QComboBox::itemData ( int index, int role = Qt::UserRole ) const
Returns the data for the given role in the given index in the combobox, or QVariant::Invalid if there is no data for this role.
意法半导体(中国)投资有限公司
2023-06-12 广告
单片机,即单片微控制器,也称为单片微型计算机,是将中央处理器(CPU)、存储器(ROM,RAM)、输入/输出接口和其他功能部件集成在一块 在一个小块的集成电路上,从而实现对整个电路或系统的数字式控制。单片机不是完成某一个逻辑功能的芯片,而是... 点击进入详情页
本回答由意法半导体(中国)投资有限公司提供
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式