简体   繁体   English

ImportError:无法在PyQt5中导入名称'QStringList'

[英]ImportError: cannot import name 'QStringList' in PyQt5

I am using PyQt5 but can't import QStringList. 我正在使用PyQt5但无法导入QStringList。 I know that QStringList used to be in the module QtCore in PyQt4. 我知道QStringList曾经在PyQt4的模块QtCore中。 So I try importing the class using 所以我尝试使用导入类

from PyQt5.QtCore import QStringList

but it shows this error 但它显示了这个错误

C:\Python34\python.exe C:/Users/User/PycharmProjects/FirstProject/Test.py
Traceback (most recent call last):
File "C:/Users/User/PycharmProjects/FirstProject/Test.py", line 3, in <module>
from PyQt5.QtCore import QStringList
ImportError: cannot import name 'QStringList'

I am using PyCharm and it shows in auto-completion something called QStringListModel. 我正在使用PyCharm,它在自动完成中显示一个名为QStringListModel的东西。 I was following the book "Rapid GUI Development with Python and Qt" by Mark Summerfield. 我正在阅读Mark Summerfield撰写的“使用Python和Qt进行快速GUI开发”一书。 How do I use QStringList, or anything else in PyQt5 that will do the job of QStringList? 如何在PyQt5中使用QStringList或其他任何可以完成QStringList工作的东西?

In PyQt5, there is no QString and hence no need for QStringList . 在PyQt5中,没有QString ,因此不需要QStringList

Any Qt API that would normally return a QString , will automatically return a Python string instead. 任何通常会返回QString Qt API都会自动返回Python字符串。 Similarly, any Qt APIs that would normally return a QStringList will return a Python list containing Python strings. 类似地,任何通常返回QStringList Qt API都将返回包含Python字符串的Python列表。 And the opposite also applies: any Qt API that would normally accept a QString or QStringList will accept the Python equivalents instead. 反之亦然:任何通常接受 QStringQStringList Qt API都会接受Python等价物。

This is the same as the default behaviour when using PyQt4 with Python 3 , or when explicitly setting the API to version 2 using sip.setapi . 这与使用PyQt4和Python 3时的默认行为相同,或者使用sip.setapi 将API明确设置为版本2时的默认行为。

For more details, see: Differences Between PyQt4 and PyQt5 in the PyQt5 Reference . 有关更多详细信息,请参阅: PyQt5参考中 PyQt4和PyQt5之间差异

As I couldn't find a solution to this problem, I came up with a workaround for my needs. 由于我找不到这个问题的解决方案,我想出了一个解决方案来满足我的需求。 I start my python script from a shell script and use grep to ignore the error message. 我从shell脚本启动我的python脚本并使用grep忽略错误消息。 I hope this helps others. 我希望这有助于其他人。 ~/run/r_entry.py 2>&1| grep -v FIFinderSyncExtensionHost

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM