96、QT 实现LED圆形指示灯控件

在这里插入图片描述
代码实现更改控件样式表的颜色和形状等

//QLabel控件变成圆形LED指示灯样式表
const QString QLabel_LED_Circle_SheetStyle_Red = "min-width: 20px; min-height: 20px;max-width:20px; max-height: 20px;border-radius: 10px; background:red";//红色
const QString QLabel_LED_Circle_SheetStyle_Green = "min-width: 20px; min-height: 20px;max-width:20px; max-height: 20px;border-radius: 10px; ;background:green";//绿色
const QString QLabel_LED_Circle_SheetStyle_Gray = "min-width: 20px; min-height: 20px;max-width:20px; max-height: 20px;border-radius: 10px;  border:1px solid black;background:grey";//灰色
const QString QLabel_LED_Circle_SheetStyle_Yellow = "min-width: 20px; min-height: 20px;max-width:20px; max-height: 20px;border-radius: 10px;  border:1px solid black;background:yellow";//黄色
    ui->led->setStyleSheet(QLabel_LED_Circle_SheetStyle_Red);// 红色圆形警示灯

在这里插入图片描述
在这里插入图片描述
第二种方法:
声明:

    void QT_QLabel_LED_Set(QLabel *label,QString rgb_color,bool shape = false,uint8_t size = 20);

直接调用此函数

/****************************************
@function:QT QLabel指示颜色设置
@param:label--文本控件
    rgb_color--颜色值,见宏RGB颜色值,如dRGB_RED
    shape--LED形状,假-圆形,真-矩形,默认圆形
    size--像素大小px,默认20px
@return:void
@note:QLabel控件变成圆形|矩形LED指示灯样式表,拖放控件时请把最小高度设置为使用的像素大小
****************************************/
void Common::QT_QLabel_LED_Set(QLabel *label,QString rgb_color,bool shape,uint8_t size)
{
    QString min_width,min_height,max_width,max_height,border_radius,border,background;
    label->setText("");// 将label中的文字清空
    if(shape)
    {
        min_width  = QString("min-width: %1px;").arg(size);       // 最小宽度:size
        min_height = QString("min-height: %1px;").arg(size / 2);  // 最小高度:size
        max_width  = QString("max-width: %1px;").arg(size * 3);   // 最大宽度:size
        max_height = QString("max-height: %1px;").arg(size);      // 最大高度:size
    }
    else
    {
        min_width  = QString("min-width: %1px;").arg(size);       // 最小宽度:size
        min_height = QString("min-height: %1px;").arg(size);  // 最小高度:size
        max_width  = QString("max-width: %1px;").arg(size);   // 最大宽度:size
        max_height = QString("max-height: %1px;").arg(size);      // 最大高度:size
    }
    // 再设置边界形状及边框
    border_radius = QString("border-radius: %1px;").arg(size/2);  // 边框是圆角,半径为size/2
    border = QString("border:1px solid black;");  // 边框为1px黑色

    // 最后设置背景颜色
    background = "background-color:" + rgb_color;
    const QString SheetStyle = min_width + min_height + border_radius + border + background;
    label->setStyleSheet(SheetStyle);
    label->setAlignment(Qt::AlignCenter);
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值