{"id":1198,"date":"2022-03-31T09:00:25","date_gmt":"2022-03-31T17:00:25","guid":{"rendered":"https:\/\/journal.copperspice.com\/?p=1198"},"modified":"2022-05-29T08:43:47","modified_gmt":"2022-05-29T16:43:47","slug":"source-code-gui-example-20","status":"publish","type":"post","link":"https:\/\/journal.copperspice.com\/?p=1198","title":{"rendered":"Source Code &nbsp;(Gui Example 20)"},"content":{"rendered":"\n<p>A common requirement for applications is to save user settings when an application is closed and then restore them the next time the application is opened. This example shows how to save information about the application and text the user has entered.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;QtCore&gt;\n#include &lt;QtGui&gt;\n\nclass MainWindow : public QWidget\n{\n public:\n   MainWindow();\n\n protected:\n   void closeEvent(QCloseEvent *event) override;\n\n private:\n   void loadSettings();\n   void saveSettings();\n\n   QLineEdit *m_text;\n};\n<\/pre><\/div>\n\n\n<p>This declaration for MainWindow declares a protected method which will be called when the program is closed. There are also two private slot methods and one line edit widget declared. Data entered in the text control will be saved as a setting.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; highlight: [7,38,41]; title: ; notranslate\" title=\"\">\nMainWindow::MainWindow()\n{\n   setMinimumSize(500, 225);\n\n   QLabel *label = new QLabel(&quot;Text stored in Settings:&quot;);\n\n   m_text = new QLineEdit();\n   \n   QPushButton *pb_load = new QPushButton();\n   pb_load-&gt;setText(&quot;Load Settings&quot;);\n\n   QPushButton *pb_save = new QPushButton();\n   pb_save-&gt;setText(&quot;Save Settings&quot;);\n\n   QPushButton *pb_close = new QPushButton();\n   pb_close-&gt;setText(&quot;Close&quot;);\n\n   QHBoxLayout *layout_1 = new QHBoxLayout();\n   layout_1-&gt;addWidget(label);\n   layout_1-&gt;addWidget(m_text);\n\n   QHBoxLayout *layout_2 = new QHBoxLayout();\n   layout_2-&gt;addStretch();\n   layout_2-&gt;addWidget(pb_load);\n   layout_2-&gt;addSpacing(10);\n   layout_2-&gt;addWidget(pb_save);\n   layout_2-&gt;addSpacing(10);\n   layout_2-&gt;addWidget(pb_close);\n   layout_2-&gt;addStretch();\n\n   QVBoxLayout *layout_main = new QVBoxLayout(this);\n   layout_main-&gt;setContentsMargins(30, 50, 30, 15);\n\n   layout_main-&gt;addLayout(layout_1);\n   layout_main-&gt;addStretch();\n   layout_main-&gt;addLayout(layout_2);\n\n   connect(pb_load,  &amp;QPushButton::clicked, \n         this, &amp;MainWindow::loadSettings);\n\n   connect(pb_save,  &amp;QPushButton::clicked, \n         this, &amp;MainWindow::saveSettings);\n\n   connect(pb_close, &amp;QPushButton::clicked, \n         this, &amp;QWidget::close);\n\n   loadSettings();\n}\n<\/pre><\/div>\n\n\n<p>The user interface for this code sets up a label, a line edit control, and three push buttons. The data entered for <span style=\"color:#6a5acd\" class=\"has-inline-color\"><strong>m_text<\/strong><\/span> will be saved as a setting when the user clicks the &#8220;Save Settings&#8221; push button and also on exit of the application. The saved value will be retrieved on line 47 when the application is opened again. <br><br>On Windows the native settings are typically saved to the Windows registry and on Unix and OS X the settings are saved to a file.<\/p>\n\n\n\n<ul><li>Windows<ul><li>HKEY_CURRENT_USER\\SOFTWARE\\CopperSpice Journal\\Example 20<\/li><\/ul><\/li><li>Unix<ul><li>~\/.config\/CopperSpice Journal\/Example 20.conf<\/li><\/ul><\/li><li>OS X<ul><li>~\/Library\/Preferences\/com.CopperSpice Journal.Example 20.plist<\/li><\/ul><\/li><\/ul>\n\n\n\n<h3>Signal \/ Slot Connections<\/h3>\n\n\n\n<p>Line 38 sets up a connection to load the previously saved settings and line 41 is a signal \/ slot connection to save the settings. The slots are triggered by clicking the corresponding push button.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; highlight: [1,14,22]; title: ; notranslate\" title=\"\">\nvoid MainWindow::loadSettings()\n{\n   QSettings settings(&quot;CopperSpice Journal&quot;,&quot;Example 20&quot;);\n\n   QRect appRect = settings.value(&quot;position&quot;).toRect();\n\n   if (appRect.isValid()) {\n      setGeometry(appRect);\n   }\n\n   m_text-&gt;setText(settings.value(&quot;initialText&quot;).toString());\n}\n\nvoid MainWindow::saveSettings()\n{\n   QSettings settings(&quot;CopperSpice Journal&quot;,&quot;Example 20&quot;);\n\n   settings.setValue(&quot;position&quot;,    this-&gt;geometry());\n   settings.setValue(&quot;initialText&quot;, m_text-&gt;text());\n}\n\nvoid MainWindow::closeEvent(QCloseEvent *event)\n{\n   saveSettings();\n}\n<\/pre><\/div>\n\n\n<p>This block of code shows the implementation of the three slot methods specified in the calls to connect(). The loadSettings() and saveSettings() both begin by constructing a new QSettings object using the overload which passes two strings. The third parameter can be used to specify the parent and in our code the default will be used. <\/p>\n\n\n\n<p>On line 5 the previously saved value for the &#8220;window application position&#8221; is retrieved and then assigned to a QRect object. All settings are saved as a key\/value pair where the key is a QString and the value is a QVariant. The QVariant class in CopperSpice is a combination of the std::any and std::variant classes.<\/p>\n\n\n\n<p>When loading the settings the value for &#8220;position&#8221; must be converted from a QVariant to QRect by calling toRect(). The &#8220;initialText&#8221; value is converted from a QVariant to QString by calling toString().  <\/p>\n\n\n\n<p>The data type for geometry() on line 18 is a QRect and it will be converted to a QVariant in the call to setValue().  A similar conversion to a QVariant occurs for the call to text() on line 19. <\/p>\n\n\n\n<p>The closeEvent() method on line 22 is called automatically by the event system on exit of a program or by closing a window. This is a protected method and no explicit event registration is required. This method was added to force a call to saveSettings() on program completion.<\/p>\n\n\n\n<h3>Main Function<\/h3>\n\n\n\n<p>Since the source code for main() has not changed there is no need to show it again. Refer to example 3 or download the full source for this example.<\/p>\n\n\n\n<h3>Running the Example<\/h3>\n\n\n\n<p>To build and run this example use the same CMake build file and commands as we showed for the first example. The only suggested modification is on line 2 of the CMakeLists.txt file, change &#8220;example_1&#8221; to &#8220;example_20&#8221;. <\/p>\n\n\n\n<p><a href=\"https:\/\/download.copperspice.com\/journal\/example_20.zip\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/download.copperspice.com\/journal\/example_20.zip<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A common requirement for applications is to save user settings when an application is closed and then restore them the next time the application is opened. This example shows how to save information about the application and text the user has entered.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=\/wp\/v2\/posts\/1198"}],"collection":[{"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1198"}],"version-history":[{"count":40,"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=\/wp\/v2\/posts\/1198\/revisions"}],"predecessor-version":[{"id":1307,"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=\/wp\/v2\/posts\/1198\/revisions\/1307"}],"wp:attachment":[{"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1198"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1198"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1198"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}