{"id":615,"date":"2022-01-20T09:00:47","date_gmt":"2022-01-20T17:00:47","guid":{"rendered":"https:\/\/journal.copperspice.com\/?p=615"},"modified":"2022-04-11T08:08:07","modified_gmt":"2022-04-11T16:08:07","slug":"source-code-gui-example-5","status":"publish","type":"post","link":"https:\/\/journal.copperspice.com\/?p=615","title":{"rendered":"Source Code &nbsp;(Gui Example 5)"},"content":{"rendered":"\n<p>In these next few examples let&#8217;s look at some of the properties for a QLineEdit. This example will show text justification.<\/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 declaration for MainWindow only needs a constructor for this example.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; highlight: [9,14,19]; title: ; notranslate\" title=\"\">\nMainWindow::MainWindow()\n{\n   setMinimumSize(700, 350);\n\n   \/\/ text edit\n   QLineEdit *text_1 = new QLineEdit();\n   text_1-&gt;setText(&quot;Sample text on the Left&quot;);\n   text_1-&gt;setMinimumSize(400, 35);\n   text_1-&gt;setAlignment(Qt::AlignLeft);\n\n   QLineEdit *text_2 = new QLineEdit();\n   text_2-&gt;setText(&quot;Sample text in the Center&quot;);\n   text_2-&gt;setMinimumSize(400, 35);\n   text_2-&gt;setAlignment(Qt::AlignHCenter);\n\n   QLineEdit *text_3 = new QLineEdit();\n   text_3-&gt;setText(&quot;Sample text on the Right&quot;);\n   text_3-&gt;setMinimumSize(400, 35);\n   text_3-&gt;setAlignment(Qt::AlignRight);\n\n   QPushButton *pb_1 = new QPushButton();\n   pb_1-&gt;setText(&quot;Close&quot;);\n\n   QVBoxLayout *layout1 = new QVBoxLayout();\n   layout1-&gt;addSpacing(75);\n   layout1-&gt;addWidget(text_1);\n   layout1-&gt;addSpacing(35);\n   layout1-&gt;addWidget(text_2);\n   layout1-&gt;addSpacing(35);\n   layout1-&gt;addWidget(text_3);\n   layout1-&gt;addSpacing(75);\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;addLayout(layout1);\n   layoutMain-&gt;addSpacing(75);\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>Lines 9, 14, and 19 show how to specify the text should be left justified, centered, or right justified. When running this program you will be able to type in any of these edit fields. Make sure to type in the right justified line edit to see how new text is &#8220;added&#8221; on the right, while the existing text is pushed to the left.<\/p>\n\n\n\n<p>Lines 24 through 41 are used to set the layout of our widgets. The addStretch() and addSpacing() methods specify where the extra white space should appear when the main window is resized by the user. CopperSpice will automatically adjust the size and position of all the widgets. <\/p>\n\n\n\n<h3>Main Function<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nint main(int argc, char *argv&#x5B;])\n{\n   QApplication app(argc, argv);\n\n   MainWindow *window = new MainWindow();\n   window-&gt;show();\n\n   return app.exec();\n}\n<\/pre><\/div>\n\n\n<p>The source code for main() is the same as shown in example 3.<\/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_5&#8221;. <\/p>\n\n\n\n<p><a href=\"https:\/\/download.copperspice.com\/journal\/example_05.zip\">https:\/\/download.copperspice.com\/journal\/example_05.zip<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In these next few examples let&#8217;s look at some of the properties for a QLineEdit.<\/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\/615"}],"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=615"}],"version-history":[{"count":20,"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=\/wp\/v2\/posts\/615\/revisions"}],"predecessor-version":[{"id":1288,"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=\/wp\/v2\/posts\/615\/revisions\/1288"}],"wp:attachment":[{"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=615"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=615"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=615"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}