前言
写上位机软件的时候,经常会用到指示灯控件,奈何Qt设计师提供的widget控件中没有指示灯,拖拖拖无计可施?那就自己画吧。painter画一个简单的指示灯,轮廓颜色和内部填充颜色搞定。新建一个类,继承QWidget,带不带ui都行,我这里选择带ui了(代码中体现),重写paintEvent即可,其他的就是一些自己定义的接口了。
特点
1、内置几种常用的颜色,红绿蓝黄白;
2、提供自定义颜色接口,轮廓+内部填充;
3、右键菜单自选颜色。
代码
colorfullamp.h
#ifndef COLORFULLAMP_H
#define COLORFULLAMP_H
#include <QWidget>
#include <QPainter>
#include <QPainterPath>
#include <QPen>
#include <QBrush>
#include <QColor>
#include <QPoint>
#include <QMouseEvent>
#include <QMenu>
#include <QColorDialog>
QT_BEGIN_NAMESPACE
namespace Ui { class ColorfulLamp; }
QT_END_NAMESPACE
class ColorfulLamp : public QWidget
{
Q_OBJECT
//Q_PROPERTY(int lamp_type READ Lamp_type WRITE set_Lamp_type)
public:
ColorfulLamp(QWidget *parent = nullptr);
~ColorfulLamp();
void red_lamp();
void green_lamp();
void blue_lamp();
void yellow_lamp();
void white_lamp();
void custom_lamp(const QColor& border = QColor(Qt::white), const QBrush& inside = QBrush(Qt::black));
void set_inside_color(const QColor& color);
void set_border_color(const QColor& color);
void set_pen_width(int width);
QBrush get_inside_color();
QColor get_border_color();
int get_pen_width();
protected:
void paintEvent(QPaintEvent *);
void mousePressEvent(QMouseEvent *);
private:
void select_inside_color();
void select_border_color();
private:
Ui::ColorfulLamp *ui;
QMenu* menu;
QBrush inside_color;
QColor border_color;
int pen_width = 4;
};
#endif // COLORFULLAMP_H
colorfullamp.cpp
#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif
#include "colorfullamp.h"
#include "ui_colorfullamp.h"
#include <QDebug>
#define sys_out qDebug()
ColorfulLamp::ColorfulLamp(QWidget *parent)
: QWidget(parent)
, ui(new Ui::ColorfulLamp)
{
ui->setupUi(this);
setAttribute(Qt::WA_TranslucentBackground); //背景透明
setWindowFlag(Qt::FramelessWindowHint); //设置无边框,背景才会透明,否则显示黑色
menu = new QMenu(this);
menu->addAction("红色", this, &ColorfulLamp::red_lamp);
menu->addAction("绿色", this, &ColorfulLamp::green_lamp);
menu->addAction("蓝色", this, &ColorfulLamp::blue_lamp);
menu->addAction("黄色", this, &ColorfulLamp::yellow_lamp);
menu->addAction("白色", this, &ColorfulLamp::white_lamp);
menu->addAction("选择轮廓颜色", this, &ColorfulLamp::select_border_color);
menu->addAction("选择指示灯颜色", this, &ColorfulLamp::select_inside_color);
// menu->addAction("关闭", [this]{close();});
border_color = QColor(Qt::white);
inside_color = QBrush(Qt::red);
}
ColorfulLamp::~ColorfulLamp()
{
delete ui;
}
void ColorfulLamp::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing); //开启抗锯齿
QPen pen;
pen.setWidth(pen_width); //轮廓线宽,默认4px
pen.setColor(border_color);
painter.setPen(pen);
int cur_width = width(); //widget宽度
int cur_height = height(); //widget高度
QPoint center(cur_width/2, cur_height/2); //圆心
int radius = __min(cur_width, cur_height)/2 - pen_width/2; //半径,取宽高的较小者
painter.drawEllipse(center, radius, radius); //画轮廓
QPainterPath path;
path.addEllipse(center, radius - pen_width/2, radius - pen_width/2);
painter.fillPath(path, inside_color); //填充圆
}
void ColorfulLamp::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::RightButton)
{
menu->exec(event->globalPos());
}
}
void ColorfulLamp::set_inside_color(const QColor& color)
{
inside_color.setColor(color);
}
void ColorfulLamp::set_border_color(const QColor& color)
{
inside_color.setColor(color);
}
void ColorfulLamp::red_lamp()
{
inside_color.setColor(Qt::red);
update();
}
void ColorfulLamp::green_lamp()
{
inside_color.setColor(Qt::green);
update();
}
void ColorfulLamp::blue_lamp()
{
inside_color.setColor(Qt::blue);
update();
}
void ColorfulLamp::yellow_lamp()
{
inside_color.setColor(Qt::yellow);
update();
}
void ColorfulLamp::white_lamp()
{
inside_color.setColor(Qt::white);
update();
}
void ColorfulLamp::custom_lamp(const QColor& border, const QBrush& inside)
{
border_color = border;
inside_color = inside;
update();
}
void ColorfulLamp::select_inside_color()
{
QColor selection = QColorDialog::getColor();
if(selection.isValid())
{
inside_color.setColor(selection);
update();
}
}
void ColorfulLamp::select_border_color()
{
QColor selection = QColorDialog::getColor();
if(selection.isValid())
{
border_color = selection;
update();
}
}
QBrush ColorfulLamp::get_inside_color()
{
return inside_color;
}
QColor ColorfulLamp::get_border_color()
{
return border_color;
}
void ColorfulLamp::set_pen_width(int width)
{
if(width > 0)
{
pen_width = width;
update();
}
}
int ColorfulLamp::get_pen_width()
{
return pen_width;
}