1. Stack layout Manager (qstackedlayout)
(1) All components are managed in the direction perpendicular to the screen
(2) only one component is displayed on the screen at a time (similar to the window's z-order, but only the topmost)
(3) Only the topmost component will be displayed.
2. features of the stack layout manager
(1) Component size consistent and full of parent component display Area
(2) You cannot nest other layout managers directly, but you can put some components in one layout and use this layout manager as a widget. Finally through the qstackedlayout.addwidget to achieve the purpose of nesting.
(3) Ability to freely switch the components that need to be displayed
(4) Only one component can be displayed at a time
3. Summary of usage of qstackedlayout
(1) int addwidget (qwidget* widget);
(2) qwidget* currentwidget ();
(3) void SetCurrentIndex (int index);
(4) int currentindex ();
4. The concept and usage of the timer:
(1) The concept of a timer
① Timer is a very important role in engineering development.
The ② timer is used to trigger a message every certain time
③ Timer messages will eventually be converted to function calls
④ macro, the timer invokes the specified function at each interval
(2) How to use
① writing Timer Message handler functions
② Creating a Timer object in a program
③ Connection timer messages and message handler functions
④ Set timer interval and start timing
Use of the "programming experiment" stack layout and timers
Main.cpp
#include <QApplication>"Widget.h"int main (int Char *argv[]) { qapplication A (argc, argv); Widget W; W.show (); return a.exec ();}
View Code
Widget.h
#ifndef _widget_h_#define_widget_h_#include<QWidget>#include<QPushButton>classWidgets: Publicqwidget{Q_objectPrivate: Qpushbutton TestBtn1; Qpushbutton TestBtn2; Qpushbutton TestBtn3; Qpushbutton TestBtn4; voidInitcontrol ();PrivateSlots:voidtimertimeout (); Public: Widgets (Qwidget* Parent =0); ~Widget ();};#endif //_widget_h_
Widget.cpp
#include"Widget.h"#include<QStackedLayout>#include<QHBoxLayout>#include<QTimer>#include<QDebug>Widget::widget (Qwidget*parent): Qwidget (parent), TESTBTN1 ( This), TESTBTN2 ( This), TestBtn3 ( This), TestBtn4 ( This) {Initcontrol ();}voidWidget::initcontrol () {qstackedlayout* Slayout =Newqstackedlayout (); Qhboxlayout* Hlayout =Newqhboxlayout (); Qwidget* Widget =NewQwidget (); Qtimer* Timer =NewQtimer ( This); Testbtn1.settext ("1st Button"); Testbtn2.settext ("2nd Button"); Testbtn3.settext ("3rd Button"); Testbtn4.settext ("4rd Button:hello world!"); //in order to achieve qstackedlayout can be nested, first new to a new widget//It contains 2 buttons, and does the horizontal layout of the dischargetestbtn2.setparent (widgets); Testbtn3.setparent (widgets); Hlayout->addwidget (&TestBtn2); Hlayout->addwidget (&TestBtn3); Widgets-setlayout (hlayout); //Building individual components of a stacked layoutSlayout->addwidget (&TESTBTN1);//0Slayout->addwidget (widgets);//1, nested widgetsSlayout->addwidget (&TESTBTN4);//2Slayout->setcurrentindex (0); SetLayout (slayout); Connect (timer, SIGNAL, timeout ()), This, SLOT (Timertimeout ())); Timer->start ( -);}voidwidget::timertimeout () {//Remove the widget's current layout managerqstackedlayout* slayout = dynamic_cast<qstackedlayout*>(layout ()); if(Slayout! =NULL) { intindex = (Slayout->currentindex () +1)% slayout->count (); Slayout-SetCurrentIndex (index); }}widget::~Widget () {}
5. Summary
(1) Qstackedlayout Manage interface components in stacks
(2) only one display of the components in Qstackedlayout
(3) Qstackedlayout can freely switch the components that need to be displayed
(4) Qtimer is a timer component in QT
(5) Qtimer can trigger a message at a specified time interval
25th Lesson Layout Manager (iv)