我可以在 QComboBox 中使用字符串作为索引吗?
Can I use a string as an index in a QComboBox?
我目前正在调用 Oracle 中的存储过程来填充 PYQT 中的 QComboBox。数据显示如下:
代码:'PAY_COMP'
说明:'Payment Company'
代码:'USER_COMP'
说明:'User Company'
我想在组合框中显示描述,但想使用代码作为索引,可以吗?原因是,当用户选择 'Payment Company' 时,我想将 'PAY_COMP' 发送到后端进行更新。
或者有其他实现方法吗?
我不知道 python,但至少在 C++ 中,您可以将 QVariant
数据附加到组合框中的每个元素。 QVariant
实际上可以是任何类型,例如字符串或枚举。
填充组合框时,我会使用成员函数void QComboBox::addItem(const QString & text, const QVariant & userData = QVariant())
。然后当用户选择一个项目并且我知道组合框的当前索引时,我可以使用 QVariant QComboBox::itemData(int index, int role = Qt::UserRole) const
来获取该项目的 QVariant
,然后可以将其转换为实际的类型包含的数据有,例如使用 QString QVariant::toString() const
.
组合框还提供了一种获取特定数据项索引的方法:int QComboBox::findData(const QVariant & data, int role = Qt::UserRole, Qt::MatchFlags flags = static_cast<Qt::MatchFlags> ( Qt::MatchExactly | Qt::MatchCaseSensitive )) const
现在您只需将其转移到 python,但我认为界面将是相同的。
我目前正在调用 Oracle 中的存储过程来填充 PYQT 中的 QComboBox。数据显示如下:
代码:'PAY_COMP' 说明:'Payment Company'
代码:'USER_COMP' 说明:'User Company'
我想在组合框中显示描述,但想使用代码作为索引,可以吗?原因是,当用户选择 'Payment Company' 时,我想将 'PAY_COMP' 发送到后端进行更新。
或者有其他实现方法吗?
我不知道 python,但至少在 C++ 中,您可以将 QVariant
数据附加到组合框中的每个元素。 QVariant
实际上可以是任何类型,例如字符串或枚举。
填充组合框时,我会使用成员函数void QComboBox::addItem(const QString & text, const QVariant & userData = QVariant())
。然后当用户选择一个项目并且我知道组合框的当前索引时,我可以使用 QVariant QComboBox::itemData(int index, int role = Qt::UserRole) const
来获取该项目的 QVariant
,然后可以将其转换为实际的类型包含的数据有,例如使用 QString QVariant::toString() const
.
组合框还提供了一种获取特定数据项索引的方法:int QComboBox::findData(const QVariant & data, int role = Qt::UserRole, Qt::MatchFlags flags = static_cast<Qt::MatchFlags> ( Qt::MatchExactly | Qt::MatchCaseSensitive )) const
现在您只需将其转移到 python,但我认为界面将是相同的。