简体   繁体   English

Python中的Qt / C ++原型

[英]Prototyping Qt/C++ in 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++. 我想用Qt编写一个C ++应用程序,但是首先使用Python构建一个原型,然后逐渐用C ++替换Python代码。

Is this the right approach, and what tools (bindings, binding generators, IDE) should I use? 这是正确的方法,我应该使用哪些工具(绑定,绑定生成器,IDE)?
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 . 理想情况下,所有内容都应该在Ubuntu存储库中可用,因此我不必担心不兼容或旧版本,并且使用简单的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. 现在我有多种选择:Qt Creator,因为很好的自动完成和Qt集成。
Eclipse, as it offers support for both C++ and Python. Eclipse,因为它支持C ++和Python。 Eric (haven't used it yet) Vim 埃里克(还没用过)Vim

PySide as it's working with CMake and Boost.Python, so theoretically it will make replacing python code easier. PySide因为它正在使用CMake和Boost.Python,所以从理论上讲它会更容易替换python代码。 PyQt as it's more widely used (more support) and is available as a Debian package. PyQt因为它被更广泛地使用(更多支持)并且可以作为Debian包使用。

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. 编辑 :因为我必须将程序部署到各种计算机,C ++ - 解决方案将需要1-5个文件(程序和一些库文件,如果我静态链接它),使用Python我必须构建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++. 我想用Qt编写一个C ++应用程序,但是首先使用Python构建一个原型,然后逐渐用C ++替换Python代码。 Is this the right approach? 这是正确的方法吗?

That depends on your goals. 这取决于你的目标。 Having done both, I'd recommend you stay with Python wherever possible and reasonable. 完成这两项工作后,我建议您尽可能合理地使用Python。 Although it takes a bit of discipline, it's very possible to write extremely large applications in Python. 虽然它需要一些纪律,但是用Python编写非常大的应用程序是非常可能的。 But, as you find hotspots and things that can be better handled in C++, you can certainly port relevant parts to C++. 但是,当您发现可以在C ++中更好地处理的热点和事物时,您当然可以将相关部分移植到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. EDITOR / IDE :使用您熟悉的任何编辑器/ IDE,但我强烈推荐一种支持重构的编辑器/ IDE。 If you're comfortable with Eclipse, use it. 如果您对Eclipse感到满意,请使用它。 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. 如果你想主要使用C ++路线并且你对任何编辑都不太熟悉,那么使用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. Eric是一个非常好的Python IDE,支持重构,除非你要做很多C ++,看看它。 Even better, its source code is an example of good PyQt usage and practices. 更好的是,它的源代码是一个很好的PyQt使用和实践的例子。

PROCESS : 过程

The quick summary: 快速摘要:

  1. Write your application in Python using PyQt 使用PyQt在Python中编写应用程序
  2. When identified as hotspots, convert decoupled Python classes to C++ 当被识别为热点时,将解耦的Python类转换为C ++
  3. Create bindings for those classes using SIP 使用SIP为这些类创建绑定
  4. Import the newly defined libraries in Python in place of their Python counterparts 用Python导入新定义的库代替Python对应的库
  5. Enjoy the speed boost 享受速度提升

General details: 一般细节:

Write the application in Python using PyQt. 使用PyQt在Python中编写应用程序。 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. 小心保持关注点的良好分离,这样当您需要将片段移植到C ++时,它们将与其依赖项分离。 When you finally need to port something to C++, write it in C++/Qt and then create bindings for it using SIP . 当你最终需要将某些东西移植到C ++时,用C ++ / Qt编写它,然后使用SIP为它创建绑定。 SIP has a good reference manual on the process, and you have all of PyQt as an example. SIP有关于该过程的一本很好的参考手册 ,你以PyQt为例。

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 . C ++ - 对于许多应用程序,依赖关系非常简单,使用NullSoft的InstallerInnoSetup等工具创建安装程序并不困难。

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. Python / PyQt - PyQt应用程序安装起来有点困难,因为它依赖于Python并且依赖于Qt库的存在。 One person documented his efforts on this post at ARSTechnica . 一个人记录了他在ARSTechnica的这篇文章所做的努力。 py2exe works pretty well on Windows and should work fine. py2exe在Windows上工作得很好,应该可以正常工作。 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. 与Python源一起提供的IME, freeze.py ,有时会确定哪些共享库是真正必要的,有时最终会创建一个不存在依赖关系的二进制文件。 Py2app can be made to work on Mac OS X . Py2app可以在Mac OS X上运行

But worse , however, is the PyQt/Qt licensing. 但更糟糕的是PyQt / Qt许可。 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 . 如果您正在开发商业应用程序,则需要拥有商业PyQt(和Qt)许可证,并确保防止用户因许可限制而轻易修改源代码或以其他方式针对PyQt / Qt API编写代码。 Because of that, the PyQt author created a tool called VendorId (although it has a Python license). 因此,PyQt作者创建了一个名为VendorId的工具(尽管它有Python许可证)。 Within VendorId is a tool called SIB that can be used to create an executable which depends only on the Python interpreter. 在VendorId中是一个名为SIB的工具,可用于创建仅依赖于Python解释器的可执行文件。 But, if you're going to go this far, you might want to install a custom Python along with your application. 但是,如果你打算走这么远,你可能想要在你的应用程序中安装自定义Python。

DISCLAIMER : I haven't used PySide at all, so I'm not sure how it compares to PyQt. 免责声明 :我根本没有使用过PySide ,所以我不确定它与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. PySide正在进行中,尚不适合需要生产级稳定性的应用程序开发。

But, on a good note, they intend, at least for the initial release to "maintain API compatibility with PyQt." 但是,好的一点是,他们打算,至少在初始版本中“保持与PyQt的API兼容性”。 So, aside from the C++ bindings, you could easily switch between the two later. 因此,除了C ++绑定之外,您可以稍后在两者之间轻松切换。

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 . 如果您只是在学习Qt并希望利用Python为您提供的原型设计速度,那么我建议您使用PyQt创建一个示例项目。 As you said, there is a debian package, so you are just a simple apt-get away from making your first application. 正如你所说,有一个debian包,所以你只是一个简单的方法apt-get远离你的第一个应用程序。

I personally use gVim as my Python/Qt editor, but you can really use any Python-friendly editor without much trouble. 我个人使用gVim作为我的Python / Qt编辑器,但你可以毫不费力地使用任何Python友好的编辑器。 I liked WingIDE and they have auto-complete for Qt but once you sip from the vim kool-aid it's hard to switch. 我喜欢WingIDE并且他们已经为Qt自动完成了,但是一旦你从vim kool-aid啜饮它就很难切换。

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 . 我会说PySidePyQt兼容95%并且LPGL许可证很好,但是如果你只想尝试原型化你的第一个Qt应用程序,那么我认为没有真正的理由使用PySide Although, I do like the PySide docs better, you can also just use them and replace all the library references with PyQt . 虽然,我更喜欢PySide文档,但您也可以使用它们并用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. 根据您正在构建的应用程序的复杂性,最好从头开始使用C ++版本,而不是尝试做一堆SIP重构黑魔法。 Once you have a solid grasp of the Qt framework, you should be able to switch between the C++ and Python bindings pretty effortlessly. 一旦掌握了Qt框架,您就可以非常轻松地在C ++和Python绑定之间切换。

I would draw UI mockups before starting to code prototypes. 在开始编写原型代码之前,我会绘制UI模型。 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 在实现之前验证UI设计概念

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. 有很多工具可以帮助你做到这一点,但如果你打算使用Qt, MockupUI可能是一个不错的选择,因为它为Windows 7,8或10渲染Qt小部件的原生样式,这使你的模型看起来更真实。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM