Python 是否与 QML(Qt-Quick)配合得很好?

2022-01-19 00:00:00 python qml qt-quick

问题描述

我是 Qt-Quick 的初学者.我不知道 Qt 是 QML 的基础.而且我也不熟悉 C++,它又是 Qt 和 QML 的主要支持语言.我知道 JS 可以在 QML 层本身做很多逻辑.但是如果我们需要与操作系统交互,那么我们必须使用一些基础语言.由于我对 Python 很熟悉,我正在计划QML-JS-Python"组合.

I am a beginner in Qt-Quick. I am not aware of Qt which is a basis for QML. And also I'm not familiar with C++ which is again main supported language for both Qt and QML. I'm aware JS can do a lot of logic in QML layer itself. But if we need interactions with OS, then we have to use some base language. As I'm comfortable with Python, I m planning for "QML-JS-Python" combination.

所以,我的问题:

  1. 用于高级应用程序和游戏开发Qt-Quick 携手做得好吗?
  2. 我的组合三重奏 (QML-JS-Python) 是否足够好?
  3. 我是否需要学习 Qt for App 开发才能从 QML 与 Python 耦合?
  4. 如果 Qust 3 是,那么我应该学习完整的 Qt 还是只学习作为 Python 和 QML 之间通信门的那几行代码?

对不起,如果我提出这些问题是愚蠢的.但我愿意接受建议和意见.

Sorry If I'm dumb in posing these Questions. But I would like to take suggestions and opinions .

此组合 QML-JS-Python 的任何限制

提前致谢


解决方案

在概念层面上,它们配合得非常好.我编写了一个 python/qml/js/opengl 程序,它很好地结合了所有内容.那是使用 Qt4.7 和 PySide.

On a conceptual level, they go together very well. I've written a python/qml/js/opengl program that combines everything fairly nicely. That was with Qt4.7 and PySide.

如果您只是使用 QML,您可以最大限度地减少您需要接触的 Qt 数量(尽管一如既往,更多的知识会让您变得更强大).本质上,一旦你有了一个 qdeclarativeview,你的 Qt 工作就可以在信号/槽处理之外完成,这在 PySide 下是一种乐趣.我建议您可以使用 Python 和 QML 快速提高工作效率,而不必过多担心 Qt 方面的问题,并根据需要选择它.

If you're just using QML, you can minimise the amount of Qt you'll need to be exposed to (though, as always, more knowledge makes you more powerful). Essentially, once you have a qdeclarativeview, your Qt work is done apart from the signal/slot handling, which is a joy under PySide. I would suggest that you can be productive quickly using Python and QML without worrying too much about the Qt side of things, picking it up as necessary.

根据经验,我建议在您自己的脑海中明确区分 Python 和 QML.我将 QML 限制在 GUI 逻辑上,它做得很好;实际上,QML 处理接口如何响应输入,然后将信号发送回主程序.它的工作方式有点像在 GUI 和程序的其余部分之间创建一个非常基本的精简接口,只发出高级逻辑的信号(而不是,例如,发送回一个点击,它会发送回一个信号,例如"打开处理",GUI 将处理如何呈现该更改).就我而言,这只是直接插入到我的 MVC 框架中,但您可以按照自己的喜好进行操作.

From experience, I suggest making the demarcation between Python and QML clear in your own mind. I restricted the QML very much to GUI logic, which is does very well; in effect the QML handles how the interface responds to inputs, and then it sends signals back to the main program. It works a bit like creating a very basic, pared down interface between the GUI and the rest of the program, only signalling high level logic (rather than, for example, sending back a click, it would send back a signal saying for example "turn on the processing", the GUI would deal with how to render that change). In my case, this just plugged straight into my MVC framework, but you can do it how you like.

尽管如此,有一个很大的警告.PySide 的开发相当停滞,目前不支持 Qt5 及其所有 QML 改进优点.关于应该如何支持它已经有各种各样的讨论,但没有多少实际代码被提交.

There is one big fat caveat though in all this. The development of PySide has rather stalled and currently doesn't support Qt5 and all its QML improvement goodness. There have been various discussions about how it should be supported, but not much actual code committed.

我相信 PyQt 支持 Qt5,但作为 GPL 或商业双重许可(PySide 是 LGPL,因此可以与封闭源代码一起使用).我没有 PyQt 的经验,除此之外,PySide 几乎是一个替代品.

I believe PyQt supports Qt5, but dual licensed as either GPL or commercial (PySide is LGPL so can be used with closed source code). I have no experience of PyQt, other than it, and PySide being approximately drop in replacements for one-another.

似乎我在谈论将其用作 MVVM.

It seems like I'm talking about using it as a MVVM.

(任何限制问题):整个 Qt 通过 PySide 和 PyQt 暴露出来.这意味着您可以像在 C 中一样在 Python 中编写扩展.我编写了一个捕获鼠标滚动事件的小部件,因此我可以从 Flickable 中窃取滚动.这是一个用 Python 创建的 QML 元素,尽管我也必须从 Python 加载它;我不太清楚如何创建一个可以从 QML 文档中加载的扩展.当然可以编写一个独立于主代码的 C 扩展,因此您仍然可以选择.

(any limitations question): The whole of Qt is exposed through PySide and PyQt. This means you can write extensions in Python as you would in C. I wrote a widget that captured mouse scroll events so I could steal the scroll from a Flickable. This was a QML element created in Python, though I also had to load it from Python; I couldn't quite work out how to create an extension that I could load from inside the QML document. It's possible of course to write a C extension which is standalone to your main code, so you still always have that option.

PySide2 现在支持 Qt5.

Edit2:截至 2021 年,Pyside 现在称为 QT For Python ,并且非常支持 QT6,包括 QT6.请务必记下许可证.

As of 2021, Pyside is now known as QT For Python , and is very much supported up to and including QT6. Make sure you keep a note of the license.

相关文章