Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Best Practice Questions



  • Hi everyone. One thing I'm struggling with learning PyQt/PySide (I'm using the Qt.py module so everything is PySide2 syntax and only the features that are shared between PyQt 4/5 & PySide 1/2 are available) is best practices. I see and hear people doing things in different ways.

    1 - Usage of super

    class MyCustomWidget(QWidget):
        def __init__(self, parent=None):
            super(MyCustomWidget, self).__init__(parent=parent)
    

    OR

    class MyCustomWidget(QWidget):
        def __init__(self):
            QWidget.__init__(self)
    

    I figure as all native QWidgets at least have the parent argument then for consistency you should as well. I've been told I shouldn't do this though.

    2 - Keeping Python references to any created Qt objects

    self._myLayout = QVBoxLayout()
    self.setLayout(self._myLayout)
    

    OR

    myLayout = QVBoxLayout()
    self.setLayout(self._myLayout)
    

    Is it necessary or at least best practice to maintain an active reference in Python to any created objects whether they be layouts or QWidgets, or is this just unnecessary clutter?

    3 - Setting layouts on widgets

    myLayout = QVBoxLayout(myWidget)
    

    OR

    myLayout = QVBoxLayout()
    myWidget.setLayout(myLayout)
    

    What would be considered best practice? I honestly find the top version easier to understand as when I see the layout is made I instantly know what QWidget it is for.

    4 - Setting up UI in methods called from __init__

    class MyCustomWidget(QWidget):
        def __init__(self):
            QWidget.__init__(self)
    
            self.setupUI()
            self.setupMenu()
            
        def setupUI(self):
            self._myLayout = QVBoxLayout()
            self.myButton = QPushButton()
            ...
        
        def setupMenu(self):
            ...
    

    I've seen implementations like this a lot, where they define their instance attributes in other methods, sometimes several different ones. This breaks standard python wisdom, but is it considered worth it because it increases readability? I always define everything in my __init__ and just keep try to keep everything in a logical order.

    5 - Deleting widgets
    Not really a best practice thing, just if anyone has the time, what is the correct way to delete a QWidget and ensure it's removed fully? Is just setting it's parent to None enough?

    Please if answering a point let me know the reasons why it is best practice, not just that it is best practice, so I can gain a better understanding, thanks!


  • Banned

    1 - Usage of super

    This one is done mainly by either lazy coders or newbies for the most part. Anyone that has fully researched Super( ) would know that using it in most cases creates more issues than not using it. In fact the whole purpose of Super( ) is to help with a specific but rare issue that occurs by not using it and most of the programmers that are using Super( ) are not even aware of the specific and rather rare issue. Further it is always good practice to K.I.S.S. your code (Keep It Simple and Smart) and using Super( ) breaks this rule by adding unnecessary complexity.

    2 - Keeping Python references to any created Qt objects

    Again the K.I.S.S. principle applies you do not need a handle (self.) to an object if you do not plan to manipulate that object later on so VBox = QVBoxLayout( ) is generally the rule for me as that container is almost never manipulated by my code later on.

    3 - Setting layouts on widgets

    And once again the K.I.S.S. principle weighs in saying that you should not add more complexity than is needed. With that said this is VBox = QVBoxLayout(Widget) more complex in its usage as it obfuscates what you are actually doing where the following is very obvious and straight forward. Lastly what are you saving with that obfuscation -- one additional line of code?

    VBox = QVBoxLayout()
    VBox .setLayout(VBox )
    

    4 - Setting up UI in methods called from __init__

    This one can be pretty much preference however for me it depends on just how much code is being implemented within the secondary functions/methods. Still keep in mind that __init__ should be just that whatever you are initializing for that particular object. Lastly I think the self.setupUI( ) call is a copy/paste issue with folks that use a Designer or Creator UI code base and rarely needed for any other reason. But again this one is mostly just preference/style so no good or bad to it.

    5 - Deleting Widgets

    Well since you are asking this question perhaps you might want to read this -- as he covers this along with other potential issues that you might be interested in.

    https://machinekoder.com/how-to-not-shoot-yourself-in-the-foot-using-python-qt/


Log in to reply