什么时候做
resos = [_ui.ComboBox.itemText(i) for i in range(_ui.ComboBox.count())]
它给我列表
[PyQt4.QtCore.QString(u'1280x720 from 1.316'),
PyQt4.QtCore.QString(u'1920x1080 from 1.316'),]
如何覆盖 ComboBox 的 itemText 方法,使其只返回 python 字符串?而不是在上面的列表理解中进行字符串转换!
最佳答案
您可以考虑导入sip
并更改用于QString
的API。将 API 版本设置为 v2 会禁用 QString
并且 PyQt 方法将返回 python unicode 字符串。
这包含在 PyQt4 Documentation 中,但简而言之:
import sip
sip.setapi('QString', 2)
from PyQt4 import QtCore
# This will raise an attribute exception because QString is only wrapped
# in version 1 of the API.
s = QtCore.QString()
由于 QtCore.QString
不存在,您将从通常返回 QString
的方法中获取 native Python 类型。
请注意,更改 QVariant
的 API 通常也很有用,尤其是在处理 PyQt 中的模型时,可以避免强制转换为 Python 类型。
关于python - 如何重写 Pyqt 组合框 ItemText 方法以便它返回默认的 python 字符串而不是 QString?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30047328/