另一种方式,使用QMap:
在组合框中声明并填充QMap<QString, QSomeObject::SomeEnum>
你想要的枚举值,然后用 QMap 键的 QStringList 填充你的 QComboBox。
最终通过QMap的value()方法结合QComboBox的currentText()方法得到用户选择的枚举值。
QSerialPort类和QSerialPort::FlowControl枚举的示例:
QMap<QString, QSerialPort::FlowControl> *flowControlOptions = new QMap<QString, QSerialPort::FlowControl>;
flowControlOptions->insert("None",QSerialPort::NoFlowControl);
flowControlOptions->insert("Software",QSerialPort::SoftwareControl);
flowControlOptions->insert("Hardware",QSerialPort::HardwareControl);
QComboBox *flowControl = new QComboBox;
flowControl->addItems(QStringList(flowControlOptions->keys()));
flowControl->setCurrentIndex(2);
QSerialPort *sPort = new QSerialPort;
// Some code after... The user has selected an option
sPort->setFlowControl(flowControlOptions->value(flowControl->currentText()));