如何在 PyQt4 中创建 QString?

发布于 2024-08-04 11:22:01 字数 447 浏览 8 评论 0原文

>>> 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 类参考

为什么我无法导入 QString 来自 QtCore,如文档中所指定?

>>> 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' 

Yes, I've read QString Class Reference

Why can't I import QString from QtCore, as specified in the docs ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

铜锣湾横着走 2024-08-11 11:22:01

在Python 3中,QString默认自动映射到原生Python字符串:

QString 类被实现为自动与 Python 字符串相互转换的映射类型。此外,None 会转换为空 QString。但是,空 QString 会转换为空 Python 字符串(而不是 None)。 (这是因为 Qt 通常应该返回空 QString,但它应该返回空 QString。)

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

QStringList 类作为映射类型实现,可以自动与 Python 字符串列表相互转换。

未实现 QLatin1Char、QLatin1String 和 QStringMatcher 类。

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

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://pyqt.sourceforge.net/Docs/PyQt4/qstring.html

笑咖 2024-08-11 11:22:01

从 PyQt4 4.6+ 开始,Python3 中的 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 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.

滥情空心 2024-08-11 11:22:01
In [1]: from PyQt4 import QtCore
In [2]: s = QtCore.QString('foo')
In [3]: s
Out[3]: PyQt4.QtCore.QString(u'foo')
In [1]: from PyQt4 import QtCore
In [2]: s = QtCore.QString('foo')
In [3]: s
Out[3]: PyQt4.QtCore.QString(u'foo')
你与清晨阳光 2024-08-11 11:22:01

这取决于您的进口声明。

如果您编写,则

from PyQt4 import QtGui, QtCore

必须使用以下方式调用 QString

yourstr = QtCore.QString('foo')

我认为您已经编写了以下内容:

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

这并不是真正推荐的,但您应该使用以下方式调用 String:

yourstr = QString('foo')

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')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文