Through numerous suggestions, I'm trying to split PyQt code so it's more modular, each ChildClass
inheriting Main(QApplication)
. Inheritance in the following works, but only at parsing attributes defined within ChildClass
, not at changing something in Main. The below shows the GUI, prints "Child Class", but doesn't change lineEdit
to "ChildClass
", without an error saying "u cannot be found" i.e. it's seeing "u", but not updating it.
(the 'running=0' I feel needed adding because otherwise a recursive depth error kept appearing, seemingly because Main.__init__
triggers Songs.__init__
, which itself trigger Main.__init__ again
)
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.uic import *
app = QApplication(sys.argv)
from ui.main import Ui_MainWindow
running = 0
class Main(QMainWindow):
def __init__(self):
global running
QMainWindow.__init__(self)
self.u = Ui_MainWindow()
self.u.setupUi(self)
if running == 0:
running = 1
ChildClass();
class ChildClass(Main):
def __init__(self):
Main.__init__(self)
print "Child Class"
self.u.lineEdit.setText("ChildClass")
The way I've found works is to pass Main's self as an argument to ChildClass
, and then have ChildClass
use that instead of self i.e.
Main():
...
ChildClass(self)
ChildClass():
def __init__(self, a):
a.u.lineEdit.setText("Child Class can now change Qt Line Edit")
Am I on the right track here; is there a way to truly inherit Main, without having to pass Main's "self" as an argument (which would make inheritance obsolete)?
Regards