Skip to content

CopperSpice Journal

CopperSpice Journal

Source Code  (Gui Example 21)

Posted on April 22, 2022April 27, 2022 By Barbara Geller

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.

#include <QtCore>
#include <QtGui>

class MainWindow : public QWidget
{
 public:
   MainWindow(); 
};

The class for MainWindow simply declares a constructor.

MainWindow::MainWindow()
{
   setMinimumSize(500, 600);

   QLabel *label = new QLabel();
   label->setText("Label with a Frame");
   label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
   label->setFrameStyle(QFrame::Panel | QFrame::Raised);
   label->setLineWidth(3);
   label->setMinimumSize(25, 30);

   auto *frame1 = new QFrame();
   frame1->setFrameStyle(QFrame::Box);
   frame1->setLineWidth(3);
   frame1->setMidLineWidth(2);
   frame1->setMinimumSize(25, 30);

   auto *frame2 = new QFrame(this);
   frame2->setFrameStyle(QFrame::Box | QFrame::Raised);
   frame2->setLineWidth(3);
   frame2->setMidLineWidth(2);
   frame2->setMinimumSize(25, 30);

   auto *frame3 = new QFrame(this);
   frame3->setFrameStyle(QFrame::Box | QFrame::Sunken);
   frame3->setLineWidth(3);
   frame3->setMidLineWidth(2);
   frame3->setMinimumSize(25, 30);

   QPushButton *pb_close = new QPushButton();
   pb_close->setText("Close");

   QGridLayout *grid = new QGridLayout();
   grid->setVerticalSpacing(20);

   grid->addWidget(label);
   grid->addWidget(frame1);
   grid->addWidget(frame2);
   grid->addWidget(frame3);

   QHBoxLayout *layout = new QHBoxLayout();
   layout->addStretch();
   layout->addWidget(pb_close);
   layout->addStretch();

   QVBoxLayout *layout_main = new QVBoxLayout(this);
   layout_main->addLayout(grid);
   layout_main->addSpacing(25);
   layout_main->addLayout(layout);

   connect(pb_close, &QPushButton::clicked, 
      this, &QWidget::close);
}

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.

A frame is defined by calling setFrameStyle() and passing enum values to set the frame shape and the frame Shadow. The shape and shadow can also be set independently calling setFrameShape() and setFrameShadow().

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.

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.

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.

Main Function

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.

Running the Example

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 “example_1” to “example_21”.

https://download.copperspice.com/journal/example_21.zip

Uncategorized

Post navigation

Previous Post: Source Code  (Gui Example 20)
Next Post: Source Code  (Gui Example 22)
  • CopperSpice Journal Homepage
    • Table of Contents
    • Example Source Code
    • Discussion Forum
  • CopperSpice Homepage
  • Github Repository
  • Videos About C++

Post comments or questions on our CopperSpice Forum

Copyright © 2021-2025 CopperSpice