Search code examples
pythonqtpyqtqt-designerpyuic

findChild on object created within pyqt designer


I have the following problem with my pyqt:

Assuming i create an object within the Qt Designer and save it as an .ui file. Then i use pyuic to convert it to an .py file. Because i want to integrate a new module into a given program, this is the favorite way to go (because later the .ui files will be converted automatically at startup to .py files).

If i have a look at my .py file i see the following for the window:

class Ui_SubWindow(object):
    def setupUi(self, SubWindow):
        SubWindow.setObjectName(_fromUtf8("SubWindow"))
        ....

i have a RemoteWindow class as MainWindow where the SubWindow is initiated:

class RemoteWindow(QtGui.QMainWindow):
  def __init__(self, subcore):
    super(RemoteWindow, self).__init__(subcore.core.gui)
    self.subcore = subcore
    self.ui = Ui_SubWindow()

Now i have a core program:

class SubCore(object):
  def __init__(self, core, identity, number):
    ...
    self.gui = RemoteWindow(self)
    self.newController = NewController(self.gui)

and the new controller class:

class NewController(object):
  def __init__(self, subwindow):
    self.subwindow = subwindow
    self.ui = subwindow.ui

from my controller i want to call a .findChild() on that window

submitFrame = self.ui.findChild(QtGui.QFrame, "frameSubmit")

, but all i get is an:

AttributeError: 'Ui_SubWindow' object has no attribute 'findChild'

I assume this is, because the class Ui_SubWindow is not a child class of some QObject but of an object, am i right?

self.ui is the same as subwindow.ui, where subwindow is an instance of RemoteWindow which has as .ui argument the Ui_SubWindow Class.

Is there any chance to make the pyuic or the Qt Designer to make this SubWindow a child of QObject, without manipulating the automatically generated .py file?


Solution

  • Qt Designer creates a design, that is, a class that serves to fill the main widget so the internal widgets are not children of this class, if you want to use findChild you must do it to the widget that returns this class after calling the method setupUi.

    class RemoteWindow(QtGui.QMainWindow):
      def __init__(self, subcore):
        super(RemoteWindow, self).__init__(subcore.core.gui)
        self.subcore = subcore
        self.ui = Ui_SubWindow()
        self.ui.setupUi(self) # Here the widget is filled
        [...]
    

    And then you should use it in the following way:

    class NewController(object):
      def __init__(self, subwindow):
        self.subwindow = subwindow
        submitFrame = self.subwindow.findChild(QtGui.QFrame, "frameSubmit")
        [...]