PyQt或PySide -使用哪一个

时间:2021-12-01 23:44:29

I started learning a bit of python and would now like to toy around a bit with gui-building. Qt seems to be a good choice because of its cross-platformishness.
Now there seem to be two bindings available: PyQt by Riverbank Computing and PySide, originally developed by Nokia.
So which one should I choose? All I can find are two year old feature comparisons, but what differences are there nowadays?
Which one is easier to use, has more/better documentation? Are both still in active development?
Licensing isn't of much concern to me since I don't intend to write commercial applications.

我开始学习一些python语言,现在喜欢用gui构建一些东西。Qt似乎是一个不错的选择,因为它具有跨平台性。现在似乎有两种绑定:Riverbank Computing的PyQt和诺基亚最初开发的PySide。那么我该选哪个呢?我能找到的只是两年前的特性比较,但是现在有什么不同呢?哪个更容易使用,有更多/更好的文档?都在积极发展吗?许可对我来说并不重要,因为我不打算编写商业应用程序。

6 个解决方案

#1


24  

Both toolkits are actively maintained, and by now more or less equal in features and quality. There are only few, rather unimportant differences.

这两种工具包都得到了积极的维护,到目前为止,它们在特性和质量上大致相当。只有很少的,相当不重要的区别。

Still, I'd recommend PySide for Python 2. It has a more reasonable API, mainly it doesn't expose Qt types, which have a direct equivalent in Python (e.g. QString, QList, etc.) or which are completely superfluous due to Python's dynamic nature, like QVariant. This avoids many tedious conversions to and from Qt types, and thus eases programming and avoids many errors.

不过,我还是建议PySide使用Python 2。它有一个更合理的API,主要是它不公开Qt类型,这些类型在Python中有直接的等价关系(例如QString、QList等),或者由于Python的动态特性(如QVariant)而完全是多余的。这避免了许多对Qt类型的繁琐转换,从而简化了编程并避免了许多错误。

PyQt also supports this modern API, and uses it by default for Python 3, but not for Python 2 to maintain backwards compatibility.

PyQt还支持这个现代API,并在Python 3的默认情况下使用它,但不支持Python 2来维护向后兼容性。

#2


22  

There is also the licensing difference. PySide is LGPL while PyQt is GPL. This could make a difference if you don't wish to make your project opensource. Although PyQt always has the propriety version available for a fairly reasonable price.

还有许可方面的差异。PySide是LGPL, PyQt是GPL。如果您不希望使您的项目成为opensource,那么这可能会有所不同。尽管PyQt总是有合适的版本,并且价格合理。

I tend to find the PySide documentation more intuitive. The API, in my opinion is slightly more Pythonic and the rate of bug fixes is quite impressive at the moment.

我倾向于发现PySide文档更直观。在我看来,这个API稍微有点python化,bug修复的速度在当前非常令人印象深刻。

PyQt has the advantage of Python 3 support and incumbency. There is a lot more 3rd party documentation/tutorials for it.

PyQt具有Python 3支持和代理的优点。它有更多的第三方文档/教程。

#3


16  

I recently ported a significant code base (over 8,000 lines of code) from PyQt to PySide.

我最近将一个重要的代码库(超过8000行代码)从PyQt移植到PySide。

Right now I'd say PyQt is a much more mature, performant and stable project. I hit a number of bugs in PySide, and suspect that any big project will hit issues. Having said that, I reported a bug to the project and it was fixed and in a new release within a few weeks. I'm also having a problem where the app takes about 15 seconds to quit. I've not yet spent the time to find out why. However it's only a matter of time before there will be no reasons for choosing PyQt over PySide.

现在我要说PyQt是一个更成熟、性能更好、更稳定的项目。我在PySide中发现了一些bug,并怀疑任何大型项目都会遇到问题。话虽如此,我还是向项目报告了一个bug,并在几周内修复并发布了一个新的版本。我还遇到了一个问题,这个应用程序需要15秒才能退出。我还没有花时间找出原因。然而,选择PyQt而不是PySide只是时间问题。

If you do decide to go with PyQt for now, make sure you use API v2 throughout. It is a better API, and will ease any future transition to PySide. Also if you do port, just follow the guidelines on the PySide wiki. Even for an 8+ kloc app consisting of about 20 source files it just took an afternoon.

如果您决定暂时使用PyQt,请确保始终使用API v2。它是一个更好的API,并将缓解任何未来向PySide的过渡。另外,如果您使用端口,请遵循PySide wiki上的指导原则。即使是一个包含大约20个源文件的8+ kloc应用程序,也只花了一个下午的时间。

#4


9  

An important fact is that PyQt4 has two versions of its APIs for some things. Version 1 items are such things as using QString instead of unicode, and QVariant (basically just a wrapper, I believe - I've never actually done anything which uses it) instead the wrapped. Version 2, which can be enabled in Python 2 and is enabled in Python 3, is much better (though still unpythonic in many places - PySide is too, but it's getting distinctly better. There are still some remaining incompatibilities with them; PyQt4 has QtCore.pyqt(Signal|Slot|Property), PySide has QtCore.(Signal|Slot|Property).

一个重要的事实是PyQt4有两个版本的api。版本1的项目是使用QString而不是unicode,以及QVariant(基本上是一个包装,我相信——实际上我从未使用过它)而不是包装的。版本2可以在Python 2中启用,并且在python3中启用了,它更好(尽管在许多地方仍然是unpython的——PySide也是如此,但是它明显变得更好了)。仍然有一些不兼容的地方;PyQt4有QtCore.pyqt(信号|槽|属性),PySide有QtCore(信号|槽|属性)。

For a project of my own, I decided that I wanted to support both with no changes to the code. I prefer PySide, but on Windows I distribute with PyQt4 as at present it's quite a bit smaller for distribution at present. My solution is to check for PySide and if it's there insert an import hook to redirect PyQt4 imports to PySide, or if it's not, fix up PyQt4 to work like it should.

对于我自己的一个项目,我决定在不修改代码的情况下支持这两个项目。我更喜欢PySide,但是在我用PyQt4发布的Windows上,它现在要小一些。我的解决方案是检查PySide,如果它在那里插入一个导入钩子,将PyQt4导入到PySide,或者如果不是,将PyQt4设置为应该这样工作。

The files used:

使用的文件:

  • pyqt4pysideimporter.py
  • pyqt4pysideimporter.py
  • zip_imp.py (for py2exe support)
  • zip_imp。py(py2exe支持)
  • make_gui.py (my script for building .ui files and .qrc files with pyside or pyqt4 tools, and fixing the imports to be consistent; polls for file changes and rebuilds the changed - nothing high tech like inotify)
  • make_gui。py(我用pyside或pyqt4工具构建.ui文件和.qrc文件的脚本,并将导入固定为一致;对文件更改和重新构建更改的轮询——没有什么比inotify更高科技了)

Then you just import pyqt4pysideimporter and pyqt4pysideimporter.autoselect() (as in main.py in that repository). And after that you can just import PyQt4.

然后您只需导入pyqt4pysidesideimporter和pyqt4pysideimporter.autoselect()(如main)。py存储库)。然后导入PyQt4。

Aside: it was also stated a few days ago on the PySide mailing list that they are planning on supporting Python 3 fully within the next few months.

旁白:几天前,在PySide邮件列表中也有声明,他们计划在未来几个月内全面支持Python 3。

#5


8  

Although they might have similar interface for Qt/C++ classes, their interface for Qt/C++ macros such as signal/slot/property are very different. Porting one to another is not an easy job. It would be better to make the right decision at the very beginning.

虽然它们可能在Qt/ c++类中有类似的接口,但是它们在Qt/ c++宏(如信号/插槽/属性)中的接口是非常不同的。相互移植不是一件容易的事。最好一开始就做出正确的决定。

Beyond the grammar/license differences, I just want to point out some deficiency of PyQt in language binding, which might be essential to write QML project in Python. These differences finfally push me to PySide from PyQt.

除了语法/许可差异之外,我只想指出PyQt在语言绑定方面的一些不足之处,这对于用Python编写QML项目可能是必要的。这些差异最终把我从PyQt推到了PySide。

  • qmlRegisterType

    qmlRegisterType

    qmlRegisterType is essential to create runtime C++ binding with QML. In PySide, it is part of PySide.QtDeclarative. And this works pretty well with Python.

    qmlRegisterType对于使用QML创建运行时c++绑定是必不可少的。在PySide中,它是PySide. qt陈述句的一部分。这在Python中很有效。

    In PyQt, qmlRegisterType does not exist. And I could not find an alternative approach. I know some simple task could be done by setting QML context. But if you really need runtime binding with qmlRegister and Q_INVOKABLE, I think PySide is the only choice at the moment.

    在PyQt中,qmlRegisterType不存在。我找不到另一种方法。我知道一些简单的任务可以通过设置QML上下文来完成。但是如果您确实需要使用qmlRegister和Q_INVOKABLE进行运行时绑定,那么我认为PySide是目前唯一的选择。

  • Shiboken VS SIP

    Shiboken VS SIP

    Both can wrap Qt/C++ into python plugin. For Shiboken, I feel it is simpler and requires less coding. Just create a typesystem xml including the NAME of classes you want to export, and that's all. Shiboken does not require extra manual descriptions for the structure of the target classes.

    两者都可以将Qt/ c++打包到python插件中。对于Shiboken,我觉得它更简单,需要更少的编码。只需要创建一个typesystem xml,其中包含要导出的类的名称,仅此而已。Shiboken不需要对目标类的结构进行额外的手工描述。

    For SIP, it would require more extra coding. We will have to create a SIP file that almost reimplements everything the C++ header. It requires not only the NAME of the class, but also the DETAILS of what methods the target classes have. If the C++ class is in good design using Pimp and we want to export all methods within it, SIP should provide a way of automatically export all class methods, which it cannot at the moment. This would also add the burden for maintaining the consistency between the SIP and the C++ headers.

    对于SIP,它将需要更多的额外编码。我们必须创建一个SIP文件,该文件几乎重新实现c++头文件的所有内容。它不仅需要类的名称,还需要目标类拥有哪些方法的详细信息。如果c++类使用Pimp设计良好,并且我们想要导出其中的所有方法,那么SIP应该提供一种自动导出所有类方法的方法,而这是它目前无法实现的。这也会增加维护SIP和c++头之间一致性的负担。

    But I have to say that the documentation for Shiboken on Qt wiki is very bad and misleading. Creating Python plugin with Shiboken on Windows do not necessarily require CMake at all. generatorrunner is also not required. I only use a windows cmd script to invoke shiboken, and a qmake pro for compiling the target plugin.

    但我不得不说,Qt wiki上的Shiboken文档非常糟糕,而且具有误导性。在Windows上使用Shiboken创建Python插件并不一定需要CMake。generatorrunner也不是必需的。我只使用windows cmd脚本调用shiboken,使用qmake pro编译目标插件。

#6


4  

I have a 20k line Python app that I unsuccessfully tried to convert to PySide. Conversion is easy and most of the functionality works. There are several methods that are not implemented because they are 'deprecated', so I had to fix those. That was OK. On Windows, using PySide-1.1.2, the '==' operator is not implemented for many Qt objects. One workaround is to say: "if id(item1) == id(item2):". The other observation is that PySide seemed noticeably slower. I did not isolate PySide as the cause of the slowness, but the problem went away when I reverted back to PyQt.

我有一个20k行Python应用程序,我试图将它转换为PySide,但没有成功。转换很容易,而且大多数功能都可以工作。有几种方法由于“弃用”而没有实现,所以我必须修复它们。这是好的。在Windows上,使用PySide-1.1.2,对许多Qt对象不实现'= '操作符。一种变通方法是:“if id(item1) = id(item2):”。另一个观察是,PySide似乎明显地慢了一些。我并没有将PySide隔离为慢化的原因,但是当我回到PyQt时,问题就消失了。

Lastly, as of now, the Android kit with PySide does not seem ready for prime time.

最后,到目前为止,带有PySide的Android套件似乎还没有为黄金时段做好准备。

#1


24  

Both toolkits are actively maintained, and by now more or less equal in features and quality. There are only few, rather unimportant differences.

这两种工具包都得到了积极的维护,到目前为止,它们在特性和质量上大致相当。只有很少的,相当不重要的区别。

Still, I'd recommend PySide for Python 2. It has a more reasonable API, mainly it doesn't expose Qt types, which have a direct equivalent in Python (e.g. QString, QList, etc.) or which are completely superfluous due to Python's dynamic nature, like QVariant. This avoids many tedious conversions to and from Qt types, and thus eases programming and avoids many errors.

不过,我还是建议PySide使用Python 2。它有一个更合理的API,主要是它不公开Qt类型,这些类型在Python中有直接的等价关系(例如QString、QList等),或者由于Python的动态特性(如QVariant)而完全是多余的。这避免了许多对Qt类型的繁琐转换,从而简化了编程并避免了许多错误。

PyQt also supports this modern API, and uses it by default for Python 3, but not for Python 2 to maintain backwards compatibility.

PyQt还支持这个现代API,并在Python 3的默认情况下使用它,但不支持Python 2来维护向后兼容性。

#2


22  

There is also the licensing difference. PySide is LGPL while PyQt is GPL. This could make a difference if you don't wish to make your project opensource. Although PyQt always has the propriety version available for a fairly reasonable price.

还有许可方面的差异。PySide是LGPL, PyQt是GPL。如果您不希望使您的项目成为opensource,那么这可能会有所不同。尽管PyQt总是有合适的版本,并且价格合理。

I tend to find the PySide documentation more intuitive. The API, in my opinion is slightly more Pythonic and the rate of bug fixes is quite impressive at the moment.

我倾向于发现PySide文档更直观。在我看来,这个API稍微有点python化,bug修复的速度在当前非常令人印象深刻。

PyQt has the advantage of Python 3 support and incumbency. There is a lot more 3rd party documentation/tutorials for it.

PyQt具有Python 3支持和代理的优点。它有更多的第三方文档/教程。

#3


16  

I recently ported a significant code base (over 8,000 lines of code) from PyQt to PySide.

我最近将一个重要的代码库(超过8000行代码)从PyQt移植到PySide。

Right now I'd say PyQt is a much more mature, performant and stable project. I hit a number of bugs in PySide, and suspect that any big project will hit issues. Having said that, I reported a bug to the project and it was fixed and in a new release within a few weeks. I'm also having a problem where the app takes about 15 seconds to quit. I've not yet spent the time to find out why. However it's only a matter of time before there will be no reasons for choosing PyQt over PySide.

现在我要说PyQt是一个更成熟、性能更好、更稳定的项目。我在PySide中发现了一些bug,并怀疑任何大型项目都会遇到问题。话虽如此,我还是向项目报告了一个bug,并在几周内修复并发布了一个新的版本。我还遇到了一个问题,这个应用程序需要15秒才能退出。我还没有花时间找出原因。然而,选择PyQt而不是PySide只是时间问题。

If you do decide to go with PyQt for now, make sure you use API v2 throughout. It is a better API, and will ease any future transition to PySide. Also if you do port, just follow the guidelines on the PySide wiki. Even for an 8+ kloc app consisting of about 20 source files it just took an afternoon.

如果您决定暂时使用PyQt,请确保始终使用API v2。它是一个更好的API,并将缓解任何未来向PySide的过渡。另外,如果您使用端口,请遵循PySide wiki上的指导原则。即使是一个包含大约20个源文件的8+ kloc应用程序,也只花了一个下午的时间。

#4


9  

An important fact is that PyQt4 has two versions of its APIs for some things. Version 1 items are such things as using QString instead of unicode, and QVariant (basically just a wrapper, I believe - I've never actually done anything which uses it) instead the wrapped. Version 2, which can be enabled in Python 2 and is enabled in Python 3, is much better (though still unpythonic in many places - PySide is too, but it's getting distinctly better. There are still some remaining incompatibilities with them; PyQt4 has QtCore.pyqt(Signal|Slot|Property), PySide has QtCore.(Signal|Slot|Property).

一个重要的事实是PyQt4有两个版本的api。版本1的项目是使用QString而不是unicode,以及QVariant(基本上是一个包装,我相信——实际上我从未使用过它)而不是包装的。版本2可以在Python 2中启用,并且在python3中启用了,它更好(尽管在许多地方仍然是unpython的——PySide也是如此,但是它明显变得更好了)。仍然有一些不兼容的地方;PyQt4有QtCore.pyqt(信号|槽|属性),PySide有QtCore(信号|槽|属性)。

For a project of my own, I decided that I wanted to support both with no changes to the code. I prefer PySide, but on Windows I distribute with PyQt4 as at present it's quite a bit smaller for distribution at present. My solution is to check for PySide and if it's there insert an import hook to redirect PyQt4 imports to PySide, or if it's not, fix up PyQt4 to work like it should.

对于我自己的一个项目,我决定在不修改代码的情况下支持这两个项目。我更喜欢PySide,但是在我用PyQt4发布的Windows上,它现在要小一些。我的解决方案是检查PySide,如果它在那里插入一个导入钩子,将PyQt4导入到PySide,或者如果不是,将PyQt4设置为应该这样工作。

The files used:

使用的文件:

  • pyqt4pysideimporter.py
  • pyqt4pysideimporter.py
  • zip_imp.py (for py2exe support)
  • zip_imp。py(py2exe支持)
  • make_gui.py (my script for building .ui files and .qrc files with pyside or pyqt4 tools, and fixing the imports to be consistent; polls for file changes and rebuilds the changed - nothing high tech like inotify)
  • make_gui。py(我用pyside或pyqt4工具构建.ui文件和.qrc文件的脚本,并将导入固定为一致;对文件更改和重新构建更改的轮询——没有什么比inotify更高科技了)

Then you just import pyqt4pysideimporter and pyqt4pysideimporter.autoselect() (as in main.py in that repository). And after that you can just import PyQt4.

然后您只需导入pyqt4pysidesideimporter和pyqt4pysideimporter.autoselect()(如main)。py存储库)。然后导入PyQt4。

Aside: it was also stated a few days ago on the PySide mailing list that they are planning on supporting Python 3 fully within the next few months.

旁白:几天前,在PySide邮件列表中也有声明,他们计划在未来几个月内全面支持Python 3。

#5


8  

Although they might have similar interface for Qt/C++ classes, their interface for Qt/C++ macros such as signal/slot/property are very different. Porting one to another is not an easy job. It would be better to make the right decision at the very beginning.

虽然它们可能在Qt/ c++类中有类似的接口,但是它们在Qt/ c++宏(如信号/插槽/属性)中的接口是非常不同的。相互移植不是一件容易的事。最好一开始就做出正确的决定。

Beyond the grammar/license differences, I just want to point out some deficiency of PyQt in language binding, which might be essential to write QML project in Python. These differences finfally push me to PySide from PyQt.

除了语法/许可差异之外,我只想指出PyQt在语言绑定方面的一些不足之处,这对于用Python编写QML项目可能是必要的。这些差异最终把我从PyQt推到了PySide。

  • qmlRegisterType

    qmlRegisterType

    qmlRegisterType is essential to create runtime C++ binding with QML. In PySide, it is part of PySide.QtDeclarative. And this works pretty well with Python.

    qmlRegisterType对于使用QML创建运行时c++绑定是必不可少的。在PySide中,它是PySide. qt陈述句的一部分。这在Python中很有效。

    In PyQt, qmlRegisterType does not exist. And I could not find an alternative approach. I know some simple task could be done by setting QML context. But if you really need runtime binding with qmlRegister and Q_INVOKABLE, I think PySide is the only choice at the moment.

    在PyQt中,qmlRegisterType不存在。我找不到另一种方法。我知道一些简单的任务可以通过设置QML上下文来完成。但是如果您确实需要使用qmlRegister和Q_INVOKABLE进行运行时绑定,那么我认为PySide是目前唯一的选择。

  • Shiboken VS SIP

    Shiboken VS SIP

    Both can wrap Qt/C++ into python plugin. For Shiboken, I feel it is simpler and requires less coding. Just create a typesystem xml including the NAME of classes you want to export, and that's all. Shiboken does not require extra manual descriptions for the structure of the target classes.

    两者都可以将Qt/ c++打包到python插件中。对于Shiboken,我觉得它更简单,需要更少的编码。只需要创建一个typesystem xml,其中包含要导出的类的名称,仅此而已。Shiboken不需要对目标类的结构进行额外的手工描述。

    For SIP, it would require more extra coding. We will have to create a SIP file that almost reimplements everything the C++ header. It requires not only the NAME of the class, but also the DETAILS of what methods the target classes have. If the C++ class is in good design using Pimp and we want to export all methods within it, SIP should provide a way of automatically export all class methods, which it cannot at the moment. This would also add the burden for maintaining the consistency between the SIP and the C++ headers.

    对于SIP,它将需要更多的额外编码。我们必须创建一个SIP文件,该文件几乎重新实现c++头文件的所有内容。它不仅需要类的名称,还需要目标类拥有哪些方法的详细信息。如果c++类使用Pimp设计良好,并且我们想要导出其中的所有方法,那么SIP应该提供一种自动导出所有类方法的方法,而这是它目前无法实现的。这也会增加维护SIP和c++头之间一致性的负担。

    But I have to say that the documentation for Shiboken on Qt wiki is very bad and misleading. Creating Python plugin with Shiboken on Windows do not necessarily require CMake at all. generatorrunner is also not required. I only use a windows cmd script to invoke shiboken, and a qmake pro for compiling the target plugin.

    但我不得不说,Qt wiki上的Shiboken文档非常糟糕,而且具有误导性。在Windows上使用Shiboken创建Python插件并不一定需要CMake。generatorrunner也不是必需的。我只使用windows cmd脚本调用shiboken,使用qmake pro编译目标插件。

#6


4  

I have a 20k line Python app that I unsuccessfully tried to convert to PySide. Conversion is easy and most of the functionality works. There are several methods that are not implemented because they are 'deprecated', so I had to fix those. That was OK. On Windows, using PySide-1.1.2, the '==' operator is not implemented for many Qt objects. One workaround is to say: "if id(item1) == id(item2):". The other observation is that PySide seemed noticeably slower. I did not isolate PySide as the cause of the slowness, but the problem went away when I reverted back to PyQt.

我有一个20k行Python应用程序,我试图将它转换为PySide,但没有成功。转换很容易,而且大多数功能都可以工作。有几种方法由于“弃用”而没有实现,所以我必须修复它们。这是好的。在Windows上,使用PySide-1.1.2,对许多Qt对象不实现'= '操作符。一种变通方法是:“if id(item1) = id(item2):”。另一个观察是,PySide似乎明显地慢了一些。我并没有将PySide隔离为慢化的原因,但是当我回到PyQt时,问题就消失了。

Lastly, as of now, the Android kit with PySide does not seem ready for prime time.

最后,到目前为止,带有PySide的Android套件似乎还没有为黄金时段做好准备。