21

I want to develop in Qt, and I already know Python. I am learning C++, so what are the advantages of programming Qt in C++ over Python? C++ seems more complicated, and seems like there is not much gain.

Orcris
  • 3,135
  • 6
  • 24
  • 24

4 Answers4

24

What is the advantage of using the native C++ Qt over PyQt

Speed/power/control.

PyQt application will still require python. C++/Qt Application compiles to native exe. By using C++ you'll get access to 3rd party libraries that won't be available in python, plus you'll exterminate "middle man" - layer that sits between your program and qt dlls and potentially you can get better performance. For example, I would not write an archiver or mp3 decompressor in python, although it certainly can be done.

However that comes at a cost - c++ does not have a garbage collector, is much more complex, has "slower" development (compilation time), requires years to master and you'll get better performance only if your bottleneck is in within interpreter (i.e. scripted language overhead). I.e. C++ gives more power at a cost of greater responsibility and longer development time. If you don't need that, then you don't have a reason to stick with C++.

Choice of language depends on your application/situation and your personal preferences. If you need to make application SOON or make a mockup, then it'll be reasonable to use language you're familiar with. If you have serious performance problems, then it'll be reasonable to hire skilled C++ programmer to do the job - make native application, profile it, optimize, etc.

Please note that language is a tool. If you want to use your language for everything simply because you like the language, you're not working efficiently.

--EDIT--

Personally, I would not use python for a larger application I'm expected to maintain for a long time. However, this is because the language is not exactly compatible with my mindset (reliance on Murphy's Law) and (as a result) I'm not comfortable with it. Person that thinks differently will be probably much more comfortable with Python and might even think that C++ is too restrictive.

Another thing is that judging from my experience of writing Blender plugins and various python scripts, there's some serious performance overheads that appears because language is scripted, and very heavy list/map/array manipulation that can be performed FAST for free in C++ might take 5x..10x times longer in python. Some people might insist that this can be fixed, however, cost of this "fixing" might overcome benefits you get from using scripted language. Regardless of my preference, I still use Python for making utility scripts that need to run several utilities, split/splice/parse their text output and do something with it (C++ isn't very good at this situations), and I'd still provide Python bindings (assuming Lua is no good) in a program that must be extensible.

In the end it comes down to selection of most suitable tool - if C++ will not give you any benefit compared to Python, then there's no reason to switch.

SigTerm
  • 26,089
  • 6
  • 66
  • 115
  • 6
    Good answer, although I would like to point out, the amount of available modules for python is vast and the argument of having larger freedom with C++ is quite debatable. For anything performance intensive go C++, the rest would probably fit better with python IMHO. – enticedwanderer Apr 12 '12 at 22:35
  • Nice answer. I´m learning QT, and I have made a pair of simple applications to see how it works. I'm really impressed with the performance and the GUI Editor. The documentation, is easy to understand and you can get a lot of information from the web. For the electronics development (my case), I found very easy to manage serial ports, and other peripherals using QT. – GTRONICK Sep 15 '16 at 18:54
8

If you're planning on distributing your app, it's much easier to deliver a self-contained compiled executable than relying on your end users to install Python and PyQt first. But that may or may not be a consideration for you.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • I would probably compile executables with something like py2exe. – Orcris Apr 12 '12 at 22:03
  • Wow, have you tried that for PyQt? (I haven't.) The resulting executable are going to be pretty huge. – Greg Hewgill Apr 12 '12 at 22:22
  • 13
    Define pretty huge. 10MB overhead may have been "huge" back in 1995 but today it is laughable. Today, from app distribution standpoint, it is just as easy to deploy a PyQT (or PySide) app as a native one, or any python app for that matter. – enticedwanderer Apr 12 '12 at 22:28
  • @digivampire: "today it is laughable" Depends on your region and/or platform. For smartphone 10MB is a LOT. Also, in reagion without unlimited wireless connection trying to download 10MB via something like GPRS can be expensive even today. – SigTerm Apr 12 '12 at 22:48
  • 1
    I woudln't consider Qt to be target platform for mobile development unless you are developing (now late) Maemo app. And in that case python is clearly not the language of choice, C++ is. But that is a borderline edge case, for anything desktop related the size overhead is negligible. – enticedwanderer Apr 12 '12 at 23:29
  • 2
    Overhead would be much less than 10MB. My fairly complex PyQt app is bundled into a standalone exe which is around ~11MB. And most of it comes from Qt runtime libraries. Even with C++/Qt, you still need to deploy those runtimes somehow. – Avaris Apr 13 '12 at 01:57
  • I have built PyQt apps into packages on Win/OSX/Linux. Linux seems to build the smallest, and OSX the biggest. You are looking at over 30mb on OSX for the bare minimum libs. – jdi Apr 13 '12 at 18:54
5

C++ optimizes machine speed.

Python optimizes programmer speed.

C++ is relatively wordy: more words per idea expressed. Bugs / Lines_of_code is roughly a constant, so concision matters.

C++'s memory management is sometimes manual, which can mean strange runtime issues http://stromberg.dnsalias.org/~strombrg/checking-early.html including segfaults and memory leaks. Python takes this out of the hands of the programmer and automates it.

GUI's rarely care about the speed of their implementation language - the main issue is how fast the enduser can type and click.

J3soon
  • 3,013
  • 2
  • 29
  • 49
user1277476
  • 2,871
  • 12
  • 10
  • 2
    Qt handles (when used correctly) the memory management for you, so I don't buy that as a strong argument. The duck typing of Python can also lead to strange runtime errors that the C++ compiler would pick up. So there's arguments that Python would be worse for runtime errors than C++. I think this answer lacks objectivity. – Dan Oct 09 '20 at 12:54
1

In short, I believe that unless you have strong performance requirements, you should stick with Python. Also, as Greg mention, your program will be more portable with Python than with C++.

I love C++ but these days, for most project, I mostly turn to Python if not Java. However, if I'm writing a game or a graphics application, I might consider C++.

Danish
  • 3,708
  • 5
  • 29
  • 48
  • 2
    I think Greg's point was that it is *less* portable with Python. That's been my experience anyway. I wrote PyQT program for some colleagues. Big mistake. I am now constantly being asked to help them deal with installing PyQT. It's to the point now where, if any of them upgrades to OS X Lion and has problems, I'm throwing their machine out the window. Had I done it in C++, this probably would be less of a problem. – ely Apr 12 '12 at 22:01
  • Interesting, thanks for sharing that. I guess I assumed the pain of trying to build the same C++ program on multiple platforms would be *more* than installing Python and PyQT. BUT if all of the users will be on one platform, then C++ definitely sounds much better. – Danish Apr 12 '12 at 22:05
  • True. It probably also scales with number of people using your program. The more people you need to service, the more you'll wish you can give them all binaries that require no installation. If you only distribute the program to a few people who rarely need serviced, then the dev time bonus of Python is probably dominant. – ely Apr 12 '12 at 22:08
  • 3
    My experience has been absolutely the opposite. Using py2app and py2exe I trivially deploy my app as a binary for both Windows and Mac and have 0 issues when it comes to deployment/dependencies. – enticedwanderer Apr 12 '12 at 22:31
  • 1
    @digivampire Thats cool, will give py2app and py2exe a shot for my next project. What do you suggest for Linux? – Danish Apr 12 '12 at 22:33
  • Linux methodology is a bit different. Each distro has its own package management, and if you want to support Linux you should generate packages for at least a few most popular distros. – enticedwanderer Apr 12 '12 at 22:40
  • @Mr.F Just a tip: you can ship a Python application together with the interpreter itself. I do believe you can also customize that interpreter to include the libraries you want your application to use. This is done exactly for cases such as yours where setting up the runtime environment is too much of a fuss. I do believe `py2exe` (for Win) and `py2app` (for MacOS) support such a feature though I have never used either one of these tools. – rbaleksandar Feb 03 '16 at 18:44