基于QT 设计实现的车辆远近光灯控件.
1.设计说明
基于QWidget设计实现,通过重构QPaintEvent事件,绘制出的车辆远近光灯控件,可通过QWidget提升方法,应用于项目中.
2.核心代码:
#ifndef QCUSTOMHIGHLOWBEAM_H
#define QCUSTOMHIGHLOWBEAM_H
#include <QWidget>
#include <QPainter>
/**
* @brief Feature:
* Support
* Support
* Support
* Support
*/
#ifdef quc
#if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
#include <QtDesigner/QDesignerExportWidget>
#else
#include <QtUiPlugin/QDesignerExportWidget>
#endif
class QDESIGNER_WIDGET_EXPORT QCustomMobileSignal : public QWidget
#else
class QCustomHighLowBeam : public QWidget
#endif
{
Q_OBJECT
// Q_PROPERTY()
public:
explicit QCustomHighLowBeam(QWidget *parent = nullptr);
~QCustomHighLowBeam();
protected:
QSize sizeHint() const;
QSize minimumSizeHint() const;
void paintEvent(QPaintEvent *);
void resizeEvent(QResizeEvent *);
void drawBackground(QPainter *painter);
void drawHighBeam(QPainter *painter);
void drawLowBeam(QPainter *painter);
private:
QColor m_active_color_;
QColor m_unactive_color_;
// false:unactive true:active
bool m_highbeam_active_{false};
bool m_lowbeam_active_{false};
QColor m_background_color_;
signals:
public slots:
void setHighBeamState(bool value);
void setLowBeamState(bool value);
};
#endif // QCUSTOMHIGHLOWBEAM_H
#include "qcustomhighlowbeam.h"
QCustomHighLowBeam::QCustomHighLowBeam(QWidget *parent) : QWidget(parent)
{
m_background_color_ = QColor(55,107,154, 30);
m_active_color_ = Qt::yellow;
m_unactive_color_ = Qt::gray;
}
QCustomHighLowBeam::~QCustomHighLowBeam()
{
}
void QCustomHighLowBeam::resizeEvent(QResizeEvent *)
{
this->repaint();
}
QSize QCustomHighLowBeam::sizeHint() const
{
return QSize(150, 150);
}
QSize QCustomHighLowBeam::minimumSizeHint() const
{
return QSize(30, 30);
}
void QCustomHighLowBeam::paintEvent(QPaintEvent *)
{
int width = this->width();
int height = this->height();
int side = qMin(width, height);
//绘制准备工作,启用反锯齿,平移坐标轴中心,等比例缩放
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
painter.translate(width / 2, height / 2);
painter.scale(side / 200.0, side / 200.0);
drawBackground(&painter);
drawHighBeam(&painter);
drawLowBeam(&painter);
// drawTitle(&painter);
// drawTimeCost(&painter);
// drawSystemTime(&painter);
}
void QCustomHighLowBeam::drawBackground(QPainter *painter)
{
int width = this->width();
int height = this->height();
int side = qMin(width, height);
painter->save();
painter->setBrush(m_background_color_);
painter->drawRect(-width, -height, width * 2, height * 2);
painter->restore();
}
void QCustomHighLowBeam::drawHighBeam(QPainter *painter)
{
int width = this->width();
int height = this->height();
int side = qMin(width, height);
QLine lines[] = {
{-25, -20, -5, -20},
{-25, -10, -5, -10},
{-25, 0, -5, 0},
{-25, 10, -5, 10},
{-25, 20, -5, 20},
{-25, 20, -5, 20},
{ 2, -30, 2, 30}
};
painter->save();
if (m_highbeam_active_)
{
painter->setPen(QPen(m_active_color_, 4));
} else {
painter->setPen(QPen(m_unactive_color_, 4));
}
painter->drawLines(lines, (sizeof(lines)/sizeof (lines[0])));
QRectF rectangle(-50.0, -30.0, 105.0, 60.0);
int startAngle = 90 * 16;
int spanAngle = -180 * 16;
painter->drawArc(rectangle, startAngle, spanAngle);
painter->restore();
}
void QCustomHighLowBeam::drawLowBeam(QPainter *painter)
{
int width = this->width();
int height = this->height();
int side = qMin(width, height);
int offset = 100;
QLine lines[] = {
{-25+offset, -15, -5+offset, -25},
{-25+offset, -5, -5+offset, -15},
{-25+offset, 5, -5+offset, -5},
{-25+offset, 15, -5+offset, 5},
{-25+offset, 25, -5+offset, 15},
{-25+offset, 35, -5+offset, 25},
{ 2+offset, -30, 2+offset, 30}
};
painter->save();
if (m_lowbeam_active_)
{
painter->setPen(QPen(m_active_color_, 4));
} else {
painter->setPen(QPen(m_unactive_color_, 4));
}
painter->drawLines(lines, (sizeof(lines)/sizeof (lines[0])));
QRectF rectangle(-50.0+offset, -30.0, 105.0, 60.0);
int startAngle = 90 * 16;
int spanAngle = -180 * 16;
painter->drawArc(rectangle, startAngle, spanAngle);
painter->restore();
}
void QCustomHighLowBeam::setHighBeamState(bool value)
{
if (this->m_highbeam_active_ != value)
{
this->m_highbeam_active_ = value;
this->update();
}
}
void QCustomHighLowBeam::setLowBeamState(bool value)
{
if (this->m_lowbeam_active_ != value)
{
this->m_lowbeam_active_ = value;
this->update();
}
}
3.效果示意图



该文章介绍了一个基于QT的自定义控件QCustomHighLowBeam,用于模拟车辆的远近光灯状态。控件利用QWidget和QPaintEvent进行绘制,支持设置远近光灯的状态,并提供了相应的设置方法。在窗口大小改变时能自动调整显示。
840

被折叠的 条评论
为什么被折叠?



