为我们自定义的类添加信号和槽:
 
lcdrange.h头文件:
 
#ifndef __LCDRANGE_H__
#define __LCDRANGE_H__

#include <QWidget>

class QSlider;//new:提前声明一下

class LCDRange : public QWidget
{
  Q_OBJECT
public:
        LCDRange(QWidget *parent = 0);
        int value() const;//new : 添加一个函数
public slots:
  void setValue(int value);
signals:
  void valueChanged(int newValue);    
private:
  QSlider * slider;//new:改成成员变量
};

#endif // __LCDRANGE_H__
 
lcdrange.cpp类实现文件:
 
#include <QLCDNumber>
#include <QSlider>
#include <QVBoxLayout>


#include "lcdrange.h"

LCDRange::LCDRange(QWidget *parent)
        : QWidget(parent)
{
  //LCD数字
        QLCDNumber *lcd = new QLCDNumber(2);
        lcd->setSegmentStyle(QLCDNumber::Flat);

  //Slider
        slider = new QSlider(Qt::Horizontal);
        slider->setRange(0, 99);
        slider->setValue(0);
        connect(slider, SIGNAL(valueChanged(int)),lcd, SLOT(display(int)));
        
        //new: 新加
        connect(slider,SIGNAL(valueChanged(int)),this,SIGNAL(valueChanged(int)));

  //布局
        QVBoxLayout *layout = new QVBoxLayout;
        layout->addWidget(lcd);
        layout->addWidget(slider);
        setLayout(layout);
}

int LCDRange::value() const
{
  return slider->value();
}

void LCDRange::setValue(int value)
{
  slider->setValue(value);
}
 
mywidget.h头文件:
 
#ifndef __MYWIDGET_H__
#define __MYWIDGET_H__
#include <QWidget>

class MyWidget : public QWidget
{
public:
        MyWidget(QWidget *parent = 0);
};

#endif // __MYWIDGET_H__
 
mywidget.cpp文件:
 
#include <QApplication>
#include <QPushButton>
#include <QFont>
#include <QGridLayout>

#include "lcdrange.h"
#include "mywidget.h"

//MyWidget类实现部分
MyWidget::MyWidget(QWidget *parent)
        : QWidget(parent)
{
  //创建quit按钮
        QPushButton *quit = new QPushButton(tr("Quit"));
        quit->setFont(QFont("Times", 18, QFont::Bold));
        connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
        
        LCDRange* prevRange=0;

  //网格布局
        QGridLayout *grid = new QGridLayout;
        for (int row = 0; row < 3; ++row) {
                for (int column = 0; column < 3; ++column) {
                        LCDRange *lcdRange = new LCDRange;//创建LCD+Slider
                        grid->addWidget(lcdRange, row, column);
                                        
                        //new : 新增
                        if(prevRange)
                        {
                          //如果存在前一个
                          connect(lcdRange,SIGNAL(valueChanged(int)),prevRange,SLOT(setValue(int)));
                        }
                        prevRange=lcdRange;                        
                }
        }

        QVBoxLayout *layout = new QVBoxLayout;
        layout->addWidget(quit);
        layout->addLayout(grid);//子布局
        
        setLayout(layout);//设置布局
}
 
main.cpp主程序文件:
 
//主程序文件
#include <QApplication>
#include "mywidget.h"

int main(int argc, char *argv[])
{
        QApplication app(argc, argv);
        
        //创建MyWidget的一个实例
        MyWidget widget;
        widget.show();
        
        return app.exec();
}
 
截图:
 
我们发现拖到一个滑块,前面的所有滑块设置成同样的值。