Qt 5.10 Book Examples
Buttons.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // Buttons.cpp
3 // ======================================================================
4 // This file is a part of the book
5 // "Qt 5.10 Professional programming with C++"
6 // http://qt-book.com
7 // ======================================================================
8 // Copyright (c) 2017 by Max Schlee
9 // ======================================================================
10 
11 #include <QtWidgets>
12 #include "Buttons.h"
13 
14 // ----------------------------------------------------------------------
15 Buttons::Buttons(QWidget* pwgt/*= 0*/) : QGroupBox("QGroupBox", pwgt)
16 {
17  resize(100, 150);
18  setCheckable(true);
19  setChecked(true);
20 
21  m_pradRed = new QRadioButton("&Red");
22  m_pradGreen = new QRadioButton("&Green");
23  m_pradBlue = new QRadioButton("&Blue");
24  m_pradGreen->setChecked(true);
25  connect(m_pradRed, SIGNAL(clicked()), SLOT(slotButtonClicked()));
26  connect(m_pradGreen, SIGNAL(clicked()), SLOT(slotButtonClicked()));
27  connect(m_pradBlue, SIGNAL(clicked()), SLOT(slotButtonClicked()));
28 
29  m_pchk = new QCheckBox("&Light");
30  m_pchk->setChecked(true);
31  connect(m_pchk, SIGNAL(clicked()), SLOT(slotButtonClicked()));
32 
33  QPushButton* pcmd = new QPushButton("&Exit");
34  connect(pcmd, SIGNAL(clicked()), qApp, SLOT(quit()));
35 
36  //Layout setup
37  QVBoxLayout* pvbxLayout = new QVBoxLayout;
38  pvbxLayout->addWidget(m_pradRed);
39  pvbxLayout->addWidget(m_pradGreen);
40  pvbxLayout->addWidget(m_pradBlue);
41  pvbxLayout->addWidget(m_pchk);
42  pvbxLayout->addWidget(pcmd);
43  setLayout(pvbxLayout);
44 
46 }
47 
48 // ----------------------------------------------------------------------
50 {
51  QPalette pal = palette();
52  int nLight = m_pchk->isChecked() ? 150 : 80;
53  if(m_pradRed->isChecked()) {
54  pal.setColor(backgroundRole(), QColor(Qt::red).light(nLight));
55  }
56  else if(m_pradGreen->isChecked()) {
57  pal.setColor(backgroundRole(), QColor(Qt::green).light(nLight));
58  }
59  else {
60  pal.setColor(backgroundRole(), QColor(Qt::blue).light(nLight));
61  }
62  setPalette(pal);
63 }
64 
void slotButtonClicked()
Definition: Buttons.cpp:49
Buttons(QWidget *pwgt=0)
Definition: Buttons.cpp:15