Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. PyQt - Good practice for Qt object references
Forum Updated to NodeBB v4.3 + New Features

PyQt - Good practice for Qt object references

Scheduled Pinned Locked Moved Unsolved Language Bindings
2 Posts 2 Posters 783 Views 1 Watching
  • 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.
  • gabodevG Offline
    gabodevG Offline
    gabodev
    wrote on last edited by
    #1

    Hi!
    Can anyone tell me which of the following two ways is good practice?

    class Widget(QWidget):
    
        def __init__(self, parent=None):
            QWidget.__init__(self, parent)
            self._label = QLabel("Hi!")
    
        def do(self):
            print(self._label.text())
    
    class Widget(QWidget):
    
        def __init__(self, parent=None):
            QWidget.__init__(self, parent)
            label = QLabel("Hi!")
            label.setObjectName("label")
    
        def do(self):
            label = self.findChild(QWidget, 'label')
            if label not None:
                print(label.text())         
    

    Thanks!

    Developer at Ninja-IDE

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      First version, you don't want to call find each and every time you want to do something with an internal object.

      By the way, why not use super rather than call the base class __init__ ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      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