tags:

views:

913

answers:

4
str =  QtCore.QString('Hello')
AttributeError: 'module' object has no attribute 'QString'

QtCore.QString._init_(self)
AttributeError: 'module' object has no attribute 'QString'

Yes, I'm read QString Class Reference

if you can, answer with code sample. Thanks

+2  A: 
In [1]: from PyQt4 import QtCore
In [2]: s = QtCore.QString('foo')
In [3]: s
Out[3]: PyQt4.QtCore.QString(u'foo')
wRAR
Note the different import - I am surprised that your import did not give an invalid syntax error
Mark
from PyQt4 import QtCores = QtCore.QString('foo')AttributeError: 'module' object has no attribute 'QString'I have this problem in Py3.1.But in Py2.5 it's working, strange...
tyshkev
Maybe PyQt4 isn't installed properly for your Python 3.1. Or it doesn't support it.
wRAR
I believe that PyQt has not been ported to Py3.1 and Py3.1 is not backward compatible so it is expected not to work in most libs.
mandel
A: 

It depends on your import statement.

If you write

from PyQt4 import QtGui, QtCore

you must call QString with

yourstr = QtCore.QString('foo')

I think you've written this :

from PyQt4.QtGui import *
from PyQt4.QtCore import *

It's not really recommended, but you should call String with :

yourstr = QString('foo')
+1  A: 

In Python 3, QString is automatically mapped to the native Python string by default:

The QString class is implemented as a mapped type that is automatically converted to and from a Python string. In addition a None is converted to a null QString. However, a null QString is converted to an empty Python string (and not None). (This is because Qt often returns a null QString when it should probably return an empty QString.)

The QChar and QStringRef classes are implemented as mapped types that are automatically converted to and from Python strings.

The QStringList class is implemented as a mapped type that is automatically converted to and from Python lists of strings.

The QLatin1Char, QLatin1String and QStringMatcher classes are not implemented.

http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/pyqt4ref.html#qstring

markv
A: 

From PyQt4 4.6+ in Python3 QString doesn't exist and you are supposed to use ordinary Python3 unicode objects (string literals). To do this so that your code will work in both Python 2.x AND Python 3.x you can do following:

try:
    from PyQt4.QtCore import QString
except ImportError:
    # we are using Python3 so QString is not defined
    QString = type("")

Depending on your use case you might get away with this simple hack.

Stan