QT提供布局管理器来(自动)调整组件大小和位置。
例如当你拉伸窗口时,组件大小也一起拉伸、调整位置。
#include "mainwindow.h"
#include <QApplication>
#include<QSpinBox>
#include<QSlider>
#include<QBoxLayout>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget window;
window.setWindowTitle("Enter your age");
QSpinBox *spinBox = new QSpinBox(&window);
QSlider *slider = new QSlider(Qt::Horizontal, &window);
spinBox->setRange(0, 130);
slider->setRange(0, 130);
QObject::connect(slider, &QSlider::valueChanged, spinBox, &QSpinBox::setValue);
void (QSpinBox:: *spinBoxSignal)(int) = &QSpinBox::valueChanged;
QObject::connect(spinBox, spinBoxSignal, slider, &QSlider::setValue);
spinBox->setValue(35);
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(spinBox);
layout->addWidget(slider);
window.setLayout(layout);
window.show();
return a.exec();
}