{"id":375,"date":"2021-12-27T09:00:14","date_gmt":"2021-12-27T09:00:14","guid":{"rendered":"http:\/\/journal.copperspice.com\/?p=375"},"modified":"2022-04-11T07:41:12","modified_gmt":"2022-04-11T15:41:12","slug":"s","status":"publish","type":"post","link":"https:\/\/journal.copperspice.com\/?p=375","title":{"rendered":"Source Code &nbsp;(Gui Example 2)"},"content":{"rendered":"\n<p>Our next example will add a new Push Button. When it is clicked, a built in dialog box will be displayed that allows the user to select a color. <\/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\nint main(int argc, char *argv&#x5B;])\n{\n   QApplication app(argc, argv);\n\n   QWidget *mainWindow = new QWidget();\n   mainWindow-&gt;setMinimumSize(700, 350);\n\n   QPushButton *pb_1 = new QPushButton();\n   pb_1-&gt;setText(&quot;Show Colors&quot;);\n\n   QPushButton *pb_2 = new QPushButton();\n   pb_2-&gt;setText(&quot;Close&quot;);\n\n   QHBoxLayout *layout = new QHBoxLayout(mainWindow);\n   layout-&gt;addStretch();\n   layout-&gt;addWidget(pb_1);\n   layout-&gt;addSpacing(10);\n   layout-&gt;addWidget(pb_2);\n   layout-&gt;addStretch();\n\n   QObject::connect(pb_1, &amp;QPushButton::clicked, \n         pb_1, &#x5B;]() { QColorDialog::getColor(Qt::green); });\n\n   QObject::connect(pb_2, &amp;QPushButton::clicked,\n         mainWindow, &amp;QWidget::close);\n\n   mainWindow-&gt;show();\n\n   return app.exec();\n}\n<\/pre><\/div>\n\n\n<p>Lines 18 through 22 are expanded in this example to center the two push buttons in the window and add a spacing of 10 pixels between the buttons. If the user widens the application window the buttons will automatically stay relative to each other and centered on the screen. <\/p>\n\n\n\n<p>Lines 24 and 25 add a new Signal \/ Slot connection for push button 1. The fourth parameter uses a different syntax than the method pointer syntax for push button 2. Although this syntax looks a bit odd, the end result is to call the getColor() method in the QColorDialog class.  <\/p>\n\n\n\n<p>The code shown below is a C++ lambda expression. The syntax requires a set of square brackets, parentheses, and then a block using curly braces. The source code in the curly braces is what will be invoked.  <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;]() { QColorDialog::getColor(Qt::green); }<\/code><\/pre>\n\n\n\n<p>The following video contains more information about Lambda Expressions.<br><a rel=\"noreferrer noopener\" href=\"https:\/\/www.youtube.com\/watch?v=ZHw2XHij1is\" target=\"_blank\">https:\/\/www.youtube.com\/watch?v=ZHw2XHij1is<\/a> <\/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 say &#8220;example_2&#8221;. <\/p>\n\n\n\n<p><a href=\"https:\/\/download.copperspice.com\/journal\/example_02.zip\">https:\/\/download.copperspice.com\/journal\/example_02.zip<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Our next example will add a new Push Button. When it is clicked, a built in dialog box will be displayed that allows the user to select a color.<\/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\/375"}],"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=375"}],"version-history":[{"count":31,"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=\/wp\/v2\/posts\/375\/revisions"}],"predecessor-version":[{"id":1262,"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=\/wp\/v2\/posts\/375\/revisions\/1262"}],"wp:attachment":[{"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=375"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=375"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=375"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}