title: Opening a QDialog and saving last state
body: <p>I am trying to open a <code>QDialog</code> from a <code>QMainWindow</code>, and after closing the `QDialog, if I need to open it again, it has to open and show the same information that had when I close it.</p>
  
  <p>Here is the code of the <code>QMainWindow</code>:</p>
  
  <pre><code>class A (QMainWindow):
    def __init__(self):
      QMainWindow.__init__(self)
  
      #I create a QPushButton to open the QDialog
      self.axes1 = self.figure_canvas.figure.add_axes ([0.8, 0.01, 0.19, 0.05])
      self.button = QPushButton(self.axes1,"Open Dialog")
      self.button.on_clicked(self.OpenDialog)
  
      #This is the method to open the QDialog which is in another module
    def OpenDialog(self, event):
      text = configurePort.ConfigurePort.retrieve_data(self)
      print text
  </code></pre>
  
  <p>What this code does is create a button in my <code>QMainWindow</code> and when I click it, it opens a <code>QDialog</code>, which is created in another module. And this is the code of the <code>QDialog</code>:</p>
  
  <pre><code>class ConfigurePort(QDialog):
    def __init__(self, parent = None):
       QDialog.__init__(self, parent)
       uic.loadUi("configurePort.ui", self)
  
      #I create a button to check active ports and show them
      self.connect(self.btn_checkconn, SIGNAL("clicked()"), self.check_ports)
  
      #This method calls another class which opens another QDialog
      #and I select the port that I want
    def check_ports(self):
       self.check_serial = CheckPorts(self)
       self.check_serial.exec_()
  
      #After selecting the port, when I close the QDialog of the class named above
      #the port´s name appears in the first QDialog
    @classmethod
    def retrieve_data(cls, parent = None):
       dlg = cls(parent)
       dlg.exec_()
       text = dlg.getPortText()
       return text
  
    def closeEvent(self, event):
       #Here is where I need to write the code to close the QDialog 
       #and it does not has to be an event
  </code></pre>
  
  <p>In the method, <code>closeEvent</code>, I need to write the necessary code, so I can close the window, and using the same button that I use to open it, open it again with the last information that it showed when I closed it.</p>
  
  <p>I have tried to use <code>QSettings</code> but it did not worked (maybe I used it wrong). And I tried the <code>show()</code> and <code>hide()</code> classes of <code>PyQt</code> too, but it did not work. Hope you can help me.</p>
  
  <h2>----- EDIT -----</h2>
  
  <p>I edited the code of above. and I added some methods for a better understanding. So, i open the <code>QDialog</code> called <code>ConfigurePort</code> and it shows this:</p>
  
  <p><a href="https://i.stack.imgur.com/xcPoK.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xcPoK.jpg" alt="enter image description here"></a></p>
  
  <p>The red circle, surrounds the port´s name. It is shown in a <code>QLabel</code>,and I take this text from the <code>QDialog</code> and then print it when I close the <code>QDialog</code>. I acomplish this thanks to a question I asked before, wich is in this link:</p>
  
  <p><a href="https://stackoverflow.com/questions/37302213/getting-data-from-child-using-pyqt/37307475#37307475">Getting data from child using PyQt</a></p>
  
  <p>The <code>check_port</code> method shown in the code above, opens another <code>QDialog</code> that works great. With this I can select the ports that I need in my pc. So, this does not matter.</p>
  
  <p>So, after closing the <code>QDialog</code>(and selecting for example "COM3", as you can see in the picture), I need to open it again, and see the same information that was shown before I closed it.</p>
  
  <p>I tried to add this lines, using <code>QSettings</code> :</p>
  
  <pre><code>self.settings = QSettings("MyCompany", "MyApp")
    if not self.settings.value("windowsState") == None:
      self.restoreState(self.settings.value("windowState"))
  </code></pre>
  
  <p>But as I said before, I think that I did not use it right, but I hope that I solve this using something simpler.</p>
  
  <h2>----- EDIT 2 -----</h2>
  
  <p>Thank to the help of @Brendan Abel I have this code:</p>
  
  <pre><code>class ConfigurePort(QDialog):
    def __init__(self, parent):
      super(ConfigurePort, self).__init__(parent)
      uic.loadUi("configurePort.ui", self)
  
      self.myValue = 10
      self.restoreSettings()
  
      self.connect(self.btn_checkconn, SIGNAL("clicked()"), self.check_ports)
      self.buttonBox.button(QDialogButtonBox.Cancel).clicked.connect(self.close)
      self.buttonBox.button(QDialogButtonBox.Ok).clicked.connect(self.closeEvent)
  
      self.iniUi()
  
    def check_ports(self):
      pass
  
    def iniUi(self):
      pass #I just create some QLabels in here
  
    @classmethod
    def retrieve_data(cls, parent = None):
      dlg = cls(parent)
      dlg.exec_()
      text = dlg.getPortText()
      return text
  
    def closeEvent(self, event):
      self.saveSettings()
      super(QDialog,self).closeEvent(event)
  
    def saveSettings(self):
      settings = QSettings("MyOrg", "MyApp")
      settings.setValue("myValue", self.myValue)
  
    def restoreSettings(self):
      settings = QSettings("MyOrg", "MyApp")
      self.myValue = settings.value("myValue", self.myValue)
  </code></pre>
  
  <p>This gives me this error: <code>TypeError: QWidget.closeEvent(QCloseEvent): argument 1 has unexpected type 'bool'</code>
  I know that I am missing something, but I can not see it.  </p>