Qt4.4.3自帶教程-一個變化牽扯其餘的變化

爲咱們自定義的類添加信號和槽:
 
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();
}
 
截圖:
 
咱們發現拖到一個滑塊,前面的全部滑塊設置成一樣的值。
相關文章
相關標籤/搜索