>>> from PyQt4 import QtCore
>>> str = QtCore.QString('Hello')
AttributeError: 'module' object has no attribute 'QString'

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

是的,我已阅读 QString Class Reference

为什么我不能从文档中指定的 QtCore 导入 QString

有帮助吗?

解决方案

In [1]: from PyQt4 import QtCore
In [2]: s = QtCore.QString('foo')
In [3]: s
Out[3]: PyQt4.QtCore.QString(u'foo')

其他提示

在Python 3中,默认情况下,QString会自动映射到本机Python字符串:

  

QString类是作为映射类型实现的,它自动转换为Python字符串和从Python字符串转换。另外,None被转换为null QString。但是,null QString将转换为空的Python字符串(而不是None)。 (这是因为当它应该返回一个空的QString时,Qt经常会返回一个空的QString。)

     

QChar和QStringRef类实现为自动转换为Python字符串和从Python字符串转换的映射类型。

     

QStringList类实现为一个映射类型,它自动转换为Python字符串列表。

     

QLatin1Char,QLatin1String和QStringMatcher类未实现。

http://pyqt.sourceforge.net/Docs/PyQt4/qstring.html

来自Python3中的PyQt4 4.6+ QString不存在,你应该使用普通的Python3 unicode对象(字符串文字)。为此,您的代码可以在Python 2.x和Python 3.x中运行,您可以执行以下操作:

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

根据您的使用情况,您可能会放弃这个简单的黑客攻击。

这取决于您的导入声明。

如果你写

from PyQt4 import QtGui, QtCore

你必须用

调用QString
yourstr = QtCore.QString('foo')

我想你已写过:

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

这不是真的推荐,但你应该用:

调用String
yourstr = QString('foo')
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top