Skip to content

Commit 1367d25

Browse files
yangsheng6810Yang Sheng
authored and
Yang Sheng
committed
add sunlight
1 parent f0f8141 commit 1367d25

9 files changed

+109
-5
lines changed

final.pro

+6-2
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@ SOURCES += main.cpp\
1616
mainwindow.cpp \
1717
plant.cpp \
1818
sunflower.cpp \
19-
backgroundmusic.cpp
19+
backgroundmusic.cpp \
20+
sunlight.cpp \
21+
peashooter.cpp
2022

2123
HEADERS += mainwindow.h \
2224
plant.h \
2325
sunflower.h \
24-
backgroundmusic.h
26+
backgroundmusic.h \
27+
sunlight.h \
28+
peashooter.h
2529

2630
FORMS +=
2731

mainwindow.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "plant.h"
44
#include "sunflower.h"
55
#include "backgroundmusic.h"
6+
#include "sunlight.h"
67

78
MainWindow::MainWindow(QWidget *parent) :
89
QMainWindow(parent)
@@ -12,8 +13,11 @@ MainWindow::MainWindow(QWidget *parent) :
1213
QPushButton *quit = new QPushButton(tr("Quit"));
1314
connect(quit, SIGNAL(clicked()), this, SLOT(close()));
1415

16+
SunLight *sun = new SunLight();
17+
1518
QVBoxLayout *layout = new QVBoxLayout;
1619
layout->addWidget(quit);
20+
layout->addWidget(sun);
1721

1822
QGridLayout *grid = new QGridLayout;
1923
layout->addLayout(grid);
@@ -25,8 +29,10 @@ MainWindow::MainWindow(QWidget *parent) :
2529

2630
Plant *flower = new SunFlower();
2731
grid->addWidget(flower,0,1);
32+
connect(flower,SIGNAL(produceSunLight(int)),sun,SLOT(addSunLight(int)));
2833
flower = new SunFlower();
2934
grid->addWidget(flower,1,1);
35+
connect(flower,SIGNAL(produceSunLight(int)),sun,SLOT(addSunLight(int)));
3036

3137
QWidget *widget = new QWidget;
3238
widget->setLayout(layout);

mainwindow.h

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <QMenu>
1111
#include <QMenuBar>
1212
#include <QAction>
13+
#include <QLCDNumber>
1314

1415
class MainWindow : public QMainWindow
1516
{

peashooter.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include "peashooter.h"
2+
3+
PeaShooter::PeaShooter(QWidget *parent) :
4+
Plant(parent)
5+
{
6+
}

peashooter.h

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef PEASHOOTER_H
2+
#define PEASHOOTER_H
3+
#include "plant.h"
4+
5+
class PeaShooter : public Plant
6+
{
7+
Q_OBJECT
8+
public:
9+
explicit PeaShooter(QWidget *parent = 0);
10+
11+
signals:
12+
13+
public slots:
14+
15+
};
16+
17+
#endif // PEASHOOTER_H

sunflower.cpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
#include "sunflower.h"
2+
#include "sunlight.h"
23

3-
SunFlower::SunFlower():
4-
Plant( 0,":/images/sunflower.gif")
4+
SunFlower::SunFlower(QWidget *parent):
5+
Plant(parent, ":/images/sunflower.gif")
56
{
7+
QTimer *timer = new QTimer(this);
8+
connect(timer, SIGNAL(timeout()), this, SLOT(sunLight()));
9+
timer->start(1000);
610
}
711

12+
void SunFlower::sunLight()
13+
{
14+
emit produceSunLight(50);
15+
}

sunflower.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@
55

66
class SunFlower : public Plant
77
{
8+
Q_OBJECT
89
public:
9-
SunFlower();
10+
explicit SunFlower(QWidget *parent = 0);
11+
signals:
12+
void produceSunLight(int num);
13+
public slots:
14+
void sunLight();
1015
};
1116

1217
#endif // SUNFLOWER_H

sunlight.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "sunlight.h"
2+
3+
SunLight::SunLight(QWidget *parent) :
4+
QWidget(parent),sunLight(1000)
5+
{
6+
lcd = new QLCDNumber(4);// for debug
7+
connect(this,SIGNAL(updateSun(int)),lcd,SLOT(display(int)));
8+
QPushButton* button = new QPushButton(tr("add sunlight"));
9+
connect(button, SIGNAL(clicked()),this,SLOT(addSunLight()));
10+
QHBoxLayout* layout = new QHBoxLayout;
11+
layout->addWidget(lcd);
12+
layout->addWidget(button);
13+
this->setLayout(layout);
14+
}
15+
16+
void SunLight::addSunLight(int num)
17+
{
18+
sunLight += num;
19+
if (sunLight > 9990)
20+
sunLight = 9990;
21+
emit updateSun(sunLight);
22+
}
23+
24+
void SunLight::addSunLight()// for debug
25+
{
26+
sunLight += 50;
27+
if (sunLight > 9990)
28+
sunLight = 9990;
29+
emit updateSun(sunLight);
30+
}

sunlight.h

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef SUNLIGHT_H
2+
#define SUNLIGHT_H
3+
4+
#include <QWidget>
5+
#include <QLCDNumber>
6+
#include <QHBoxLayout>
7+
#include <QPushButton>
8+
9+
class SunLight : public QWidget
10+
{
11+
Q_OBJECT
12+
public:
13+
explicit SunLight(QWidget *parent = 0);
14+
15+
signals:
16+
17+
public slots:
18+
void addSunLight(int num);
19+
void addSunLight(void);// for debug
20+
protected:
21+
int sunLight;
22+
QLCDNumber* lcd;
23+
signals:
24+
void updateSun(int);
25+
};
26+
27+
#endif // SUNLIGHT_H

0 commit comments

Comments
 (0)