We see how to use QHBoxLayout to nicely put the contained widgets in an horizontal box; how to set the title in a window; we get acquainted with the spin box and the slider widget, and we see how to connect them, so that changing the value of one automatically result in the adjustment of the value of the other.
All this stuff in such a short example:
const int MIN_AGE = 0;
const int MAX_AGE = 130;
const int DEF_AGE = 35;
int b103(int argc, char** argv)
QApplication app(argc, argv);
QSpinBox* sb = new QSpinBox(); // 1.
QSlider* sl = new QSlider(Qt::Horizontal); // 2.
QObject::connect(sb, SIGNAL(valueChanged(int)), sl, SLOT(setValue(int))); // 3.
QObject::connect(sl, SIGNAL(valueChanged(int)), sb, SLOT(setValue(int)));
// spin box and slider now are connected
sb->setValue(DEF_AGE); // 4.
QHBoxLayout* layout = new QHBoxLayout(); // 5.
QWidget* w = new QWidget(); // 6.
w->setWindowTitle("Enter Your Age");
1. create and then specify a range for a spinbox widget.
2. create a (horizontal) slider and then specify its range.
3. the connection between two widgets is not different to the connection between a widget an the application. We just specify the object and the method for the emitting widget and then the object and method for the receiving one.
4. now that our two widget are connected we can set a default value for one of them relying on the signal-slot mechanism to let the other being consequentely adjusted.
5. we create a horizontal box and put in it our two widgets.
6. the widget w is the main (and only) window of our application, we set its title calling the setWindowTitle() method, then we set its layout using the one we have just created, and show the result to the user.
I wrote this post as homework while reading "C++ GUI Programming with Qt 4, Second Edition" by Jasmin Blanchette and Mark Summerfield.