//几个widget间通信
#include <QApplication>
#include <QFont>
#include <QLCDNumber>
#include <QPushButton>
#include <QSlider>
#include <QVBoxLayout>
#include <QWidget>

//创建自定义Widget类
class MyWidget : public QWidget
{
public:
        MyWidget(QWidget *parent = 0);
};

MyWidget::MyWidget(QWidget *parent)
        : QWidget(parent)
{
        QPushButton *quit = new QPushButton(tr("Quit"));
        quit->setFont(QFont("Times", 18, QFont::Bold));

        QLCDNumber *lcd = new QLCDNumber(3);//3位数字
        lcd->setSegmentStyle(QLCDNumber::Flat);//设置LCD数字的显示样式

        QSlider *slider = new QSlider(Qt::Horizontal);
        slider->setRange(0, 999);//设置Slider的范围0-999
        slider->setValue(0);//设置Slider的位置

  //quit按钮负责程序退出
        connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
        
        //连接slider的valueChanged信号和lcd的display槽
        connect(slider, SIGNAL(valueChanged(int)),
                        lcd, SLOT(display(int)));

        QVBoxLayout *layout = new QVBoxLayout;//垂直布局
        layout->addWidget(quit);//添加子部件
        layout->addWidget(lcd);
        layout->addWidget(slider);
        
        setLayout(layout);//设置布局
}

int main(int argc, char *argv[])
{
        QApplication app(argc, argv);
        MyWidget widget;
        widget.show();
        return app.exec();
}
 
截图: