Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QML vs Qt Designer vs pure code? (in context of PyQt)

I'm an avid pythonista looking to build my first serious GUI desktop application and it seems Qt is the best option for that, so I've started learning it, and I've found there seem to be three options for how you can build your interface:

  • QML
  • Qt Designer
  • Pure code (python/C++ only)

Now I'm trying to figure out if there are differences in the capabilities of these three approaches. Do I have equal amounts of control with all three? What are the pros and cons?

Moreover, how much control do I have anyway? Can I customize the UI to a fine degree, like the Spotify interface where nearly everything is a unique custom widget? Or am I stuck with more or less generic out of the box appearances?

like image 866
temporary_user_name Avatar asked Aug 21 '14 16:08

temporary_user_name


1 Answers

I generally use Qt Designer to produce UIs. A fair number of options are available and it gives you a rough sense of what your UI will look like.

However, rather than compile the .ui file into a .py file (which you can subclass) I prefer to dynamically load the ui at runtime using either PyQt4.uic or PySide.QtUiTools. Using these, you can call a function, passing it the path to the ui file, and it will return an object for your top level widget/window (for the sake of the next example, we'll say this is placed in my_ui). This object has attributes for all other ui elements which you can access by my_ui.my_object_name where my_object_name is the name you gave the ui element in Qt Designer.

Using this approach means you:

  • don't have to compile the ui file to a py file
  • Can still access all widgets in your ui using a pythonic interface, allowing you to configure the widgets as if you had created them with pure code

Qt Designer also has the ability to specify widget promotions, so you can insert a basic QLabel (for example) into your ui, but when you load it, it can be automatically promoted to a subclass of QLabel which you have made and specified. So you can design the rough shape of your UI in Qt Designer, but easily promote most of the components to custom widgets at run time.

Because of the differing interfaces to the .ui file loader in PySide and PyQt, I wrapped them up as part of a library I maintain (qtutils) so that they have a common interface for loading ui files at runtime and specifying widget promotions.

I also don't have any experience with QML, but based on what I've heard, it's not as powerful as using QWidgets yet. However I think that the people who are writing/developing the Qt library are pushing QML as the way of the future.

My recommendation would thus be to use Qt Designer over pure code, but it would be worth checking out QML in case it does enough for what you want.

like image 50
three_pineapples Avatar answered Sep 28 '22 15:09

three_pineapples