#include <QApplication>
#include <QHBoxLayout>
#include <QSlider>
#include <QSpinBox>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget * Widget = new QWidget;
Widget->setWindowTitle("Enter your age ");
QSlider *slider = new QSlider(Qt::Horizontal);
QSpinBox *spinbox = new QSpinBox;
slider->setRange(0,150);
spinbox->setRange(0,150);
QObject::connect(spinbox,SIGNAL(valueChanged(int)), QT4
slider,SLOT(setValue(int)));
QObject::connect(slider,SIGNAL(valueChanged(int)),
spinbox,SLOT(setValue(int)));
// QObject::connect(slider, &QSlider::valueChanged, spinbox, &QSpinBox::setValue); //QT5
// void (QSpinBox:: *spinBoxSignal)(int) = &QSpinBox::valueChanged;
// QObject::connect(spinbox, spinBoxSignal,slider, &QSlider::setValue);
spinbox->setValue(40);
QHBoxLayout *layout =new QHBoxLayout;
layout->addWidget(spinbox);
layout->addWidget(slider);
Widget->setLayout(layout);
Widget->show();
return a.exec();
}
示例如图