{"id":1126,"date":"2022-03-15T09:00:45","date_gmt":"2022-03-15T17:00:45","guid":{"rendered":"https:\/\/journal.copperspice.com\/?p=1126"},"modified":"2022-04-11T08:55:43","modified_gmt":"2022-04-11T16:55:43","slug":"source-code-gui-example-18","status":"publish","type":"post","link":"https:\/\/journal.copperspice.com\/?p=1126","title":{"rendered":"Source Code &nbsp;(Gui Example 18)"},"content":{"rendered":"\n<p>The code in this example compares the three built in GUI widgets which are used to edit text. Each of these controls offers slightly different functionality and features.<\/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<\/pre><\/div>\n\n\n<p>The only required declaration is the MainWindow() constructor.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; highlight: [8,14,28]; title: ; notranslate\" title=\"\">\nMainWindow::MainWindow()\n{\n   setMinimumSize(600, 400);\n\n   \/\/\n   QLabel *label_1 = new QLabel(&quot;Line Edit:&quot;);\n\n   QLineEdit *text_1 = new QLineEdit();\n   text_1-&gt;setText(&quot;Single line of text&quot;);\n\n   \/\/\n   QLabel *label_2 = new QLabel(&quot;Text Edit:&quot;);\n\n   QTextEdit *text_2 = new QTextEdit();\n   text_2-&gt;setText(\n        &quot;Multiple lines using html text formatting.&quot;\n        &quot;&lt;br&gt;&quot;\n        &quot;&lt;span style=\\&quot;color:#aa00ff;\\&quot;&gt;&quot;\n             &quot;Show text in magenta&lt;\/span&gt;&quot;\n        &quot;&lt;br&gt;&quot;\n        &quot;&lt;span style=\\&quot;font-size:10pt;\\&quot;&gt;&quot;\n             &quot;Set 10 pt font&lt;\/span&gt;&quot;);\n   text_2-&gt;setMaximumHeight(100);\n\n   \/\/\n   QLabel *label_3 = new QLabel(&quot;Plain Text:&quot;);\n\n   QPlainTextEdit *text_3 = new QPlainTextEdit();\n   text_3-&gt;setPlainText(&quot;Multiple lines, no formatting&quot;);\n   text_3-&gt;setMaximumHeight(100);\n\n   QPushButton *pb_1 = new QPushButton();\n   pb_1-&gt;setText(&quot;Close&quot;);\n\n   QGridLayout *layout1 = new QGridLayout();\n   layout1-&gt;setVerticalSpacing(20);\n\n   layout1-&gt;addWidget(label_1, 0, 1);\n   layout1-&gt;addWidget(text_1, 0, 2);\n   layout1-&gt;addWidget(label_2, 1, 1, Qt::AlignTop);\n   layout1-&gt;addWidget(text_2, 1, 2);\n   layout1-&gt;addWidget(label_3, 2, 1, Qt::AlignTop);\n   layout1-&gt;addWidget(text_3, 2, 2);\n\n   QHBoxLayout *layout2 = new QHBoxLayout();\n   layout2-&gt;addStretch();\n   layout2-&gt;addWidget(pb_1);\n   layout2-&gt;addStretch();\n\n   QVBoxLayout *layoutMain = new QVBoxLayout(this);\n   layoutMain-&gt;setContentsMargins(45, 35, 45, 15);\n\n   layoutMain-&gt;addLayout(layout1);\n   layoutMain-&gt;addSpacing(20);\n   layoutMain-&gt;addLayout(layout2);\n\n   connect(pb_1, &amp;QPushButton::clicked, \n         this, &amp;QWidget::close);\n}   \n<\/pre><\/div>\n\n\n<p>There are three different widgets which can be used to add or edit text in a GUI application. The QLineEdit class declared on line 8 is the most basic control. It allows the user to enter a single line of non-formatted text. This is the most commonly used text edit control. <\/p>\n\n\n\n<p>On line 14 is the declaration for a QTextEdit widget. This control offers the most features and flexibility. It supports multiple lines of text and a subset of HTML tags like line breaks, span, lists, and tables. For a full list of the supported formatting commands, refer to the <a href=\"https:\/\/www.copperspice.com\/docs\/cs_api\/richtext-html-subset.html\">API documentation<\/a>. <br><br>The QPlainTextEdit declared on line 28 is also multi line however you can not specify any formatting commands. <\/p>\n\n\n\n<p>These widgets can be used on any user interface form in any combination. <\/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_18&#8221;. <\/p>\n\n\n\n<p><a href=\"https:\/\/download.copperspice.com\/journal\/example_18.zip\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/download.copperspice.com\/journal\/example_18.zip<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The code in this example compares the three built in GUI widgets which are used to edit text. Each of these controls offers slightly different functionality and features.<\/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\/1126"}],"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=1126"}],"version-history":[{"count":19,"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=\/wp\/v2\/posts\/1126\/revisions"}],"predecessor-version":[{"id":1305,"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=\/wp\/v2\/posts\/1126\/revisions\/1305"}],"wp:attachment":[{"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}