0

Running a code posted below brings a Dialog_01 QMainWindow with one button on it. Clicking the button should close this window (Dialog_01) and replace it with another: Dialog_02 which is practically a copy of the first. Clicking Dialog_02 button closes it and reopens Dialog_01. There are no errors while the code runs. But it is definitely missing something since Dialog_02 is not showing up even while it is being declared. What am I missing?

from PyQt4 import QtCore, QtGui    


class Dialog_02(QtGui.QMainWindow):
    def __init__(self):
        super(Dialog_02, self).__init__()

        myQWidget = QtGui.QWidget()
        myBoxLayout = QtGui.QVBoxLayout()       

        Button_02 = QtGui.QPushButton("Press to close this dialog 02 and re-open Dialog 01")
        Button_02.clicked.connect(self.callAnotherQMainWindow)
        myBoxLayout.addWidget(Button_02)

        myQWidget.setLayout(myBoxLayout)
        self.setCentralWidget(myQWidget)

    def callAnotherQMainWindow(self):
        print "This is supposed to call Dialog_01"



class Dialog_01(QtGui.QMainWindow):
    def __init__(self):
        super(Dialog_01, self).__init__()

        myQWidget = QtGui.QWidget()
        myBoxLayout = QtGui.QVBoxLayout()       

        Button_01 = QtGui.QPushButton("Press to close this dialog and open Dialog 02")
        Button_01.clicked.connect(self.callAnotherQMainWindow)
        myBoxLayout.addWidget(Button_01)        

        myQWidget.setLayout(myBoxLayout)
        self.setCentralWidget(myQWidget)

    def callAnotherQMainWindow(self):
        dialog_1.hide()

        dialog_2 = Dialog_02()
        dialog_2.resize(480,320)
        dialog_2.show()
        dialog_2.raise_() 


dialog_1 = Dialog_01()
dialog_1.show()
dialog_1.resize(480,320)
sys.exit(app.exec_())

EDITED LATER: WORKING FIXED VERSION

The mistake was in using dialog.hide() instead of dialog.close(). A calling dialog needs to be closed (instead of hidden) before a second dialog can be displayed.

from PyQt4 import QtCore, QtGui    
app = QtGui.QApplication(sys.argv)

class Dialog_02(QtGui.QMainWindow):
    def __init__(self):
        super(Dialog_02, self).__init__()

        myQWidget = QtGui.QWidget()
        myBoxLayout = QtGui.QVBoxLayout()       

        Button_02 = QtGui.QPushButton("Press to close this dialog 02 and re-open Dialog 01")
        Button_02.clicked.connect(self.callAnotherQMainWindow)
        myBoxLayout.addWidget(Button_02)

        myQWidget.setLayout(myBoxLayout)
        self.setCentralWidget(myQWidget)
        self.setWindowTitle('Dialog 02')

    def callAnotherQMainWindow(self):
        self.close()
        self.dialog_01 = Dialog_01()
        self.dialog_01.show()
        self.dialog_01.raise_()



class Dialog_01(QtGui.QMainWindow):
    def __init__(self):
        super(Dialog_01, self).__init__()

        myQWidget = QtGui.QWidget()
        myBoxLayout = QtGui.QVBoxLayout()       

        Button_01 = QtGui.QPushButton("Press to close this dialog and open Dialog 02")
        Button_01.clicked.connect(self.callAnotherQMainWindow)
        myBoxLayout.addWidget(Button_01)        

        myQWidget.setLayout(myBoxLayout)
        self.setCentralWidget(myQWidget)
        self.setWindowTitle('Dialog 01')

    def callAnotherQMainWindow(self):
        self.close()
        self.dialog_02 = Dialog_02()
        self.dialog_02.show()
        self.dialog_02.raise_()

dialog_1 = Dialog_01()
dialog_1.show()
dialog_1.resize(480,320)
sys.exit(app.exec_())
alphanumeric
  • 17,967
  • 64
  • 244
  • 392
  • Having a dialog call another dialog sounds like a bad idea from a usability perspective. Is there another way you can do this? –  Mar 06 '14 at 03:49
  • I don't have preferences. I would like one dialog to be hidden while another is active...and vise versa. If that could be achieved without closing one dialog and reopening another... I am for it! Show how it could be done! :) – alphanumeric Mar 06 '14 at 03:55

1 Answers1

1

in Dialog_02 class modify the following method:

def callAnotherQMainWindow(self)
    self.close()
    self.dialog_01 = Dialog_01()
    self.dialog_01.show()
    self.dialog_01.raise_()

And in Dialog_01 class modify the following method:

def callAnotherQMainWindow(self)
    self.close()
    self.dialog_02 = Dialog_02()
    self.dialog_02.show()
    self.dialog_02.raise_()
qurban
  • 3,885
  • 24
  • 36