键/值 pyqt QComboBox [英] Key/Value pyqt QComboBox

查看:49
本文介绍了键/值 pyqt QComboBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 QComboBox 与元组中的键"和值"一起使用,类似于 django 模型中使用的元组.例如,对于一个人的性别,我有以下结构.

I want to use a QComboBox with the "keys" and "values" from a tuple similar to the ones used in a django models. For example I have the following structure for a person's sex.

SEX_CHOICES = (('M', 'Male'), ('F', 'Female'))

元组的第一项包含存储在数据库中的性别代码,第二项是我想在 QComboBox 中作为项显示的文本.

The first item of the tuple contains the code of the sex that is stored in the database, and the second one the text that I want to display in the QComboBox as an item.

有什么方法可以将 QComboBox 值设置为 M 并显示男性.同样,当用户选择元素男性时,我可以将所选值设为 M.

Is there a way in wich I could set the QComboBox value as M and it displays Male. An also when the user select the element Male I could get the selected value as M.

谢谢

推荐答案

使用

cb.addItem  ( text, userData )

并将 DB 密钥作为 userData 传递.如果您需要更改当前选择,请使用 cb.itemData() 获取每个项目的 DB 键并将其与您需要的进行比较.

and pass the DB key as userData. If you need to change the current selection, use cb.itemData() to get the DB key of each item and compare it to the one you need.

或者,当您在 Python 映射中添加项目时记录索引,并使用它来直接查找正确的索引.

Alternatively, record the indexes as you add items in a Python map and use this to directly look up the correct index.

为了使事情更易于使用,将 QComboBox 与 Python 类包装在一起,该类为当前 DB 键提供 setter 和 getter 并隐藏映射.

To make things more easy to use, wrap the QComboBox with a Python class that offers setters and getters for the current DB key and which hides the mapping.

这篇关于键/值 pyqt QComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆