如何获取 QComboBox 的当前文本内容? [英] How do you get the current text contents of a QComboBox?

查看:69
本文介绍了如何获取 QComboBox 的当前文本内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 pyqt4 和 python 2.6,我使用 qcombobox 提供选项列表.我在使用所选选项时遇到问题.我已经能够在选择选项时使用信号来触发方法,但问题是当用户单击运行时,需要考虑其中几个组合框的内容.所以基本上我需要将组合框的选定内容作为字符串获取.到目前为止,我只能使用它:

Using pyqt4 and python 2.6, I am using a qcombobox to provide a list of options. I am having problems with using the selected option. I have been able to use a signal to trigger a method when the option is selected, but the problem is that when the user clicks run, the contents of several of these comboboxes need to be taken into account. So basically I need to get the selected contents of a combobox as a string. Thus far I have only been able use this:

print combobox1.currentText()

得到这个:

PyQt4.QtCore.QString(u'Test Selection2')

当我真正想要的是测试选择"位时,有什么想法吗?我的组合框是这样制作的:

when all I really want is the 'Test Selection' bit, any ideas? My combo box was made like this:

combobox1 = qt.QComboBox()
combobox1.addItems(['Test Selection1', 'Test Selection2'])
mainLayout.addWidget(combobox1, 0, 0)

推荐答案

只需使用 str 就可以将 QString 类型转换为 python 字符串功能.假设您没有使用任何 Unicode 字符,您可以获得一个 python字符串如下:

You can convert the QString type to python string by just using the str function. Assuming you are not using any Unicode characters you can get a python string as below:

text = str(combobox1.currentText())

如果您使用任何 unicode 字符,您可以这样做:

If you are using any unicode characters, you can do:

text = unicode(combobox1.currentText())

这篇关于如何获取 QComboBox 的当前文本内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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