本文介绍了ImportError:无法在 PyQt5 中导入名称“QStringList"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PyQt5 但无法导入 QStringList.我知道 QStringList 曾经在 PyQt4 的模块 QtCore 中.所以我尝试使用

I am using PyQt5 but can't import QStringList. I know that QStringList used to be in the module QtCore in PyQt4. So I try importing the class using

from PyQt5.QtCore import QStringList

但它显示了这个错误

C:Python34python.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'

我正在使用 PyCharm,它会在自动完成中显示名为 QStringListModel 的东西.我正在关注 Mark Summerfield 的使用 Python 和 Qt 进行快速 GUI 开发"一书.如何使用 QStringList 或 PyQt5 中可以完成 QStringList 工作的其他任何东西?

I am using PyCharm and it shows in auto-completion something called QStringListModel. I was following the book "Rapid GUI Development with Python and Qt" by Mark Summerfield. How do I use QStringList, or anything else in PyQt5 that will do the job of QStringList?

推荐答案

在 PyQt5 中,没有 QString,因此不需要 QStringList.

In PyQt5, there is no QString and hence no need for QStringList.

任何通常会返回 QString 的 Qt API 都会自动返回 Python 字符串.类似地,任何通常会返回 QStringList 的 Qt API 都会返回包含 Python 字符串的 Python 列表.反之亦然:任何通常接受 QStringQStringList 的 Qt API 都将接受 Python 等价物.

Any Qt API that would normally return a QString, will automatically return a Python string instead. Similarly, any Qt APIs that would normally return a QStringList will return a Python list containing Python strings. And the opposite also applies: any Qt API that would normally accept a QString or QStringList will accept the Python equivalents instead.

这与使用 PyQt4 和 Python 3 时的默认行为相同,或者当显式 使用 sip.setapi 将 API 设置为版本 2.

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 和 PyQt5 的区别中的 PyQt5参考.

For more details, see: Differences Between PyQt4 and PyQt5 in the PyQt5 Reference.

这篇关于ImportError:无法在 PyQt5 中导入名称“QStringList"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 03:02
查看更多