{"id":1901,"date":"2023-04-14T09:00:07","date_gmt":"2023-04-14T17:00:07","guid":{"rendered":"https:\/\/journal.copperspice.com\/?p=1901"},"modified":"2023-04-16T16:52:19","modified_gmt":"2023-04-17T00:52:19","slug":"source-code-gui-example-38","status":"publish","type":"post","link":"https:\/\/journal.copperspice.com\/?p=1901","title":{"rendered":"Source Code &nbsp;(Gui Example 38)"},"content":{"rendered":"\n<p>The QStringParser class provides a variety of methods to process strings of text. If you have a body of text which needs to be separated at a comma or some other delimiter, use the split() method. There are other methods to convert a number to a string, concatenate a list of strings separated by a given delimiter, or replace a marker in some text with a value obtained at run time. This example will show how to use split() and formatArg().<\/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 private:\n   QLineEdit *textEdit;\n   QComboBox *split_A;\n   QComboBox *split_B;\n\n   QLineEdit *textFormat;\n   QLineEdit *arg_1;\n   QLineEdit *arg_2;\n   QLineEdit *result;\n\n   void parseText();\n};\n<\/pre><\/div>\n\n\n<p>This declaration for MainWindow includes the widget controls which will be updated in the implementation of the <span style=\"color:#6a5acd\" class=\"has-inline-color\">parseText()<\/span> slot method. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; highlight: [12,15,25,30,39]; title: ; notranslate\" title=\"\">\nMainWindow::MainWindow()\n{\n   setMinimumSize(700, 400);\n\n   \/\/ part 1\n   QLabel *textLabel = new QLabel(&quot;Enter Text: &quot;);\n\n   textEdit = new QLineEdit();\n   textEdit-&gt;setText(&quot;A wacky fox and sizeable pig jumped &quot;\n      &quot;halfway over a blue moon.&quot;);\n\n   QLabel *label_A1 = new QLabel(&quot;split(text, ' ')&quot;);\n   split_A = new QComboBox();\n\n   QLabel *label_B1 = new QLabel(\n      &quot;split(text, \\&quot;(fox|pig|blue)\\&quot;)&quot;);\n   split_B = new QComboBox();\n\n   \/\/ part 2\n   QLabel *label_A2 = new QLabel(&quot;Enter Text:&quot;);\n\n   textFormat = new QLineEdit();\n   textFormat-&gt;setText(&quot;%1 says hello in example %2&quot;);\n\n   QLabel *label_B2 = new QLabel(&quot;Enter Arg 1:&quot;);\n\n   arg_1 = new QLineEdit();\n   arg_1-&gt;setText(&quot;CopperSpice&quot;);\n\n   QLabel *label_C2 = new QLabel(&quot;Enter Arg 2:&quot;);\n\n   arg_2 = new QLineEdit();\n   arg_2-&gt;setText(&quot;38&quot;);\n\n   QLabel *label_D2 = new QLabel(&quot;formatArg()&quot;);\n\n   result = new QLineEdit();\n\n   QFrame *hline = new QFrame();\n   hline-&gt;setLineWidth(1);\n   hline-&gt;setFrameStyle(QFrame::HLine);\n\n   QPushButton *pb_run = new QPushButton();\n   pb_run-&gt;setText(&quot;Run QStringParser&quot;);\n\n   QPushButton *pb_close = new QPushButton();\n   pb_close-&gt;setText(&quot;Close&quot;);\n\n   \/\/\n   QGridLayout *grid1 = new QGridLayout();\n   grid1-&gt;setHorizontalSpacing(20);\n   grid1-&gt;setVerticalSpacing(15);\n\n   grid1-&gt;addWidget(textLabel, 3, 1);\n   grid1-&gt;addWidget(textEdit, 3, 2, 1, 2);\n\n   grid1-&gt;addWidget(label_A1, 4, 2);\n   grid1-&gt;addWidget(split_A, 4, 3, 1, 1);\n\n   grid1-&gt;addWidget(label_B1, 5, 2);\n   grid1-&gt;addWidget(split_B, 5, 3, 1, 1);\n\n   grid1-&gt;addWidget(hline, 6, 1, 1, 3);\n\n   grid1-&gt;addWidget(label_A2, 7, 1);\n   grid1-&gt;addWidget(textFormat, 7, 2, 1, 1);\n\n   grid1-&gt;addWidget(label_B2, 8, 1);\n   grid1-&gt;addWidget(arg_1, 8, 2, 1, 1);\n\n   grid1-&gt;addWidget(label_C2, 9, 1);\n   grid1-&gt;addWidget(arg_2, 9, 2, 1, 1);\n\n   grid1-&gt;addWidget(label_D2, 10, 2);\n   grid1-&gt;addWidget(result, 10, 3, 1, 1);\n\n   grid1-&gt;addItem(new QSpacerItem(100, 0,\n      QSizePolicy::Fixed, QSizePolicy::Fixed), 4, 3);\n\n   grid1-&gt;addItem(new QSpacerItem(0, 0,\n      QSizePolicy::Expanding, QSizePolicy::Expanding), 13, 3);\n\n   QHBoxLayout *layout1 = new QHBoxLayout();\n   layout1-&gt;addStretch();\n   layout1-&gt;addWidget(pb_run);\n   layout1-&gt;addSpacing(20);\n   layout1-&gt;addWidget(pb_close);\n   layout1-&gt;addStretch();\n\n   QVBoxLayout *layoutMain = new QVBoxLayout(this);\n   layoutMain-&gt;setContentsMargins(20, 40, 30, 15);\n   layoutMain-&gt;addLayout(grid1);\n   layoutMain-&gt;addLayout(layout1);\n\n   connect(pb_run,   &amp;QPushButton::clicked, \n      this, &amp;MainWindow::parseText);\n\n   connect(pb_close, &amp;QPushButton::clicked, \n      this, &amp;QWidget::close);\n\n   parseText();\n}\n<\/pre><\/div>\n\n\n<p>There are two parts to this example. The first part shows a textEdit string declared on line 9 with a default sentence which can be modified at run time. Lines 12 and 15 are text labels which indicate how this input text should be parsed. The combo boxes for <code><strong>split_A<\/strong><\/code> and <strong><code>split_B<\/code><\/strong> display the results after calling the QStringParser::split() method. <br><br>In the second part a new text string is declared on line 23 with two markers, %1 and %2. The percent sign must be present and there is a limit of 99 different markers in a single body of text. A marker can be repeated any number of times within the text. The values of our text fields <code><strong>arg_1<\/strong><\/code> and <code><strong>arg_2<\/strong><\/code> will be used to replace the markers by QStringParser::formatArg(). <br><br>&#8211; If you modify the textFormat string at run time and remove the %1 marker, the value for arg_1 will be used for the %2 maker. The value for arg_2 will be ignored.<br><br>&#8211; If you use %1, %2, and %3 no substitution will occur for the last marker and the text &#8220;%3&#8221; will appear in the output. The original string must be changed in the source code to add a third marker. <br><br>After modifying the input text fields, click the &#8220;Run QStringParser&#8221; push button to update the displayed results.  <\/p>\n\n\n\n<h3>Signal \/ Slot Connections<\/h3>\n\n\n\n<p>When the <strong><span style=\"color:#6a5acd\" class=\"has-inline-color\">Run QStringParse<\/span>r<\/strong> push button is clicked the following slot method will be invoked. The connection is set up on line 95. This method is invoked directly at the end of the constructor to populate the result fields.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; highlight: [8,15,19]; title: ; notranslate\" title=\"\">\nvoid MainWindow::parseText()\n{\n   \/\/ part 1\n   QString text = textEdit-&gt;text();\n\n   QStringList list = QStringParser::split(text, ' ');\n   split_A-&gt;clear();\n   split_A-&gt;addItems(list);\n\n   list = QStringParser::split(text,\n      QRegularExpression(&quot;(fox|pig|blue)&quot;, \n      QPatternOption::CaseInsensitiveOption));\n\n   split_B-&gt;clear();\n   split_B-&gt;addItems(list);\n\n   \/\/ part 2\n   QString str = textFormat-&gt;text();\n   result-&gt;setText(str.formatArg(arg_1-&gt;text())\n      .formatArg(arg_2-&gt;text()));\n}\n<\/pre><\/div>\n\n\n<p>This block of code is where the QStringParser methods are called to parse the two sample strings. On line 4 the value for <strong><code>textEdit<\/code><\/strong> is retrieved. The call to split() on line 6 returns a list of strings which is created by breaking up the original string after each &#8220;space&#8221;. The result is added to the combo box split_A on line 8.<br><br>On line 10 a regex is used to indicate where the original string should be split apart. The enum on line 12 indicates the regular expression should be case-insensitive, the default is case-sensitive. This result is added to combo box split_B on line 15.<br><br>In part two <strong><code>textFormat<\/code><\/strong> is retrieved and the %1 and %2 markers are replaced by the values in <strong><code>arg_1<\/code><\/strong> and <strong><code>arg_2<\/code><\/strong> using the method formatArg().<\/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_38&#8221;. <\/p>\n\n\n\n<p><a href=\"https:\/\/download.copperspice.com\/journal\/example_38.zip\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/download.copperspice.com\/journal\/example_38.zip<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The QStringParser class provides a variety of methods to process strings of text. This example will show how to use split() and formatArg().<\/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\/1901"}],"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=1901"}],"version-history":[{"count":80,"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=\/wp\/v2\/posts\/1901\/revisions"}],"predecessor-version":[{"id":2384,"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=\/wp\/v2\/posts\/1901\/revisions\/2384"}],"wp:attachment":[{"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}