<div dir="ltr"><div>> I'm trying to understand how does save state works using QT...<br>> ... I only found C++ examples which I cant read,<br><br>Being able to mentally translate the C++ code in<br>the Qt doc into Python is a very useful skill!<br>

It really isn't too hard. Turn :: into dot and add<br></div>"self." everywhere.<br><div><br>The first thing to know is how to get control when<br>the app is quitting and what to do then.<br>There is an example here:<br>

<br><a href="http://qt-project.org/doc/qt-5.0/qtwidgets/qmainwindow.html#saveState">http://qt-project.org/doc/qt-5.0/qtwidgets/qmainwindow.html#saveState</a><br><br>What this is trying to say is: your main window subclass<br>

(what you called MyWindow in your sample code)<br>needs to define its own method closeEvent().<br><br>What does this method do? You look up to the top<br>of the QMainWindow page and there is no definition<br>for closeEvent. This tells you it is inherited.<br>

<br>So you click the link at the top, "List of all<br>members, including inherited members"<br>and see closeEvent in the list, click it, and<br>you are now on the QWidget page. So this is where<br>QMainWindow inherits closeEvent from.<br>

<br>On that page you get the details of what closeEvent<br>does and when it is called. Read it.<br><br>Now go back to the QMainWindow::saveState link above.<br><br>So when your main window is being closed, Qt will<br>call its closeEvent method. If you do not define<br>

one, nothing is saved at close time. So add<br>some code to your class like:<br><br>   def closeEvent(self, event):<br>      ...something…<br><br>In that method you can use a QSettings object<br>to save the window size and position ("Geometry").<br>

Maybe back in the __init__ you did this:<br><br>      self.settings = QSettings("MyCompany","MyAppname")<br><br>Read about the QSettings object here:<br><br><a href="http://qt-project.org/doc/qt-5.0/qtcore/qsettings.html">http://qt-project.org/doc/qt-5.0/qtcore/qsettings.html</a><br>

<br>Now in the closeEvent you can do this (not tested!)<br><br>      self.settings.setValue("geometry", self.saveGeometry())<br>      self.settings.setValue("statebytes", self.saveState())<br><br>Then, up in your __init__ method, you can use<br>

the settings object to recover the geometry<br>and the state bytearray from the last time the<br>program ended. See this link:<br><br><a href="http://qt-project.org/doc/qt-5.0/qtwidgets/qmainwindow.html#restoreState">http://qt-project.org/doc/qt-5.0/qtwidgets/qmainwindow.html#restoreState</a><br>

<br>for a very similar example.<br><br>In short:<br><br>* During shutdown your closeEvent() is called<br><br>* in it, you save everything in QSettings<br><br>* During __init__ you get everything back by<br></div><div>querying the settings object.<br>

<br><br></div></div>