我有一些代码(见下文)的Python 2下按预期工作,但是当我的Python 3下运行它,它引发异常:
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
这是为什么,是有什么解决办法?
#!/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的导入前添加这些
import sip
sip.setapi('QVariant', 1)
但在实际的代码,我不能这样做(的地方在不同的地方QVariant
需要API2)。