QComboBox绑定对象列表

qt QComBox绑定对象列表依靠的是QVariant

绑定数据到UI

 for (QVector<Department>::const_iterator it=department.constBegin(); it!=department.constEnd(); ++it)
        {
            ui->departmentCB->addItem(it->Name,QVariant::fromValue(it->Id));
        }

  取数据

int departmentId=ui->departmentCB->itemData(ui->departmentCB->currentIndex()).toInt();

如果有更好的方法可以在评论中评论,本人对QVariant的了解还比较显浅。

原文地址:https://www.cnblogs.com/ssvip/p/14769478.html