为我们自定义的类添加信号和槽:
lcdrange.h头文件:
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
#ifndef __LCDRANGE_H__
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
#define __LCDRANGE_H__
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
#include <QWidget>
class QSlider;
//new:提前声明一下
class LCDRange :
public QWidget
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
{
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
Q_OBJECT
public:
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
LCDRange(QWidget *parent = 0);
int value()
const;
//new : 添加一个函数
public slots:
void setValue(
int value);
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
signals:
void valueChanged(
int newValue);
private:
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
QSlider * slider;
//new:改成成员变量
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
};
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
#endif
// __LCDRANGE_H__
lcdrange.cpp类实现文件:
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
#include <QLCDNumber>
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
#include <QSlider>
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
#include <QVBoxLayout>
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
#include
"lcdrange.h"
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
LCDRange::LCDRange(QWidget *parent)
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
: QWidget(parent)
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
{
//LCD数字
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
QLCDNumber *lcd =
new QLCDNumber(2);
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
lcd->setSegmentStyle(QLCDNumber::Flat);
//Slider
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
slider =
new QSlider(Qt::Horizontal);
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
slider->setRange(0, 99);
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
slider->setValue(0);
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
connect(slider, SIGNAL(valueChanged(
int)),lcd, SLOT(display(
int)));
//new: 新加
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
connect(slider,SIGNAL(valueChanged(
int)),
this,SIGNAL(valueChanged(
int)));
//布局
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
QVBoxLayout *layout =
new QVBoxLayout;
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
layout->addWidget(lcd);
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
layout->addWidget(slider);
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
setLayout(layout);
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
}
int LCDRange::value()
const
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
{
return slider->value();
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
}
void LCDRange::setValue(
int value)
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
{
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
slider->setValue(value);
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
}
mywidget.h头文件:
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
#ifndef __MYWIDGET_H__
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
#define __MYWIDGET_H__
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
#include <QWidget>
class MyWidget :
public QWidget
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
{
public:
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
MyWidget(QWidget *parent = 0);
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
};
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
#endif
// __MYWIDGET_H__
mywidget.cpp文件:
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
#include <QApplication>
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
#include <QPushButton>
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
#include <QFont>
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
#include <QGridLayout>
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
#include
"lcdrange.h"
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
#include
"mywidget.h"
//MyWidget类实现部分
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
MyWidget::MyWidget(QWidget *parent)
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
: QWidget(parent)
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
{
//创建quit按钮
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
QPushButton *quit =
new QPushButton(tr(
"Quit"));
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
quit->setFont(QFont(
"Times", 18, QFont::Bold));
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
LCDRange* prevRange=0;
//网格布局
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
QGridLayout *grid =
new QGridLayout;
for (
int row = 0; row < 3; ++row) {
for (
int column = 0; column < 3; ++column) {
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
LCDRange *lcdRange =
new LCDRange;
//创建LCD+Slider
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
grid->addWidget(lcdRange, row, column);
//new : 新增
if(prevRange)
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
{
//如果存在前一个
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
connect(lcdRange,SIGNAL(valueChanged(
int)),prevRange,SLOT(setValue(
int)));
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
}
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
prevRange=lcdRange;
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
}
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
}
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
QVBoxLayout *layout =
new QVBoxLayout;
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
layout->addWidget(quit);
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
layout->addLayout(grid);
//子布局
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
setLayout(layout);
//设置布局
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
}
main.cpp主程序文件:
//主程序文件
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
#include <QApplication>
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
#include
"mywidget.h"
int main(
int argc,
char *argv[])
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
{
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
QApplication app(argc, argv);
//创建MyWidget的一个实例
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
MyWidget widget;
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
widget.show();
return app.exec();
![InBlock.gif](http://xcf007.blog.51cto.com/images/editer/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
}
截图:
我们发现拖到一个滑块,前面的所有滑块设置成同样的值。
本文转自 xcf007 51CTO博客,原文链接:http://blog.51cto.com/xcf007/104096
,如需转载请自行联系原作者