{"id":1313,"date":"2022-04-22T09:00:45","date_gmt":"2022-04-22T17:00:45","guid":{"rendered":"https:\/\/journal.copperspice.com\/?p=1313"},"modified":"2022-04-27T15:48:59","modified_gmt":"2022-04-27T23:48:59","slug":"source-code-gui-example-21","status":"publish","type":"post","link":"https:\/\/journal.copperspice.com\/?p=1313","title":{"rendered":"Source Code &nbsp;(Gui Example 21)"},"content":{"rendered":"\n<p>This example shows several different frame styles which can be added to a variety of GUI widgets. A frame can also be used to group a collection of related widgets. The purpose of using a frame is to change the visual appearance of the border around a single widget or a group of widgets.<\/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 class for MainWindow simply declares a constructor.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; highlight: [12,18,24]; title: ; notranslate\" title=\"\">\nMainWindow::MainWindow()\n{\n   setMinimumSize(500, 600);\n\n   QLabel *label = new QLabel();\n   label-&gt;setText(&quot;Label with a Frame&quot;);\n   label-&gt;setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);\n   label-&gt;setFrameStyle(QFrame::Panel | QFrame::Raised);\n   label-&gt;setLineWidth(3);\n   label-&gt;setMinimumSize(25, 30);\n\n   auto *frame1 = new QFrame();\n   frame1-&gt;setFrameStyle(QFrame::Box);\n   frame1-&gt;setLineWidth(3);\n   frame1-&gt;setMidLineWidth(2);\n   frame1-&gt;setMinimumSize(25, 30);\n\n   auto *frame2 = new QFrame(this);\n   frame2-&gt;setFrameStyle(QFrame::Box | QFrame::Raised);\n   frame2-&gt;setLineWidth(3);\n   frame2-&gt;setMidLineWidth(2);\n   frame2-&gt;setMinimumSize(25, 30);\n\n   auto *frame3 = new QFrame(this);\n   frame3-&gt;setFrameStyle(QFrame::Box | QFrame::Sunken);\n   frame3-&gt;setLineWidth(3);\n   frame3-&gt;setMidLineWidth(2);\n   frame3-&gt;setMinimumSize(25, 30);\n\n   QPushButton *pb_close = new QPushButton();\n   pb_close-&gt;setText(&quot;Close&quot;);\n\n   QGridLayout *grid = new QGridLayout();\n   grid-&gt;setVerticalSpacing(20);\n\n   grid-&gt;addWidget(label);\n   grid-&gt;addWidget(frame1);\n   grid-&gt;addWidget(frame2);\n   grid-&gt;addWidget(frame3);\n\n   QHBoxLayout *layout = new QHBoxLayout();\n   layout-&gt;addStretch();\n   layout-&gt;addWidget(pb_close);\n   layout-&gt;addStretch();\n\n   QVBoxLayout *layout_main = new QVBoxLayout(this);\n   layout_main-&gt;addLayout(grid);\n   layout_main-&gt;addSpacing(25);\n   layout_main-&gt;addLayout(layout);\n\n   connect(pb_close, &amp;QPushButton::clicked, \n      this, &amp;QWidget::close);\n}\n<\/pre><\/div>\n\n\n<p>A frame can be drawn around widgets like a QLabel or QTextEdit.  Both of these widgets inherit from the QFrame class so adding a frame simply involves calling a few methods from the QFrame class. <\/p>\n\n\n\n<p>A frame is defined by calling<strong> <\/strong>setFrameStyle() and passing enum values to set the f<span style=\"color:#6a5acd\" class=\"has-inline-color\"><strong>rame shape<\/strong><\/span> and the <strong><span style=\"color:#6a5acd\" class=\"has-inline-color\">frame Shadow<\/span><\/strong>.  The shape and shadow can also be set independently calling setFrameShape() and setFrameShadow().<\/p>\n\n\n\n<p>On line 5 a new QLabel widget is created and then on line 8 the frame shape is set to Panel and the frame shadow is set to Raised. The call to setLineWidth() on line 9 defines the thickness of the frame style.<\/p>\n\n\n\n<p>On lines 12, 18, and 24 three different frames are declared to show various other combinations. All three of these use a frame shape of Box. The frame shadows are Plain (which is the default), Raised, and Sunken. <\/p>\n\n\n\n<p>The label and three frame widgets are added to a Grid. The grid and the close push button are then  added to a vertical layout.  <\/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_21&#8221;. <\/p>\n\n\n\n<p><a href=\"https:\/\/download.copperspice.com\/journal\/example_21.zip\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/download.copperspice.com\/journal\/example_21.zip<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This example shows several different frame styles which can be added to a variety of GUI widgets. A frame can also be used to group a collection of related widgets. The purpose of using a frame is to change the visual appearance of the boarder around a single widget or a group of widgets.<\/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\/1313"}],"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=1313"}],"version-history":[{"count":11,"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=\/wp\/v2\/posts\/1313\/revisions"}],"predecessor-version":[{"id":1326,"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=\/wp\/v2\/posts\/1313\/revisions\/1326"}],"wp:attachment":[{"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}