Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Best Practice Questions
Forum Update on Monday, May 27th 2025

Best Practice Questions

Scheduled Pinned Locked Moved Unsolved Qt for Python
1 Posts 2 Posters 796 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kainev
    wrote on last edited by kainev
    #1

    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!

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved