QComboBox 中的 QVariant(Python 2 与 Python 3) [英] QVariant in QComboBox (Python 2 vs Python 3)
问题描述
我有一些代码(见下文)在 Python 2 下按预期工作,但是当我在 Python 3 下执行它时,它引发了异常:
I have some code (see below) that works as expected under Python 2, but when I execute it under Python 3 it raises the exception:
Traceback (most recent call last):
File "./test3.py", line 23, in <module>
programsComboBox.addItem("Jan Novak",QtCore.QVariant("661107/3939"))
TypeError: PyQt4.QtCore.QVariant represents a mapped type and cannot be instantiated
这是为什么,有什么解决方法吗?
Why is this and is there any workaround?
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
import sys
def activated(i):
data=programsComboBox.itemData(i)
label.setText("Rodne cislo: "+data.toString())
app = QtGui.QApplication(sys.argv)
mainWindow = QtGui.QMainWindow()
mainWindow.setWindowTitle("QVariant")
mainWidget=QtGui.QWidget(mainWindow)
mainWindow.setCentralWidget(mainWidget)
layout=QtGui.QVBoxLayout(mainWidget)
label=QtGui.QLabel("Rodne cislo: ",mainWidget)
programsComboBox=QtGui.QComboBox(mainWidget)
programsComboBox.addItem("Jan Novak",QtCore.QVariant("661107/3939"))
programsComboBox.addItem("Jakub Dvorak",QtCore.QVariant("750802/1278"))
layout.addWidget(programsComboBox)
layout.addWidget(label)
app.connect(programsComboBox,QtCore.SIGNAL("activated (int)"),activated)
mainWindow.show()
sys.exit(app.exec_())
更新:对于这个简单的代码,在第一次 PyQt4 导入之前添加这些就足够了
Update: For this simple code it's enough to add these before the first PyQt4 import
import sip
sip.setapi('QVariant', 1)
但在实际代码中我不能这样做(在不同的地方 QVariant
需要 api2).
But in the real code I can't do that (somewhere in a different place QVariant
requires api2).
推荐答案
你有什么理由必须使用 QVariant 吗?
Is there any reason why you have to use a QVariant?
如果您将项目添加为字符串:
If you add your item as a string:
programsComboBox.addItem("Jan Novak", "661107/3939")
那么代码应该可以在 Python 2 和 3 中运行.
then the code should work in Python 2 and 3.
我自己不使用 Python 3,因此无法对此进行测试,但修改在 Python 2.7 上运行良好.
I don't use Python 3 myself so can't test this, but the modification runs fine on Python 2.7.
我很好奇,所以我在使用 Python 3.2 的 VM 中对此进行了测试.似乎 .itemData()
将返回一个字符串对象,而不是 Python 3 中的 QVariant.
I was curious, so I tested this in a VM using Python 3.2. It seems that .itemData()
will return a string object rather than a QVariant in Python 3.
除了在上述添加项目时使用字符串外,您还可以将 activated
函数修改为:
In addition to using a string when adding an item as above you could modify your activated
function to:
def activated(i):
data=programsComboBox.itemData(i)
if not isinstance(data, str):
data = data.toString()
label.setText("Rodne cislo: " +data)
以便它可以与 Python 2 和 3 一起使用.如果 .itemData()
的返回不是字符串,则该函数将使用 .toString() 转换为字符串
.
so that it would work with Python 2 and 3. If the return of .itemData()
isn't a string then the function will convert to a string using .toString()
.
我不确定这是否是正确的方法,但它对我有用.我仍然需要正确地研究整个 2 到 3 移植的事情.
I'm not sure if this is the correct way, but it works for me. I still need to look into the whole 2 to 3 porting thing properly.
这篇关于QComboBox 中的 QVariant(Python 2 与 Python 3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!