Python版本Qt:6.4终于发布了!

Qt for Python Release: 6.4 is finally here!

Python版本Qt:6.4终于发布了!

October 13, 2022 by Cristián Maureira-Fredes | Comments

​2022年10月13日,克里斯蒂安·毛雷拉·弗雷德斯(Cristián Maureira Fredes)|评论

It is early fall in the northern hemisphere, and with that not only do the leaves drop, but also a new Qt for Python release! 🐍 

现在是北半球的初秋,不仅树叶掉落,而且Python发布了一个新的Qt! 🐍

Perhaps you were wondering why the release was not on the same day as Qt?: It was a mixture of CI not liking our configurations, conferences happening during the release, and COVID affecting 50% of the team. Now, everything is working well, and most of us have recovered. 😊 

也许您想知道为什么发布日期与Qt不在同一天:这是CI不喜欢我们的配置、发布期间举行的会议和影响50%团队的COVID的综合原因。现在,一切都进展顺利,我们大多数人都已康复😊

If you want to head to the changelog directly, you can find it here.

​如果你想直接进入变更日志,你可以在这里找到它。

Catching up with Qt: new modules and functions 

追赶Qt:新模块和功能

A new Qt release brings new modules, and as usual, Qt for Python joins the ride. You can now use the QtPdfQtPdfWidgetsQHttpServer, and QtSpatialAudio modules not just with Qt, but with Qt for Python as well. 

一个新的Qt发行版带来了新的模块,像往常一样,Qt for Python也加入了其中。现在,您不仅可以将QtPdf、QtPdfWidgets、QHttpServer和QtSpatialAudio模块用于Qt,还可以将Qt用于Python。

We have implemented Q_ARG/Q_RETURN_ARG for QMetaObject.invokeMethod() with arguments. Also, we included the QLoggingCategory class along with the qCDebug()qCInfo()qCWarning() and qCCritical() functions.  

我们已经为带参数的QMetaObject.invokeMethod()实现了Q_ARG/Q_RETURN_ARG。此外,我们还包括QLoggingCategory类以及qCDebug()、qCInfo()、qCWarning()和qCCritical()函数。

In case you were missing the qHash() functions, we have exposed them to Python as well, along with many other functions: 

如果您缺少qHash()函数,我们还将它们与许多其他函数一起公开给Python:

  • qt_set_sequence_auto_mnemonic() 
  • glGet(Boolean|Double|Float|Integer)v() and glGet(Boolean|Double|Float|Integer)i_v()  
  • QWebEngineCookieStore.setCookieFilter() and QtWebEngineProfile.setNotificationPresenter()  
  • QQmlContext.setContextProperties() 
  • QAudioBuffer.data() and QAudioBuffer.constData() 

PyPy and Python Compatibility 

PyPy和Python兼容性

After the initial release with the PyPy 3.8 compatibility, we solved some issues to make PySide compatible with PyPy 3.9. You can get these fixes from PyPi. 

​在PyPy 3.8兼容的初始版本发布后,我们解决了一些问题,使PySide与PyPy3.9兼容。您可以从PyPi获得这些修复

As you might be aware, few years ago we introduced support for NumPy. We have now decided to include it by default, unlocking lots of specialized APIs, like a new function that we added: a NumPy overload for QSurfaceDataProxy.resetArray()

您可能知道,几年前我们引入了对NumPy的支持。我们现在决定默认包含它,解锁许多专用API,比如我们添加的一个新函数:QSurfaceDataProxy.resetArray()的NumPy重载。

In case you are as excited as we are about Python 3.11 and the upcoming releases, we are happy to tell you that PySide 6.4.0 is already compatible with the last RC version from Python 3.11, so you can try it out and let us know of any corner case we forgot to fix. 

​如果您对Python 3.11和即将发布的版本感到兴奋,我们很高兴地告诉您,PySide 6.4.0已经与Python3.11的最后一个RC版本兼容,所以您可以尝试一下,让我们知道我们忘记修复的任何角落案例

New examples 

新示例

The official Qt for Python examples provide valuable points of reference, demonstrating how to use PySide and the Qt APIs in practice, and with this release, we have added several examples. This includes:

官方的Qt for Python示例提供了有价值的参考点,演示了如何在实践中使用PySide和Qt API,在这个版本中,我们添加了几个示例。这包括:

In addition, we have added two new examples that demonstrate how use the Trio package for async support in Python in conjunction with Qt. This is only the beginning of a renewed effort to improve support for Python async features with Qt, so stay tuned for more examples demonstrating asyncio with Qt, support for awaiting Qt Signals, and more in the future! 

​此外,我们还添加了两个新示例,演示如何在Python中结合Qt使用Trio包实现异步支持。这只是改进对Qt的Python异步特性支持的新努力的开始,所以请继续关注更多演示Qt异步的示例,支持等待Qt信号,以及将来的更多示例! 

The new Enum system 

新的枚举系统

PySide is in the interesting position of being both Qt (with its C++ roots and all that entails) but also distinctly Python. Where possible, we strive to align PySide further with Python conventions and use as many Python features as we can, if they make sense. 

PySide的有趣之处在于,它既是Qt(具有C++根和所有必需的),又是Python。在可能的情况下,我们努力使PySide进一步与Python约定保持一致,并尽可能多地使用Pythons特性(如果它们有意义的话)。

One such area that we tackled now affects enums, which was implemented as bound C++ classes, but now it is updated to leverage Python's own enum mechanism

​我们现在解决的一个领域影响了枚举,它是作为绑定C++类实现的,但现在它被更新为利用Python自己的枚举机制

Although the new Python enums and the old Shiboken-based ones are compatible with each other, but there are a few differences or restrictions: 

虽然新的Python枚举和基于Shiboken的旧枚举彼此兼容,但存在一些差异或限制:

  • Python enums cannot be inherited from each other, whereas Shiboken enums can 
  • Python枚举不能相互继承,而Shiboken枚举可以
  • Python enums do not allow undefined values, Shiboken enums do 
  • Python枚举不允许未定义的值,Shiboken枚举允许
  • Python enums always need exactly one argument, Shiboken enums have a default zero value 
  • Python枚举始终只需要一个参数,Shiboken枚举具有默认零值
  • Python enums rarely inherit from int, Shiboken enums always do 
  • Python枚举很少继承int,Shiboken枚举总是这样

You can see that  some of these differences are between flags. For example, until now, you could either write 

您可以看到,这些差异中的一些是标记之间的差异。例如,到目前为止,您可以

    flags = Qt.Alignment() 

    enum = Qt.AlignmentFlag 

or use the enum shortcuts like these:

或使用如下枚举快捷方式:

    Qt.AlignLeft = Qt.AlignmentFlag.AlignLeft 

    Qt.AlignTop  = Qt.AlignmentFlag.AlignTop

These shortcuts, and flags no longer exist (officially) from 6.4.0 onwards. Instead, Python has an enum.Flags class, which is a subclass of the enum.Enum class. 

从6.4.0开始,这些快捷方式和标志不再(正式)存在。相反,Python有一个enum.Flags类,它是enum.Enum类的子类。

If the thought of updating all your code left and right makes you sweat more than an overheated pumpkin spice latte, do not fret, for salvation has a name, and it’s “forgiveness mode”. This transitory mode allows you to continue using the old constructs but translates them internally into the new ones. For example, if you write 

如果一想到要更新左右两侧的所有代码,你就会汗流浃背,而不仅仅是一杯过热的南瓜香料拿铁咖啡,别担心,因为救赎有一个名字,它就是“宽恕模式”。这种临时模式允许您继续使用旧的构造,但在内部将它们转换为新的构造。例如,如果你写

    flags = Qt.Alignment() 

    enum = Qt.AlignLeft 

    item.setForeground(QColor(Qt.green)) 

    flags_type = QPainter.RenderHints 

    flags = QPainter.RenderHints() 

    chart_view.setRenderHint(QPainter.Antialiasing) 

you get a construct that mimics the following code, which is the recommended way of using flags and enums: 

您将得到一个模拟以下代码的构造,这是使用标志和枚举的推荐方法:

    flags = Qt.AlignmentFlag(0) 

    enum = Qt.AlignmentFlag.AlignLeft 

    item.setForeground(QColor(Qt.GlobalColor.green)) 

    flags_type = QPainter.RenderHint 

    flags = QPainter.RenderHint(0) 

    chart_view.setRenderHint(QPainter.RenderHint.Antialiasing) 

With this you can at least initially ignore the difference between old and new enums, as long as the new enums are properties of classes. 

这样,只要新枚举是类的属性,您至少可以在开始时忽略旧枚举和新枚举之间的差异。

New tools 🧰

新工具🧰

We have been been requested many times to offer more tooling, which could enable new and experienced developers to achieve more with PySide. So we decided to include a couple of extra tools to reduce some of the time-consuming steps while developing applications using Qt for Python: 

我们多次被要求提供更多的工具,这将使新的和有经验的开发人员能够使用PySide实现更多的功能。因此,我们决定加入一些额外的工具,以减少使用Qt for Python开发应用程序时的一些耗时步骤:

pyside6-qml

This tool mimics the capabilities of QML runtime utility for Python and enables quick prototyping with Python modules. It also makes it easy to integrate new QML types defined in Python files, thereby enabling creating pseudo Python QML plugins.   

该工具模拟了Python的QML运行时实用程序的功能,并支持使用Pythons模块进行快速原型制作。它还可以方便地集成在Python文件中定义的新QML类型,从而支持创建伪PythonQML插件。

https://play.hubspotvideo.com/3306dce9-79d1-4644-a194-17fd8ce9ac67

pyside6-qtpy2cpp

This tool is a simple approach to convert your Python code into C++, enabling you to file issues on the Qt project when the problem is not found on the Python layer. We also thought about helping newcomers that want to convert their Python projects into C++. Please keep in mind this tool is experimental, and many issues will be found, so always double check the generated code.

这个工具是一种将Python代码转换为C++的简单方法,当在Pythons层上找不到问题时,可以将问题归档到Qt项目中。我们还考虑帮助那些希望将Python项目转换为C++的新手。请记住,此工具是实验性的,会发现许多问题,因此请务必仔细检查生成的代码。

https://play.hubspotvideo.com/3799398a-ddc3-48fe-a2c6-149d14f9cca6

pyside6-deploy

This tool is intended to help in-experienced developers with freezing and compiling tools to get an executable from their PySide project. We include a simple wrapper for Nuitka, so you can just run pyside6-deploy your_file.py to get it done. 

此工具旨在帮助经验丰富的开发人员使用冻结和编译工具,从PySide项目中获取可执行文件。我们为Nuitka提供了一个简单的包装器,因此您只需运行pyside6-deploy your_file.py来完成它。

https://play.hubspotvideo.com/f67fd074-940a-4a53-8029-c5e2abdcc17f

Other wrappers were added as well, such as pyside6-qmlformat and pyside6-qmlls to improve the QML workflow.

还添加了其他包装器,如pyside6-qmlformat和pyside6qmlls,以改进QML工作流。

Many improvements to Shiboken 🥷

Shiboken的许多改进

Considering that most of the changes related to the new Enum system happened at a CPython level, Shiboken was modified in favor of adjusting the generated code to the new implementation. But not only that, there are always new improvements that the team is rolling out, to improve the user experience for those who rely on Shiboken as a binding tool can take some benefit. 

考虑到与新Enum系统相关的大多数更改都发生在CPython级别,因此对Shiboken进行了修改,以便将生成的代码调整为新的实现。但不仅如此,团队还不断推出新的改进,以改善依赖Shiboken作为绑定工具的用户体验,这会带来一些好处。

For the people out there using modern C++, you might have noticed that so far we didn’t have support for the latest C++ operator, the spaceship comparison – starting from 6.4.0 it is possible to recognize it for binding generation purposes. 

对于使用现代C++的人们来说,您可能已经注意到,到目前为止,我们还没有支持最新的C++操作符,即太空船比较—从6.4.0开始,为了进行绑定生成,可以识别它。

Even though the typesystem looks like a simple XML file, it’s constantly evolving, adding new features, checks, etc., to extend the functionality when exposing complex structures or functions. For that a few things were added in this release: 

尽管类型系统看起来像一个简单的XML文件,但它仍在不断发展,添加了新的特性、检查等,以便在公开复杂的结构或函数时扩展功能。为此,本版本中添加了一些内容:

  • Meta type registration is now configurable in the typesystem, while using the ‘qt-register-metatype' on value-types. This can be used so the meta type registration is generated for the name option. 
  • 现在可以在类型系统中配置元类型注册,同时在值类型上使用“qt-register-metatype”。这可以用于为名称选项生成元类型注册。
  • You can use now use ‘handle’ and ‘value-handle’ as types for smart-pointer-type, to be able to use a basic handle type (operator->()) or a getter function returning a value T (like for std::optional). 
  • 您现在可以使用“handle”和“value handle“作为智能指针类型的类型,以便能够使用基本句柄类型(operator->())或返回值T的getter函数(如std::optional)。
  • A function to get the type  name of a polymorphic class can now be specified for object-types
  • 现在可以为对象类型指定获取多态类的类型名的函数。
  • It is possible now to define parent ownership heuristics for a type, so we can avoid unintended ownership transfers 
  • 现在可以为类型定义父所有权启发,因此我们可以避免意外的所有权转移

A few other things are included as well, like the exception propagation through return value ownership modifications, a way of specifying free functions with complete custom implementations, extra command-line options for compiler, path, and platform, and it’s also possible to specify a list of functions to be generated, contrary to the old approach where functions had to be skipped. 

还包括一些其他内容,例如通过修改返回值所有权来传播异常,使用完整的自定义实现指定自由函数的方法,编译器、路径和平台的额外命令行选项,还可以指定要生成的函数列表,这与必须跳过函数的旧方法不同。

And finally, there is now support for std::unique_ptr :) 

最后,现在支持std::unique_ptr:)

Internal improvements and Documentation 📚

内部改进和文档📚

We have been working on how to improve the automatic transformation of C++ snippets into Python for our documentation, covering many corner cases that existed earlier. If you see a snippet that is not well-formed,  let us know via a bug report, otherwise, we cannot catch all the issues with the current snippets. 

​我们一直在为我们的文档研究如何改进C++代码段到Python的自动转换,涵盖了以前存在的许多角落案例。如果您看到一个格式不好的代码段,请通过错误报告告诉我们,否则,我们无法用当前代码段捕获所有问题

Complementary, if for some reason you build the documentation locally, we decoupled the process a bit, so you no longer require a complete PySide build to get some API documentation properly built. 

作为补充,如果出于某种原因,您在本地构建文档,我们会稍微解耦该过程,因此您不再需要完整的PySide构建来正确构建一些API文档。

If you are using VS Code, we have updated our debugging tutorial for Windows users

​如果您使用的是VS代码,我们已经为Windows用户更新了调试教程

What else is cooking? 🧑‍🍳

还在做什么? 🧑‍🍳

We have been doing lots of research during the last months because we wanted to spend some time exploring ideas and areas where we could make Python shine. 

在过去的几个月里,我们做了大量的研究,因为我们想花一些时间探索可以让Python发光的想法和领域。

If you follow our development notes and are currently on one of our platforms, you might know what we have been playing with lately. In case you have not, the team has been working on: 

如果你关注我们的开发笔记,并且目前在我们的一个平台上,你可能知道我们最近在玩什么。如果您没有,团队一直在努力:

  • Asynchronous support, 
  • 异步支持,
  • Embedded system compatibility and wheels automation, 
  • 嵌入式系统兼容性和车轮自动化,
  • WebAssembly, and 
  • WebAssembly和
  • Android deployment 
  • Android部署

Do you have any preference for those options? Let us know! 

你对这些选项有什么偏好吗?让我们知道!

Keep in mind that we are really open to new crazy ideas, where do you want to see PySide next? Are those topics enough? We would really appreciate your feedback. 

请记住,我们真的对新的疯狂想法持开放态度,你接下来想在哪里看到PySide?这些话题够了吗?我们非常感谢您的反馈。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值