1

My development eviroment:

os: windows xp

python: python-3.1.2.msi

pyqt: PyQt-Py3.1-gpl-4.7.4-1.exe

code:

import sys    
from PyQt4 import QtCore, QtGui    
app = QtGui.QApplication(sys.argv)    
s = QtCore.QtString()    
sys.exit(app.exec_())

It always show me

in 'module'

s = QtCore.QtString()

AttributeError: 'module' object has no attribute 'QtString'

I chaged code:

import sys    
from PyQt4.QtGui import *    
from PyQt4.QtCore import *    
app = QApplication(sys.argv)    
s = QtString()    
sys.exit(app.exec_())

Then it always show me like this:

in 'module'

s = QtString()

NameError: name 'QtString' is not defined

what should i do?

Community
  • 1
  • 1
Vector.Lee
  • 33
  • 1
  • 1
  • 5

2 Answers2

6

The issue is explained here http://inputvalidation.blogspot.com/2010/10/python3-pyqt4-and-missing-qstring.html

The reason why you couldn't load QString is that it is missing from PyQt4 (maybe earlier, who knows). Since Py3k, as opposed to Py2k, supports Unicode by default, there's no need in this class.

Instead of QString, for compatibility reasons, you should use this snippet somewhere around your import's:

try:
    from PyQt4.QtCore import QString
except ImportError:
    QString = str
Pugsley
  • 1,146
  • 14
  • 14
2

Do you mean QString instead of QtString ?

(you can do help(QtCore) in the python interpreter and search for string)

Andre Holzner
  • 18,333
  • 6
  • 54
  • 63
  • To add: The rule of thumb is `Qt-` for modules and `Q-` for classes. –  Aug 18 '10 at 10:47
  • Thanks! I do help(QtCore) and find there is no QtString but QString. So i replaced QtString with QString. But it always show me: in s = QtCore.QString() AttributeError: 'module' object has no attribute 'QString' in s = QString() NameError: name 'QString' is not defined Why? – Vector.Lee Aug 19 '10 at 01:42
  • from PyQt4.QtGui import * ; from PyQt4.QtCore import * ; s = QString() seems to work for me. Make sure you're running this on a 'fresh' python interpreter instance. – Andre Holzner Aug 19 '10 at 05:42
  • python: python-3.1.2.msi pyqt: PyQt-Py3.1-gpl-4.7.4-1.exe I got from a website. what`s your version? – Vector.Lee Aug 20 '10 at 03:02
  • good point: my Python version is 2.6.5 on Linux (I got the impression that my distribution not have a working package for PyQt4 under python 3) – Andre Holzner Aug 20 '10 at 07:57