{"id":4234,"date":"2022-02-07T15:57:30","date_gmt":"2022-02-07T15:57:30","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=4234"},"modified":"2022-02-07T15:57:31","modified_gmt":"2022-02-07T15:57:31","slug":"13d-wxwidgets-wxconfig-usage","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2022\/02\/07\/13d-wxwidgets-wxconfig-usage\/","title":{"rendered":"13d. wxWidgets &#8211; wxConfig Usage"},"content":{"rendered":"\n<p><strong>OVERVIEW<\/strong><\/p>\n\n\n\n<p>The purpose of config classes in wxWidgets is to store various information and data which can be retrievable later on. Config data can be stored in a hierarchical manner much like a directory system.<\/p>\n\n\n\n<p>Depending on the underlying platform, configuration data is stored in different ways. For eg.in Linux and Unix, they are stored as files in the disk and in Windows they are stored as keys in the Windows registry. wxWidgets provides an abstract class called <em>wxConfigBase<\/em> which is then implemented as <em>wxFileConfig<\/em> and <em>wxRegConfig<\/em>.<\/p>\n\n\n\n<p>To make things easy, there is a class called <em>wxConfig<\/em> which automatically works with <em>wxFileConfig<\/em> and <em>wxRegConfig<\/em> as required, thus saving you the trouble of dealing with file systems and Windows registry. <\/p>\n\n\n\n<p>In the sample code below, we write some values to <em>wxConfig<\/em> in a hierarchy. When we load the application with a command line parameter <em>read<\/em>, it reads the values back from the <em>wxConfig<\/em> system. Note that the <em>delete config <\/em>statement is what saves all the values to the wxConfig system. Without that line, nothing would be saved.<\/p>\n\n\n\n<p><strong>SAMPLE CODE<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">config-usage.cpp<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include  &lt;wx\/wxprec.h&gt;\n#ifndef WX_PRECOMP\n\t#include &lt;wx\/wx.h&gt;\n#endif\n#include &lt;wx\/config.h&gt;\n#include &lt;wx\/string.h&gt;\n\n\nint main(int argc, char **argv) {\n\twxInitialize();\n\twxConfig *config = new wxConfig(&quot;myapp&quot;);\n\n\tif (argc &gt;= 2) {\n\t  wxString arg = argv&#x5B;1];\n\t  if (arg == wxT(&quot;read&quot;)) {\n\t    wxPuts(config-&gt;Read(wxT(&quot;Key 1&quot;)));\n\t    wxPuts(config-&gt;Read(wxT(&quot;Key 2&quot;)));\n\t    wxPuts(config-&gt;Read(wxT(&quot;Key 3&quot;)));\n\t    \n\t    config-&gt;SetPath(&quot;\/folder1&quot;);\n\t    wxPuts(config-&gt;Read(wxT(&quot;Folder1Key&quot;)));\n\n\t    config-&gt;SetPath(&quot;\/folder1\/folder11&quot;);\n\t    wxPuts(config-&gt;Read(wxT(&quot;Folder11Key&quot;)));\n\t  }\n\t  return 1;\n\t}\n\tconfig-&gt;Write(wxT(&quot;Key 1&quot;), wxT(&quot;Value 1&quot;));\n\tconfig-&gt;Write(wxT(&quot;Key 2&quot;), wxT(&quot;Value 2&quot;));\n\tconfig-&gt;Write(wxT(&quot;Key 3&quot;), wxT(&quot;Value 3&quot;));\n\n\tconfig-&gt;SetPath(&quot;\/folder1\/folder11&quot;);\n\tconfig-&gt;Write(wxT(&quot;Folder11Key&quot;), wxT(&quot;Value in folder 11&quot;));\n\tconfig-&gt;SetPath(&quot;\/folder1&quot;);\n\tconfig-&gt;Write(wxT(&quot;Folder1Key&quot;), wxT(&quot;Value in folder 1&quot;));\n\n\n\tdelete config;\n\twxPuts(wxT(&quot;done&quot;));\n\twxUninitialize();\n}\n\n<\/pre><\/div>\n\n\n<p>The output is given below<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"726\" height=\"233\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/02\/Screenshot-from-2022-02-07-21-25-28.png\" alt=\"\" class=\"wp-image-4238\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/02\/Screenshot-from-2022-02-07-21-25-28.png 726w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/02\/Screenshot-from-2022-02-07-21-25-28-620x199.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/02\/Screenshot-from-2022-02-07-21-25-28-300x96.png 300w\" sizes=\"auto, (max-width: 726px) 100vw, 726px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>OVERVIEW The purpose of config classes in wxWidgets is to store various information and data which can be retrievable later on. Config data can be <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2022\/02\/07\/13d-wxwidgets-wxconfig-usage\/\" title=\"13d. wxWidgets &#8211; wxConfig Usage\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":3595,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[302,365],"tags":[],"class_list":["post-4234","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cc","category-wxwidgets"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4234","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=4234"}],"version-history":[{"count":5,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4234\/revisions"}],"predecessor-version":[{"id":4240,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4234\/revisions\/4240"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/3595"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=4234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=4234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=4234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}