0

I have a QWidget (login form) and QMainWindow(main form). the problem is when I clicked a qpushbutton into the qwidget the QmainWindow should appear, but It doesn't.

class Ui_frmInicial(QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.resize(400, 250)
        self.btnOpen = QtGui.QPushButton(self)
        self.btnOpen.setGeometry(QtCore.QRect(110, 170, 111, 41))
        self.btnOpen.clicked.connect(self.btnOpen_clicked)
    def btnOpen_clicked(self):
        print('ok ')
        #mform = Ui_mainForm()
        #mform.show()
if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    ui = Ui_frmInicial()
    ui.show()
    sys.exit(app.exec_())

and the other class is:

class Ui_mainForm(QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        print('ok')
        self.resize(928, 695)
        QtCore.QMetaObject.connectSlotsByName(self)

what would be the mistake? I run the project from Ui_frmInicial. In the console I show the print 'ok' into the init() function but the qmainwindow doesn't show. Thanks in advance

GSandro_Strongs
  • 773
  • 3
  • 11
  • 24

1 Answers1

1

You need to ensure, the window you're opening is not deleted immediately after show is called.

I would suggest, you define the window in the main scope and init the form with it:

class Ui_frmInicial(QWidget):
    def __init__(self, window):                         #window parameter
        QtGui.QWidget.__init__(self)
        self.resize(400, 250)
        self.btnOpen = QtGui.QPushButton(self)
        self.btnOpen.setGeometry(QtCore.QRect(110, 170, 111, 41))
        self.btnOpen.clicked.connect(lambda c: window.show()) #lambda callback

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    window = Ui_mainForm()                              #window definition
    ui = Ui_frmInicial(window)                          #ui initialisation
    ui.show()
    sys.exit(app.exec_())

Or at least as a member of the form.

tynn
  • 38,113
  • 8
  • 108
  • 143
  • Thanks a lot but I had an error. I changed by def __init__(self, window): When I run in the console says Traceback (most recent call last): window.show() NameError: global name 'window' is not defined. Please help me. thanks in advance. – GSandro_Strongs Mar 19 '15 at 21:41
  • I added comments, where I changed your code. I guess you've just overlooked something there. – tynn Mar 19 '15 at 21:51
  • I changed the code, like this: self.btnOpen.clicked.connect(btnOpen_clicked) and def btnOpen_clicked(self):lambda c:self.window.show() I'll put here because in the future I'll do business logical. QMAinwindow doesn't appear yet. Is corret to put the lambda code into that function? – GSandro_Strongs Mar 19 '15 at 22:05
  • 1
    If you've set `self.window` in your `__init__` you'll just need to leave out the lambda. So `btnOpen_clicked(self): self.window.show()` could do the trick. – tynn Mar 19 '15 at 22:17
  • thanls a lot. what I add if I want to hide the first form. the main windows is showed and the other one I don't need It? . thanks in advance. – GSandro_Strongs Mar 19 '15 at 22:22
  • 1
    Just call `self.hide()` after `self.window.show()`. Or change your setup somehow, that you'd be able to delete the form completely. Just try it yourself and if it's not working out, ask a new question targeting that new problem. Good luck for you! – tynn Mar 19 '15 at 22:35