Qt——布局管理器

教程地址

运行截图:

代码:

#include "mainwindow.h"
#include <QApplication>
#include <QHBoxLayout>
#include <QWidget>
#include <QSpinBox>
#include <QObject>
#include <QHBoxLayout>
#include <QSlider>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QWidget window;
    window.setWindowTitle("Enter your age");

    //QSpinBox是只能输入数字的输入框,并且带有上下箭头的步进按钮
    QSpinBox *spinBox = new QSpinBox(&window);
    //QSlider是带有滑块的滑竿
    QSlider *slider = new QSlider(Qt::Horizontal, &window);
    spinBox->setRange(0, 130);
    slider->setRange(0, 130);

    //将 slider 的valueChanged()信号同 spinBox 的setValue()函数相连
    QObject::connect(slider, &QSlider::valueChanged, spinBox, &QSpinBox::setValue);
    //显式指定valueChanged函数,指定其参数为int
    void (QSpinBox:: *spinBoxSignal)(int) = &QSpinBox::valueChanged;
    QObject::connect(spinBox, spinBoxSignal, slider, &QSlider::setValue);
    spinBox->setValue(19);

    //布局管理器
    QHBoxLayout *layout = new QHBoxLayout;
    layout->addWidget(spinBox);
    layout->addWidget(slider);
    window.setLayout(layout);

    window.show();

    return app.exec();
}

 

posted @ 2017-07-15 17:53  Not-Bad  阅读(484)  评论(0编辑  收藏  举报