Qt/C++ 原型设计在Python中

发布于 2024-08-29 21:19:32 字数 612 浏览 7 评论 0 原文

我想用 Qt 编写一个 C++ 应用程序,但首先使用 Python 构建原型,然后逐渐用 C++ 替换 Python 代码。

这是正确的方法吗?我应该使用什么工具(绑定、绑定生成器、IDE)?
理想情况下,所有内容都应该在 Ubuntu 存储库中可用,这样我就不必担心不兼容或旧版本,并通过简单的 aptitude 安装 来设置所有内容。
是否有关于此过程的全面文档,或者我是否必须学习每个组件?如果是,是哪些组件?

现在我有多种选择: Qt Creator,因为很好的自动完成和 Qt 集成。
Eclipse,因为它提供对 C++ 和 Python 的支持。 Eric(还没用过) Vim

PySide 因为它与 CMake 和 Boost.Python 一起使用,所以理论上它会让替换 python 代码变得更容易。 PyQt,因为它使用更广泛(更多支持)并且可作为 Debian 软件包使用。

编辑:由于我必须将程序部署到不同的计算机上,因此 C++ 解决方案将需要 1-5 个文件(如果我静态链接该程序和一些库文件),使用 Python I必须在每个平台上构建 PyQt/PySide/SIP/任何东西,并解释如何安装 Python 和其他所有东西。

I want to write a C++ application with Qt, but build a prototype first using Python and then gradually replace the Python code with C++.

Is this the right approach, and what tools (bindings, binding generators, IDE) should I use?
Ideally, everything should be available in the Ubuntu repositories so I wouldn't have to worry about incompatible or old versions and have everything set up with a simple aptitude install.
Is there any comprehensive documentation about this process or do I have to learn every single component, and if yes, which ones?

Right now I have multiple choices to make:
Qt Creator, because of the nice auto completion and Qt integration.
Eclipse, as it offers support for both C++ and Python.
Eric (haven't used it yet)
Vim

PySide as it's working with CMake and Boost.Python, so theoretically it will make replacing python code easier.
PyQt as it's more widely used (more support) and is available as a Debian package.

Edit: As I will have to deploy the program to various computers, the C++-solution would require 1-5 files (the program and some library files if I'm linking it statically), using Python I'd have to build PyQt/PySide/SIP/whatever on every platform and explain how to install Python and everything else.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鸵鸟症 2024-09-05 21:19:32

我想用Qt编写一个C++应用程序,但首先使用Python构建原型,然后逐渐用C++替换Python代码。这是正确的方法吗?

这取决于你的目标。完成这两件事后,我建议您在可能且合理的情况下继续使用 Python。尽管需要一些纪律,但用 Python 编写非常大的应用程序是很有可能的。但是,当您发现热点和可以在 C++ 中更好处理的事情时,您当然可以将相关部分移植到 C++。

是否有关于此过程的全面文档,或者我是否必须学习每个组件?如果是,是哪些组件?

以下是我对各个部分的推荐:

编辑器/IDE:使用您熟悉的任何编辑器/IDE,但我强烈推荐一个支持重构的编辑器/IDE。如果您熟悉 Eclipse,请使用它。如果您主要想走 C++ 路线并且对任何编辑器都不太熟悉,那么使用 QtCreator 可能会更好。 Eric 是一个非常好的 Python IDE,支持重构,除非你打算做了很多C++,看看吧。更好的是,它的源代码是 PyQt 良好使用和实践的示例。

流程

快速总结:

  1. 使用 PyQt 在 Python 中编写应用程序
  2. 当确定为热点时,将解耦的 Python 类转换为 C++
  3. 使用 SIP
  4. 导入 Python 中新定义的库来代替 Python 对应项
  5. 享受速度提升

一般细节:

使用 PyQt 在 Python 中编写应用程序。请小心保持良好的关注点分离,以便当您需要将片段移植到 C++ 时,它们将与其依赖关系分开。当您最终需要将某些内容移植到 C++ 时,请使用 C++/Qt 编写它,然后使用 SIP。 SIP 有一个关于该过程的很好的参考手册,并且您可以将 PyQt 的所有内容作为一个例子。

部署

C++ - 对于许多应用程序来说,依赖关系非常简单,使用 NullSoft 的安装程序InnoSetup

Python/PyQt - PyQt 应用程序安装起来有点困难,因为它依赖于 Python 以及 Qt 库的存在。一个人在 ARSTechnica 上的这篇文章py2exe 在 Windows 上运行得很好并且应该可以正常工作。 Python 源代码附带的 IME,freeze.py 有时在确定哪些共享库是真正必要的方面存在问题,有时最终会创建一个不存在依赖项的二进制文件。 Py2app 可以在 Mac OS X 上运行

然而,但更糟糕是 PyQt/Qt 许可。如果您正在开发商业应用程序,则需要拥有商业 PyQt(和 Qt)许可证,并确保防止用户轻松修改源代码或以其他方式针对 PyQt/Qt API 编写代码 由于许可限制。因此,PyQt 作者创建了一个名为 VendorId 的工具(尽管它有Python 许可证)。 VendorId 中有一个名为 SIB< /a> 可用于创建仅依赖于 Python 解释器的可执行文件。但是,如果您打算走到这一步,您可能需要随应用程序一起安装自定义 Python。

免责声明:我根本没有使用过PySide,所以我不确定它与 PyQt 相比如何。另请注意其网站上的以下警告:

PySide 仍在开发中,尚不适合需要生产级稳定性的应用程序开发。

但是,好的一点是,他们打算至少在初始版本中“保持 API 与 PyQt 的兼容性”。因此,除了 C++ 绑定之外,您以后还可以轻松地在两者之间切换。

I want to write a C++ application with Qt, but build a prototype first using Python and then gradually replace the Python code with C++. Is this the right approach?

That depends on your goals. Having done both, I'd recommend you stay with Python wherever possible and reasonable. Although it takes a bit of discipline, it's very possible to write extremely large applications in Python. But, as you find hotspots and things that can be better handled in C++, you can certainly port relevant parts to C++.

Is there any comprehensive documentation about this process or do I have to learn every single component, and if yes, which ones?

Here's what I'd recommend for the various pieces:

EDITOR/IDE: Use any editor/IDE you're comfortable with, but I'd highly recommend one that supports refactoring. If you're comfortable with Eclipse, use it. If you want to mainly go the C++ route and you're not too familiar with any editors, you might be better off with QtCreator. Eric is an extremely good Python IDE with support for refactoring, unless you're going to be doing lots of C++, take a look at it. Even better, its source code is an example of good PyQt usage and practices.

PROCESS:

The quick summary:

  1. Write your application in Python using PyQt
  2. When identified as hotspots, convert decoupled Python classes to C++
  3. Create bindings for those classes using SIP
  4. Import the newly defined libraries in Python in place of their Python counterparts
  5. Enjoy the speed boost

General details:

Write the application in Python using PyQt. Be careful to keep a good separation of concerns so that when you need to port pieces to C++ they will be separate from their dependencies. When you finally need to port something to C++, write it in C++/Qt and then create bindings for it using SIP. SIP has a good reference manual on the process, and you have all of PyQt as an example.

DEPLOYMENT:

C++ - For many applications the dependencies are sufficiently simple that it's not too difficult to create an installer using a tool like NullSoft's Installer or InnoSetup.

Python/PyQt - PyQt applications are a bit more difficult to install because of the dependency on Python and its dependence on the presence of the Qt libraries. One person documented his efforts on this post at ARSTechnica. py2exe works pretty well on Windows and should work fine. IME, freeze.py, which comes with the Python source, sometimes has problems determining which shared libraries are truly necessary and will sometimes end up creating a binary whose dependencies aren't present. Py2app can be made to work on Mac OS X.

But worse, however, is the PyQt/Qt licensing. If you are developing a commercial application, you need to have a commercial PyQt (and Qt) license and make sure to prevent the users from easily modifying the source or otherwise writing code against the PyQt/Qt API because of licensing restrictions. Because of that, the PyQt author created a tool called VendorId (although it has a Python license). Within VendorId is a tool called SIB that can be used to create an executable which depends only on the Python interpreter. But, if you're going to go this far, you might want to install a custom Python along with your application.

DISCLAIMER: I haven't used PySide at all, so I'm not sure how it compares to PyQt. Also, note the following warning on their website:

PySide is a work in progress and is not yet suited for application development requiring production-level stability.

But, on a good note, they intend, at least for the initial release to "maintain API compatibility with PyQt." So, aside from the C++ bindings, you could easily switch between the two later.

早乙女 2024-09-05 21:19:32

如果您刚刚学习 Qt 并且想要利用 Python 提供的原型设计速度,那么我建议您使用 PyQt 制作一个示例项目。正如您所说,有一个 debian 软件包,因此您只需一个简单的 apt-get 即可制作您的第一个应用程序。

我个人使用 gVim 作为我的 Python/Qt 编辑器,但您实际上可以使用任何 Python 友好的编辑器,不会遇到太多麻烦。我喜欢 WingIDE,它们具有 Qt 自动完成功能,但一旦你使用 vim kool-aid,就很难切换。

我想说,PySidePyQt 兼容 95% 以上,并且 LPGL 许可证很好,但如果您只是尝试为您的第一个原型制作原型Qt 应用程序,那么我认为没有真正的理由使用 PySide。虽然我确实更喜欢 PySide 文档,但您也可以只使用它们并将所有库引用替换为 PyQt

根据您正在构建的应用程序的复杂性,从头开始使用 C++ 版本可能比尝试执行一堆 SIP 重构黑魔法更好。一旦您牢牢掌握了 Qt 框架,您应该能够毫不费力地在 C++ 和 Python 绑定之间切换。

If you are just learning Qt and want to leverage the speed of prototyping that Python gives you, then I would recommend you make a sample project using PyQt. As you said, there is a debian package, so you are just a simple apt-get away from making your first application.

I personally use gVim as my Python/Qt editor, but you can really use any Python-friendly editor without much trouble. I liked WingIDE and they have auto-complete for Qt but once you sip from the vim kool-aid it's hard to switch.

I would say that PySide is 95%+ compatible with PyQt and the LPGL license is nice, but if you are just trying to prototype your first Qt app, then I don't think there is a real reason to use PySide. Although, I do like the PySide docs better, you can also just use them and replace all the library references with PyQt.

Depending on the complexity of the application you are building, it might be better off to just start from scratch with a C++ version than to try to do a bunch SIP refactoring black magic. Once you have a solid grasp of the Qt framework, you should be able to switch between the C++ and Python bindings pretty effortlessly.

穿越时光隧道 2024-09-05 21:19:32

在开始编写原型代码之前,我会绘制 UI 模型。以下是一些好处:

  • 比编写原型更快,因为不涉及编程

  • 用数据快速填充小部件,例如表格和树

    >

  • 向屏幕添加描述和注释

  • 轻松将模型集成到规范文档中,而无需捕获屏幕

    p>

  • 在实现之前验证 UI 设计概念

有很多工具可以帮助您做到这一点,但如果您想要要使用 Qt,MockupUI 可能是一个不错的选择,因为它为 Windows 7,8 或 10 呈现具有本机样式的 Qt 小部件,这使得你的模型看起来更真实。

I would draw UI mockups before starting to code prototypes. Here are some benefits:

  • Quicker than coding prototypes as there is no programming involved

  • Quickly fill widgets, such as tables and trees, with data

  • Add descriptions and notes to your screens

  • Easily integrate mockups into specification documents without having to capture screens

  • Validate UI design concepts before implementing

There are a lot of tools that can help you do that, but if you are going to use Qt, MockupUI may be a good choice as it renders Qt widgets with native styles for Windows 7,8 or 10 which makes your mockup look more realistic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文