Qt 5.10 Book Examples
main.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // main.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 
13 // ----------------------------------------------------------------------
14 int main(int argc, char** argv)
15 {
16  QApplication app(argc, argv);
17  QWidget wgt;
18  wgt.setFixedSize(300, 50);
19  wgt.show();
20 
21  QLabel* plblOff = new QLabel("Off");
22  QLabel* plblOn = new QLabel("On");
23 
24  QHBoxLayout* phbx = new QHBoxLayout;
25  phbx->addWidget(plblOn);
26  phbx->addStretch(1);
27  phbx->addWidget(plblOff);
28  wgt.setLayout(phbx);
29 
30  QPushButton* pcmd = new QPushButton("Push", &wgt);
31  pcmd->setAutoFillBackground(true);
32  pcmd->show();
33 
34  int nButtonWidth = wgt.width() / 2;
35 
36  QStateMachine* psm = new QStateMachine;
37 
38  QState* pStateOff = new QState(psm);
39  QRect rect1(0, 0, nButtonWidth, wgt.height());
40  pStateOff->assignProperty(pcmd, "geometry", rect1);
41  pStateOff->assignProperty(plblOff, "visible", true);
42  pStateOff->assignProperty(plblOn, "visible", false);
43  psm->setInitialState(pStateOff);
44 
45  QState* pStateOn = new QState(psm);
46  QRect rect2(nButtonWidth, 0, nButtonWidth, wgt.height());
47  pStateOn->assignProperty(pcmd, "geometry", rect2);
48  pStateOn->assignProperty(plblOff, "visible", false);
49  pStateOn->assignProperty(plblOn, "visible", true);
50 
51  QSignalTransition* ptrans1 =
52  pStateOff->addTransition(pcmd, SIGNAL(clicked()), pStateOn);
53 
54  QSignalTransition* ptrans2 =
55  pStateOn->addTransition(pcmd, SIGNAL(clicked()), pStateOff);
56 
57  QPropertyAnimation* panim1 =
58  new QPropertyAnimation(pcmd, "geometry");
59  ptrans1->addAnimation(panim1);
60 
61  QPropertyAnimation* panim2 =
62  new QPropertyAnimation(pcmd, "geometry");
63  ptrans2->addAnimation(panim2);
64 
65  psm->start();
66 
67  return app.exec();
68 }
int main(int argc, char **argv)
Definition: main.cpp:15