第3篇 Qt实现十字路口交通灯控制系统(二)

第3篇 Qt实现十字路口交通灯控制系统(二)

1.系统最简单的类Lamp

灯类Lamp是交通系统中最简单的类,可以说它没有和其他类进行交互,即信号传递处理等,就是一个普通的C++类,没有基类。

1.1.属性

(1)位置,x,y
位置由两个整型变量x,y表示,即int x,y;
主要在绘制灯的时候用到。
(2)状态,status
bool型变量,用于判断该灯的颜色
(3)大小,width,height
整型静态变量,因为每个灯的大小都是一样的,宽度和高度都是25。

1.2 .操作

(1)Lamp();
给变量赋初值,避免出错。
(2)Lamp(int _x,int _y,int _status);
给变量赋对应的值。
(3)void setInfo(int _x,int _y,int _status);
如果想改变位置或者状态的话可以通过这个函数实现。
(4)void setX(int _x);
设置横坐标。
(5)void setY(int _y);
设置纵坐标。
(6)void setStatus(bool _status);
设置状态,即状态转换时,通过这个函数实现。
(7)int getX();
获取横坐标。
(8)int getY();
获取纵坐标。
(9)bool getStatus();
获取灯的状态。
(10)static int getWidth();
获取灯的宽度。
(11)static int getHeight();
获取灯的高度。

1.3.头文件

#ifndef LAMP_H
#define LAMP_H

class Lamp
{
public:
    Lamp();
    Lamp(int _x,int _y,int _status);
    void setInfo(int _x,int _y,int _status);
    void setX(int _x);
    void setY(int _y);
    void setStatus(bool _status);
    int getX();
    int getY();
    bool getStatus();
    static int getWidth();
    static int getHeight();

private:
    int x,y;//灯的位置
    bool status;//灯的状态
    static int width;//灯的宽度
    static int height;//灯的高度
};

#endif // LAMP_H

1.4.源文件

#include "lamp.h"

int Lamp::width = 25;
int Lamp::height = 25;

Lamp::Lamp()
{
    this->x = 0;
    this->y = 0;
    this->status = false;
}

Lamp::Lamp(int _x,int _y,int _status)
{
    this->x = _x;
    this->y = _y;
    this->status = _status;
}
void Lamp::setInfo(int _x,int _y,int _status){
    this->x = _x;
    this->y = _y;
    this->status = _status;
}

void Lamp::setX(int _x){
    this->x = _x;
}

void Lamp::setY(int _y){
    this->y = _y;
}

void Lamp::setStatus(bool _status){
    this->status = _status;
}

int Lamp::getX(){
    return this->x;
}

int Lamp::getY(){
    return this->y;
}

bool Lamp::getStatus(){
    return this->status;
}

int Lamp::getWidth(){
    return Lamp::width;
}

int Lamp::getHeight(){
    return Lamp::height;
}

2.结语

在头文件声明的静态变量要在源文件中重新定义一次,不用再加上static,静态函数也是一样。不然会报错。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我可以帮您解答。如果您想在Qt实现LED交通灯的亮灭,可以通过以下步骤进行: 1. 创建一个新的Qt工程,并添加三个QLabel控件,分别代表红灯、黄灯和绿灯。 2. 设置QLabel的背景色,分别设置红色、黄色和绿色。 3. 在Qt中使用QTimer类,定时器每隔一段时间就切换灯的状态。例如,红灯亮10秒,黄灯亮3秒,绿灯亮15秒,然后再切换到红灯。 4. 在QTimer的槽函数中,根据当前灯的状态,改变对应QLabel的背景色,从而实现LED交通灯的亮灭。 以下是一个简单的示例代码: ``` #include <QtWidgets> class TrafficLight : public QWidget { public: TrafficLight(QWidget *parent = nullptr) : QWidget(parent) { QHBoxLayout *layout = new QHBoxLayout(this); layout->addWidget(redLabel = new QLabel); layout->addWidget(yellowLabel = new QLabel); layout->addWidget(greenLabel = new QLabel); redLabel->setFixedSize(50, 50); yellowLabel->setFixedSize(50, 50); greenLabel->setFixedSize(50, 50); redLabel->setAutoFillBackground(true); yellowLabel->setAutoFillBackground(true); greenLabel->setAutoFillBackground(true); redLabel->setStyleSheet("background-color: red;"); yellowLabel->setStyleSheet("background-color: gray;"); greenLabel->setStyleSheet("background-color: gray;"); timer = new QTimer(this); connect(timer, &QTimer::timeout, this, &TrafficLight::changeLight); timer->start(1000); } private: QLabel *redLabel; QLabel *yellowLabel; QLabel *greenLabel; QTimer *timer; int state = 0; void changeLight() { switch (state) { case 0: redLabel->setStyleSheet("background-color: red;"); yellowLabel->setStyleSheet("background-color: gray;"); greenLabel->setStyleSheet("background-color: gray;"); state = 1; timer->start(10000); break; case 1: redLabel->setStyleSheet("background-color: gray;"); yellowLabel->setStyleSheet("background-color: yellow;"); greenLabel->setStyleSheet("background-color: gray;"); state = 2; timer->start(3000); break; case 2: redLabel->setStyleSheet("background-color: gray;"); yellowLabel->setStyleSheet("background-color: gray;"); greenLabel->setStyleSheet("background-color: green;"); state = 3; timer->start(15000); break; case 3: redLabel->setStyleSheet("background-color: red;"); yellowLabel->setStyleSheet("background-color: gray;"); greenLabel->setStyleSheet("background-color: gray;"); state = 1; timer->start(10000); break; } } }; int main(int argc, char *argv[]) { QApplication app(argc, argv); TrafficLight light; light.show(); return app.exec(); } ``` 运行上述代码,您将看到一个简单的LED交通灯示例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大唐不良猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值