#include <QApplication>
2 #include <QHBoxLayout>
3 #include <QSlider>
4 #include <QSpinBox>
5 int main(int argc, char *argv[])
6 {
7 QApplication app(argc, argv);
8 QWidget *window = new QWidget;
9 window->setWindowTitle("Enter Your Age");//set up the QWidget that will serve as the application's main window.
10 QSpinBox *spinBox = new QSpinBox;
11 QSlider *slider = new QSlider(Qt::Horizontal);
12 spinBox->setRange(0, 130);
13 slider->setRange(0, 130);//create a QSpinBox and a QSlider,
/*these widgets should have window as their parent, but it isn't necessary here because the layout system will figure this out by itself and automatically set the parent of the spin box and the slider*/
14 QObject::connect(spinBox, SIGNAL(valueChanged(int)),
15 slider, SLOT(setValue(int)));
16 QObject::connect(slider, SIGNAL(valueChanged(int)),
17 spinBox, SLOT(setValue(int)));//A a =new A...; B b = new B...; (a, signal, b, action); (b,signal,a,action)
18 spinBox->setValue(35);
/*QSpinBox emits the valueChanged(int) signal with an int argument of 35.QSlider's setValue(int) slot,sets the slider value to 35.The slider then emits the valueChanged(int) signal because its own value changed, triggering the spin box's setValue(int) slot. But at this point, setValue(int) doesn't emit any signal, since the spin box value is already 35. This prevents infinite recursion.*/
19 QHBoxLayout *layout = new QHBoxLayout;
/*A layout manager is an object that sets the size and position of the widgets that lie under its responsibility.Qt has three main layout manager classes:QHBoxLayout,QVBoxLayout,QGridLayout lays out widgets in a grid.*/
20 layout->addWidget(spinBox);
21 layout->addWidget(slider);
22 window->setLayout(layout);//installs the layout manager on the window
23 window->show();
24 return app.exec();
25 }