Qt4.4.3自带教程-这次分成多个文件

lcdrange.h文件:
#ifndef __LCDRANGE_H__
#define __LCDRANGE_H__

#include <QWidget>

// place your code here
class LCDRange : public QWidget
{
public:
        LCDRange(QWidget *parent = 0);
};

#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
        QSlider *slider = new QSlider(Qt::Horizontal);
        slider->setRange(0, 99);
        slider->setValue(0);
        connect(slider, SIGNAL(valueChanged( int)),lcd, SLOT(display( int)));

   //布局
        QVBoxLayout *layout = new QVBoxLayout;
        layout->addWidget(lcd);
        layout->addWidget(slider);
        setLayout(layout);
}
 
 
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()));

   //网格布局
        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);
                }
        }

        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();
}
 
 
结果同上次的:

你可能感兴趣的:(C++,GUI,职场,qt,休闲)