PyQT5 QComboBox - 获取组合框的值 [英] PyQT5 QComboBox - get value of combobox

查看:264
本文介绍了PyQT5 QComboBox - 获取组合框的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Qt 还是很陌生,但我正在开发一种计算器并想使用组合框来选择一个系数.我已经成功地在 pyGT 中创建了一个带有列表存储的组合框,但看起来 pyQT 是完全不同的.

I am still very new to Qt but I am developing a type of calculator and want to use a combobox to select a coefficient. I have had success creating a combobox with a liststore in pyGT but it appears pyQT is quite different.

我很难把头放在数据模型和列表模型上.本质上,我希望在组合框中显示一个名称,并将该名称的值传递给计算器方程.到目前为止,我所看到的一切都只是针对单个条目,而不是关联"条目.

I am having a hard time wrapping my head around the data models and list models. Essentially I want to have a name show up in the combobox and have the value of that name get passed to the calculator equation. Everything I have seen so far has been just for single entries and not 'associated' entries.

任何人都可以向我解释或指向一个教程来引导我完成我想要完成的任务吗?

Can anyone explain or point me to a tutorial to walk me through what I am trying to accomplish?

推荐答案

您可以使用 addItem 添加具有关联值(数据)的名称(文本):

You can use addItem to add a name (text) with an associated value (data):

    self.combo.addItem('Foo', 23)
    self.combo.addItem('Bar', 42)

一个插槽可以连接到组合的激活信号框,它将发送用户选择的项目的索引:

A slot can be connected to the activated signal of the combo box, which will send the index of the item selected by the user:

    self.combo.activated.connect(self.handleActivated)

然后您可以使用 itemTextitemData 通过 index 参数访问名称和值:

You can then use itemText and itemData to access the name and value via the index parameter:

    def handleActivated(self, index):
        print(self.combo.itemText(index))
        print(self.combo.itemData(index))

这篇关于PyQT5 QComboBox - 获取组合框的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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