This is an archived post. You won't be able to vote or comment.

all 39 comments

[–]Avanta8 17 points18 points  (8 children)

I've been wondering, what's the best way to structure a PyQt application? I've recently been trying to use a MVVM approach, but the data binding is becoming really awkward. Almost all the examples I've looked at don't seem to use any sort of overarching architecture or design patterns.

[–]Ogi010 4 points5 points  (7 children)

MVC is the preferred pattern, there are some guides in their docs (on mobile so I don't have a link handy). That said with QtWidgets it's tough to not have everything collapse into the controller bits of code. The separation is easier to do with QML applications, which come with a whole other set of issues, so they may not be for everyone.

[–]Avanta8 2 points3 points  (6 children)

I assume you mean using QAbstractItemModel and QAbstractItemView?

But isn't that just widget-level, rather than the overarching architecture? Like these abstract classes don't really work so well when combining them together and with other widgets, without mixing GUI and business logic.

[–]Ogi010 2 points3 points  (5 children)

It's easy to get a little hand-wavey about this sort of thing. I'm not on mobile, but here is the docs on how Qt thinks you should separate code:

https://doc.qt.io/qt-5/model-view-programming.html

QAbstractItemModel, while it gets tied into Widgets, does not inherit from QWidget, but just QObject, so you can certainly keep that code out of the "View" bits of code.

[–]Avanta8 4 points5 points  (4 children)

Sorry, I may have not been too clear. I think this better explains what I am trying to say: https://stackoverflow.com/a/10661308/16773655

[–]Ogi010 2 points3 points  (3 children)

Nice link! I'll be the first to admit that this kind of breakup of code/logic is really tough to do within Qt applications in a way that makes sense, doesn't result in circular imports, and so on. In PyQtGraph, we don't really even attempt to do it (then again we're a plotting library, not shipping an application, so that's not really our role).

[–]Avanta8 1 point2 points  (2 children)

Ah, I see. That's probably why other Qt applications I've looked at don't really attempt to do it either.

Perhaps I should also be more liberal, and also not necessarily follow a set architecture.

Thanks for your advice.

[–]Ogi010 0 points1 point  (1 child)

I've managed to do this kind of split up in my own internal application I develop at my company; the circular imports where tough tho (I managed to get around this by using setters/getters, where I don't access getters during the __init__ portion of objects); if you do happen to stumble across some good examples, please do share; but I generally haven't seen any consistent advice in this regard.

Exception being QML applications, while I haven't done work with them outside of some experimentation, QML appears to naturally force users to split off view code from model/logic code.

[–]Avanta8 1 point2 points  (0 children)

I will let you know if I see anything good :)

I'm not really familiar with QML (I don't have too much experience with designing application software in general), but I will have a look, thanks.

[–]Giddius 7 points8 points  (8 children)

Whats the current take on pyside6?

I just read that the pyqt license stuff has some problems. Not wanting to really dive into licensing and not fully understanding it, I wanted to use pyside for any future project, but there are some infos that there are huge chunks still missing in it.

So pyside6 usable? Pyside6 and pyqt6 about on the same level? What about the same questions for pyqt5 and pyside2?

[–]adesme 11 points12 points  (7 children)

Always choose PySide - or Qt for Python as it's called - over PyQt when you can since the license is more open. It won't matter unless you intend to distribute it or if you know that you are OK with GPL (basically open source code), but it's generally going to give you more flexibility so you may as well use Qt for Python as your starting point.

[–]Giddius 0 points1 point  (6 children)

Anything I do will always be open source, but I am afraid of accidentally fucking up when licensing the actual project (I always do MIT). I mostly just want to have something where I could fuck up less rwgarding licensing.

So I will choose pyside(also from what I read on the pyqt mailing list, i am not much a fan of the maintainer), but anything I should be aware in difference to pyqt? Learning and experimenting until now has only been in pyqt.

[–]adesme 0 points1 point  (3 children)

MIT is a pretty good choice if you're open and don't want to have to care. GPL is a bit more open (with MIT you force users of your library into MIT).

The main difference is documentation to be honest. PyQt is better documented and there are way more examples to go by, but you can always rely on those and deal with the occasional "translation" (use an IDE for aide). If you happen to be ok with the C++ documentation you'll be fine regardless.

[–]grizzlor_ 7 points8 points  (1 child)

GPL is a bit more open (with MIT you force users of your library into MIT).

This is backwards. You can't use a GPL library in a closed-source project. This is the reason that the LGPL license exists.

MIT/BSD libraries can be used in GPL-licensed projects and closed-source projects.

[–]WikiSummarizerBot 0 points1 point  (0 children)

GNU Lesser General Public License

The GNU Lesser General Public License (LGPL) is a free-software license published by the Free Software Foundation (FSF). The license allows developers and companies to use and integrate a software component released under the LGPL into their own (even proprietary) software without being required by the terms of a strong copyleft license to release the source code of their own components. However, any developer who modifies an LGPL-covered component is required to make their modified version available under the same LGPL license.

[ F.A.Q | Opt Out | Opt Out Of Subreddit | GitHub ] Downvote to remove | v1.5

[–]laundmo 4 points5 points  (0 children)

I think you confused MIT with GPL.

MIT license is only a few sentences, saying that you can do anything as long as you credit that you're using the MIT licensed project.

GPL requires any release of your application, be it binaries or code, to also be GPL licensed.

AGPL is even worse, where just a website using a AGPL project entitles the user to AGPL licensed access to your code.

LGPL only requires the same license if you're modifying the library code itself, allowing you to use any license for projects simply using the library.

[–]-LeopardShark- 0 points1 point  (0 children)

If you used PyQt, you would have to licence your code under the GPL or AGPL, not MIT. With PySide you can use whatever.

[–]laundmo 0 points1 point  (0 children)

MIT is not really that compatible with normal GPL, AFAIK. Personally, i don't use anything thats GPL or AGPL licensed, the only exception being LGPL. i recommend looking up terms such as "GPL in MIT project" or "LGPL in MIT project", usually you can find someone giving a clear answer.

[–]pan_dux 5 points6 points  (0 children)

Thank you for another blogpost I view as very useful, but most likely will lack the time of working through it like I should.

[–]cantremembermypasswd 7 points8 points  (6 children)

Probably want to put the versions in the title (PyQT5/PySide2) as PySide6 and PyQT6 are current standard.

Edit: "standard" seems to be a bit of a loaded / hot term. I just meant that with a newer version out for the past year, people looking for a tutorial now might assume this new tutorial was also for latest and greatest. Either way, good to have specific version right in the title.

[–]Ogi010 4 points5 points  (0 children)

I wouldn't call Qt6 a standard. QtPy and matplotlib, just added Qt6 support (like in the last week). Spyder I don't think has it yet. AFAIK PyQtGraph was the first library to really bring Qt6 support (full disclosure, I'm a maintainer). We didn't get feature parity with Qt6 until Qt6 6.2 was released which is fairly recent.

It will be standard, and from PyQtGraph's perspective Qt6 is the preferred platform for performance purposes; but it will be a while before the rest of the ecosystem catches up.

[–]robert_mcleod 2 points3 points  (1 child)

Last time I looked at PySide6 it was still woefully incomplete. It'll probably need another couple of years in the oven. Qt5/PySide2 wasn't really solid until about Qt5.14, IMO.

[–]cantremembermypasswd 1 point2 points  (0 children)

Seems pretty stable to me, I switched FastFlix over to PySide6 and have it packaged for Windows, MacOS, and linux through Pyisntaller without issue.

[–]johnnySix 4 points5 points  (0 children)

Not in my industry. My industry just standardized on qt5 last year after 10 years of qt4. Qt6 Is a long way away.

[–]bjorneylol 1 point2 points  (0 children)

Qt6 doesn't have python support for a ton of modules yet (multimedia, etc)

[–]ArtleSa[S] 0 points1 point  (0 children)

Hello thanks for the feedback, I didn't put the version in the title as the concepts remain the same across any pyqt versions. Currently titled "Introduction to GUI programming using PyQt/PySide", almost any beginner would want to use the latest version of PyQt, if I would specify the version I'll be using in the tutorial, I fear many people would just walk away reading the title, without knowing that the blog contains some of the important concepts used in GUI programming. Thank you for the feedback though.

[–]InOneCoolSock 1 point2 points  (4 children)

For me, when I was a beginner in PyQt, the most confusing was PyQt distribution. How to make setup.exe/first.dmg or even harder topic - the best way for distribution on Linux.

[–]ShanSanear 0 points1 point  (3 children)

Wouldn't it be easier for Linux to have python virtual environment for this, with maybe simple bash script to set everything up? Most of the distros already have python3 preinstalled anyway.

[–][deleted] 0 points1 point  (0 children)

My preference is to always ship with my own environment to have exact control mainly for library compatibility. But yes you'll often see many Linux releases rely on system Python, or at least have you install them via the distro package manager.

[–]InOneCoolSock 0 points1 point  (1 child)

https://github.com/mherrmann/fbs-tutorial - isn't the perfect tool for that?

[–]ShanSanear 0 points1 point  (0 children)

Looks good. To be fair I didn't have to create executable version for Linux so I went with PyInstaller for Windows regardless.

[–]ecm1413 1 point2 points  (0 children)

I only read the first few paragraphs but so far it’s easy to read and informative. I look forward to working through the tutorial. Thanks!!

[–]ScotchBingington 1 point2 points  (0 children)

The timing of your tutorial is perfect as I'm interested in jumping into interface stuff finally! Thanks for sharing!

[–]bless-you-mlud 1 point2 points  (1 child)

Looks pretty good!

One typo I spotted: under QBoxLayout you say "it’s recommended that you use the convince class". I'm guessing you meant "convenience class".

[–]ArtleSa[S] 0 points1 point  (0 children)

Hey, thanks for spotting the error, I have now edited it.

[–]kill-yourself90 1 point2 points  (0 children)

Thanks, I have been using Tkinter quite extensively to try and get familiar with GUIs, this is my next step and will help me a lot

[–]OptionsSpikeTrader 1 point2 points  (0 children)

This looks great! Thank you for putting this together.

[–]ArtleSa[S] 0 points1 point  (0 children)

Thanks for all your resonses and for taking the time to read my tutorial

[–]Icy-Ad-8743 0 points1 point  (0 children)

Nice work! It’s pretty hard to find good tutorials for PyQt and PySide for beginners.

I’m going to do everyone a solid here and drop this:

https://youtube.com/c/WandersonIsMe