Qt 5.10 Book Examples
MyProgram.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // MyProgram.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 "MyProgram.h"
13 
14 // ----------------------------------------------------------------------
15 MyProgram::MyProgram(QWidget* pwgt/*=0*/)
16  : QWidget(pwgt)
17  , m_settings("BHV","MyProgram")
18 {
19  m_plbl = new QLabel;
20  m_ptxt = new QTextEdit;
21  m_pcbo = new QComboBox;
22  m_pchk = new QCheckBox("Disable edit");
23 
24  m_pcbo->addItem("Classic");
25  m_pcbo->addItem("Borland");
26 
27 
28  connect(m_pchk, SIGNAL(clicked()), SLOT(slotCheckBoxClicked()));
29  connect(m_pcbo,
30  SIGNAL(activated(int)),
31  SLOT(slotComboBoxActivated(int))
32  );
33  readSettings();
34 
35  //Layout setup
36  QVBoxLayout* pvbxLayout = new QVBoxLayout;
37  QHBoxLayout* phbxLayout = new QHBoxLayout;
38 
39  pvbxLayout->setContentsMargins(5, 5, 5, 5);
40  phbxLayout->setSpacing(15);
41  pvbxLayout->setSpacing(15);
42  pvbxLayout->addWidget(m_plbl);
43  pvbxLayout->addWidget(m_ptxt);
44  phbxLayout->addWidget(m_pcbo);
45  phbxLayout->addWidget(m_pchk);
46 
47  pvbxLayout->addLayout(phbxLayout);
48  setLayout(pvbxLayout);
49 
50 }
51 
52 
53 // ----------------------------------------------------------------------
55 {
56  writeSettings();
57 }
58 
59 // ----------------------------------------------------------------------
61 {
62  m_settings.beginGroup("/Settings");
63 
64  QString strText = m_settings.value("/text", "").toString();
65  int nWidth = m_settings.value("/width", width()).toInt();
66  int nHeight = m_settings.value("/height", height()).toInt();
67  int nComboItem = m_settings.value("/highlight", 0).toInt();
68  bool bEdit = m_settings.value("/edit", false).toBool();
69 
70  m_nCounter = m_settings.value("/counter", 1).toInt();
71 
72  QString str = QString().setNum(m_nCounter++);
73  m_plbl->setText("This program has been started " + str + " times");
74 
75  m_ptxt->setPlainText(strText);
76 
77  resize(nWidth, nHeight);
78 
79  m_pchk->setChecked(bEdit);
81 
82  m_pcbo->setCurrentIndex(nComboItem);
83  slotComboBoxActivated(nComboItem);
84 
85  m_settings.endGroup();
86 }
87 
88 // ----------------------------------------------------------------------
90 {
91  m_settings.beginGroup("/Settings");
92  m_settings.setValue("/counter", m_nCounter);
93  m_settings.setValue("/text", m_ptxt->toPlainText());
94  m_settings.setValue("/width", width());
95  m_settings.setValue("/height", height());
96  m_settings.setValue("/highlight", m_pcbo->currentIndex());
97  m_settings.setValue("/edit", m_pchk->isChecked());
98  m_settings.endGroup();
99 }
100 
101 // ----------------------------------------------------------------------
103 {
104  m_ptxt->setEnabled(!m_pchk->isChecked());
105 }
106 
107 // ----------------------------------------------------------------------
109 {
110  QPalette pal = m_ptxt->palette();
111  pal.setColor(QPalette::Active,
112  QPalette::Base,
113  n ? Qt::darkBlue : Qt::white
114  );
115  pal.setColor(QPalette::Active,
116  QPalette::Text,
117  n ? Qt::yellow : Qt::black
118  );
119  m_ptxt->setPalette(pal);
120 }
virtual ~MyProgram()
Definition: MyProgram.cpp:54
void slotCheckBoxClicked()
Definition: MyProgram.cpp:102
MyProgram(QWidget *pwgt=0)
Definition: MyProgram.cpp:15
void slotComboBoxActivated(int)
Definition: MyProgram.cpp:108
void readSettings()
Definition: MyProgram.cpp:60
void writeSettings()
Definition: MyProgram.cpp:89