0

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

AAEM
  • 1,837
  • 2
  • 18
  • 26
user2422819
  • 177
  • 13
  • Why are you calling `ChildClass();` in `Main`'s constructor? Why do you have semicolons? Inheritance works fine in PyQt and I don't see the problem. – Neil G May 21 '14 at 14:14
  • 1
    Hello again. I think you're running into a XY problem. That is you have a problem and you want to solved with what you think is the solution. Your solution have become into a new problem. I think you hould describe what **excactly are you trying to achive**. – Raydel Miranda May 21 '14 at 14:18
  • Hi Raydal. I don't understand, I thought that's what I've posted: trying to inherit from Main, but the statements in ChildClass can't update the Main application. – user2422819 May 21 '14 at 14:22
  • Of course you run into recursion, because `ChildClass()` will itself call `Main.__init__`, which in turn triggers `ChildClass()` again. Are you aware, that `ChildClass()` creates a new instance of an object? It seems you expect it to change the already existing `Main`instance. – sebastian May 21 '14 at 14:34

0 Answers0