Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

Saving Window Size State: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
'''English''' [[Saving Window Size State Bulgarian|Български]]
[[Category:snippets]]


=Saving Window Size State=
'''English''' [[Saving_Window_Size_State_Bulgarian|Български]]


The above code will save and restore window position, size, toolbar visibility, toolbar docking area, dock states, locations and sizes. It saves using [http://doc.qt.nokia.com/stable/qsettings.html QSettings] ''[doc.qt.nokia.com]'' which will store your settings in a platform correct way.
= Saving Window Size State =


===Categories:===
<code>class MainWindow : public QMainWindow<br />{<br /> Q_OBJECT


* [[:Category:snippets|snippets]]
public:<br /> MainWindow(QWidget *parent = 0) : QMainWindow(parent) {<br /> QSettings settings;
 
restoreGeometry(settings.value(&quot;mainWindowGeometry&amp;quot;).toByteArray());
 
// create docks, toolbars, etc…
 
restoreState(settings.value(&quot;mainWindowState&amp;quot;).toByteArray());<br /> }
 
void closeEvent(QCloseEvent *event) {<br /> QSettings settings;<br /> settings.setValue(&quot;mainWindowGeometry&amp;quot;, saveGeometry());<br /> settings.setValue(&quot;mainWindowState&amp;quot;, saveState());<br /> }<br />};
 
int main(int argc, char *argv[])<br />{<br /> QApplication a(argc, argv);<br /> QCoreApplication::setOrganizationDomain(&quot;OrgDomain&amp;quot;);<br /> QCoreApplication::setOrganizationName(&quot;OrgName&amp;quot;);<br /> QCoreApplication::setApplicationName(&quot;AppName&amp;quot;);<br /> QCoreApplication::setApplicationVersion(&quot;1.0.0&amp;quot;);
 
MainWindow w;<br /> w.show();
 
return a.exec&amp;amp;#40;&amp;#41;;<br />}</code>

Revision as of 09:34, 24 February 2015


English Български

Saving Window Size State

class MainWindow : public QMainWindow<br />{<br /> Q_OBJECT

public:<br /> MainWindow(QWidget *parent = 0) : QMainWindow(parent) {<br /> QSettings settings;

restoreGeometry(settings.value(&quot;mainWindowGeometry&amp;quot;).toByteArray());

// create docks, toolbars, etc…

restoreState(settings.value(&quot;mainWindowState&amp;quot;).toByteArray());<br /> }

void closeEvent(QCloseEvent *event) {<br /> QSettings settings;<br /> settings.setValue(&quot;mainWindowGeometry&amp;quot;, saveGeometry());<br /> settings.setValue(&quot;mainWindowState&amp;quot;, saveState());<br /> }<br />};

int main(int argc, char *argv[])<br />{<br /> QApplication a(argc, argv);<br /> QCoreApplication::setOrganizationDomain(&quot;OrgDomain&amp;quot;);<br /> QCoreApplication::setOrganizationName(&quot;OrgName&amp;quot;);<br /> QCoreApplication::setApplicationName(&quot;AppName&amp;quot;);<br /> QCoreApplication::setApplicationVersion(&quot;1.0.0&amp;quot;);

MainWindow w;<br /> w.show();

return a.exec&amp;amp;#40;&amp;#41;;<br />}