0

I am working with python code generated using QT Designer. I want to open a new dialogue from a button on my MainWindow. When I use the following code, the dialogue window disappears as soon as it is created. I assume this is because the QDialog object is destroyed when the method hits the return statement. What is the right way to call this dialogue?

def OpenDialogue(self):

    DialogueWindow = QtGui.QDialog()
    my_dialogue = MyDialogue.Ui_Dialog()
    my_dialogue.setupUi(DialogueWindow)
    DialogueWindow.show()

    return

For example, should I instantiate DialogueWindow in the same place that I define MainWindow and pass it through to this method?

The MainWindow constructor is as follows:

class Ui_MainWindow(QtGui.QMainWindow, object):

    def setupUi(self, MainWindow):
        [code]

It is instantiated as follows:

app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = GUI.Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
app.exec_()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
PProteus
  • 549
  • 1
  • 10
  • 23
  • You could display the constructor of the MainWindow class – eyllanesc Jun 21 '17 at 18:36
  • Please show me what I asked for, besides you could try: `DialogueWindow = QtGui.QDialog(self)` – eyllanesc Jun 21 '17 at 18:37
  • If I change `DialogueWindow.show()` to `DialogueWindow.exec_()`, it works! – PProteus Jun 21 '17 at 18:43
  • 1
    Since you are working with QT Designer, you may want to import the ui you made ready from Designer to your code by uic. I had some problem importing the ui by uic to my QDialog before, the display was poor. Then I checked this answer and it worked magically. You may want to have a look here. https://stackoverflow.com/questions/40886912/load-qdialog-directly-from-ui-file – Lumi Wang Aug 11 '17 at 05:52

1 Answers1

6

I will call Ui_MainWindow and Ui_Dialog to the classes generated by Qt Designer through the MainWindow and Dialog template, respectively. Then it is best to create classes that MainWindow and Dialog implement the interfaces. I will call pushButton to the button that you refer, then you must pass the parent to the dialog object.

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        [...]

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        [...]

class Dialog(QtGui.QDialog, Ui_Dialog):
    def __init__(self, parent=None):
        QtGui.QDialog.__init__(self, parent)
        self.setupUi(self)


class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)
        self.setupUi(self)
        self.pushButton.clicked.connect(self.openDialog)

    def openDialog(self):
        d = Dialog(self)
        d.show()

Another solution is to use exec_() by opening the window in non-modal mode

def openDialog(self):
    d = Dialog(self)
    d.exec_()

Note: It is not recommended to modify the classes generated by Qt Designer.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • That's a good tip to inherit from the auto-generated classes. I had just been meticulously updating the auto-generated code every time I had to regenerate it. – PProteus Jun 21 '17 at 18:53