Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 684
fighter-ii Avatar asked Oct 24 '25 08:10

fighter-ii


1 Answers

You don't need to use findChild at all, because pyuic will automatically create attributes in the ui object for all the widgets defined in Qt Designer. The attribute names are taken from the objectName. So all you need is:

submitFrame = self.ui.frameSubmit
like image 199
ekhumoro Avatar answered Oct 27 '25 00:10

ekhumoro