QT:QLabel的LED透明跑马灯

实验内容:

1 背景透明

2 三个按键设置颜色

3 auto按键自动切换颜色

4 1秒定时器

实验效果:

测试代码

widget.h

cpp 复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QObject>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QDebug>
#include <QPainter>
#include <QTimer>

class MyLed : public QLabel
{
    Q_OBJECT
public:
    MyLed(int w = 100,int h = 100,QWidget *parent = nullptr):QLabel(parent),m_width(w),m_height(h)
    {
        this->setWindowFlag(Qt::FramelessWindowHint);
        setAttribute(Qt::WA_TranslucentBackground);//设置背景透明
        setColor(QColor(0,0,0,0));
    }
    void setColor(const QColor & color)
    {
        QPixmap pix(m_width,m_height);
        pix.fill(Qt::transparent);

        QPainter p(&pix);
        p.setPen(QColor(0,0,0,0));
        p.setBrush(color);

        QRect rect =  pix.rect();
        int rx = (rect.width() - 2)/2;
        int ry = (rect.height() - 2)/2;
        qDebug() <<"rx = " << rx;
        qDebug() <<"ry = " << ry;
        p.drawEllipse(rect.center(),rx,ry);
        this->setPixmap(pix);
    }
private:
    QColor m_color;
    int m_width;
    int m_height;
};

#define func(num) button_##num##_click()

class Widget : public QWidget
{
    Q_OBJECT
public:
    Widget(QWidget *parent = nullptr)
        : QWidget(parent)
    {
        this->setWindowFlag(Qt::FramelessWindowHint);
        setAttribute(Qt::WA_TranslucentBackground);//设置背景透明

        m_timer.setInterval(1000);

        connect(&m_timer,&QTimer::timeout,[this](){
            static int count = 1;
            switch(count){
            case 1:
                this->func(1);
                break;
            case 2:
                this->func(2);
                break;
            case 3:
                this->func(3);
                break;
            }
            count++;
            if(count > 3){
                count = 1;
            }

        });

        QVBoxLayout *root = new QVBoxLayout();
        QHBoxLayout *led_layout = new QHBoxLayout();
        QHBoxLayout *button_layout = new QHBoxLayout();
        this->setWindowTitle(QString::fromUtf8("LED监视器"));


        m_led = new MyLed();
        led_layout->addStretch();
        led_layout->addWidget(m_led);
        led_layout->addStretch();

        connect(m_button_1,SIGNAL(clicked()),this,SLOT(button_1_click()));
        connect(m_button_2,SIGNAL(clicked()),this,SLOT(button_2_click()));
        connect(m_button_3,SIGNAL(clicked()),this,SLOT(button_3_click()));
        connect(m_button_auto,SIGNAL(clicked()),this,SLOT(button_auto_click()));
        //QObject::connect(m_button_1,&QPushButton::clicked,someFunction);


        button_layout->addWidget(m_button_1);
        button_layout->addWidget(m_button_2);
        button_layout->addWidget(m_button_3);
        button_layout->addWidget(m_button_auto);

        root->addLayout(led_layout);
        root->addLayout(button_layout);
        this->setLayout(root);
    }
    ~Widget(){ }
    MyLed *m_led;
    QPushButton *m_button_1 = new QPushButton("red");
    QPushButton *m_button_2 = new QPushButton("green");
    QPushButton *m_button_3 = new QPushButton("blue");
    QPushButton *m_button_auto = new QPushButton("auto");
private slots:
    void button_1_click()
    {
        qDebug() <<QString::fromLocal8Bit("red");
        m_led->setColor(QColor(255,0,0));
    }
    void button_2_click()
    {
        qDebug() <<QString::fromLocal8Bit("green");
        m_led->setColor(QColor(0,255,0));
    }
    void button_3_click()
    {
        qDebug() <<QString::fromLocal8Bit("blue");
        m_led->setColor(QColor(0,0,255));
    }
    void button_auto_click()
    {
        QPushButton *button = (QPushButton *)sender();
        qDebug() << button->text();
        if(m_timer.isActive()){
            m_timer.stop();
        }else{
            m_timer.start();
        }

    }
private:
    QTimer m_timer;

};
#endif // WIDGET_H

main.cpp

cpp 复制代码
#include "widget.h"

#include <QApplication>

void someFunction()
{
    qDebug() << QString::fromLocal8Bit("hehe");
}

int app_1(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}

int app_2(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MyLed w;
    w.setColor(QColor(255,0,0));
    w.show();
    return a.exec();
}

int main(int argc, char *argv[])
{
    return app_1(argc,argv);
    // return app_2(argc,argv);
}

小结

相关推荐
上海合宙LuatOS9 分钟前
全栈工程师实战手册:LuatOS日志系统开发指南!
java·开发语言·单片机·嵌入式硬件·物联网·php·硬件工程
多敲代码防脱发9 分钟前
导出导入Excel文件(详解-基于EasyExcel)
java·开发语言·jvm·数据库·mysql·excel
9527华安1 小时前
紫光同创FPGA实现AD7606数据采集转UDP网络传输,提供PDS工程源码和技术支持和QT上位机
网络·qt·fpga开发·udp·紫光同创·ad7606
来自星星的坤1 小时前
深入理解 NumPy:Python 科学计算的基石
开发语言·python·numpy
小声读源码1 小时前
【技巧】使用UV创建python项目的开发环境
开发语言·python·uv·dify
yxc_inspire1 小时前
基于Qt的app开发第七天
开发语言·c++·qt·app
zm-v-159304339861 小时前
解锁遥感数据密码:DeepSeek、Python 与 OpenCV 的协同之力
开发语言·python·opencv
周Echo周2 小时前
20、map和set、unordered_map、un_ordered_set的复现
c语言·开发语言·数据结构·c++·算法·leetcode·list
明天更新2 小时前
Java处理压缩文件的两种方式!!!!
java·开发语言·7-zip
老胖闲聊2 小时前
C# 注册表操作类
开发语言·c#