基于QT自定义设计的车辆雨刮控制显示器
1. 设计说明
基于QWidget设计实现,通过重构QPaintEvent事件,绘制出的车辆远近光灯控件,可通过QWidget提升方法,应用于项目中.
2. 核心代码
#ifndef QCUSTOMWIPER_H
#define QCUSTOMWIPER_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 QCustomWiper : public QWidget
#endif
{
Q_OBJECT
// Q_PROPERTY()
public:
enum WiperMode {
WIPERMODE_DEFAULT = 0,
WIPERMODE_A = 1,
WIPERMODE_B = 2,
WIPERMODE_C = 3,
};
explicit QCustomWiper(QWidget *parent = nullptr);
~QCustomWiper();
protected:
QSize sizeHint() const;
QSize minimumSizeHint() const;
void paintEvent(QPaintEvent *);
void resizeEvent(QResizeEvent *);
void drawBackground(QPainter *painter);
void drawWiper(QPainter *painter);
private:
QColor m_active_color_;
QColor m_unactive_color_;
WiperMode m_wiper_state_;
QColor m_background_color_;
signals:
public slots:
void setWiperMode(WiperMode mode);
};
#endif // QCUSTOMWIPER_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->cbx_wipermode->addItem("关闭", QVariant("DEFAULT"));
ui->cbx_wipermode->addItem("模式A", QVariant("MODEA"));
ui->cbx_wipermode->addItem("模式B", QVariant("MODEB"));
ui->cbx_wipermode->addItem("模式C", QVariant("MODEC"));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_cbx_wipermode_currentIndexChanged(const QString &arg1)
{
QString value = ui->cbx_wipermode->currentData().toString();
if (value == "DEFAULT")
{
ui->widget->setWiperMode(QCustomWiper::WIPERMODE_DEFAULT);
} else if (value == "MODEA") {
ui->widget->setWiperMode(QCustomWiper::WIPERMODE_A);
} else if (value == "MODEB") {
ui->widget->setWiperMode(QCustomWiper::WIPERMODE_B);
} else if (value == "MODEC") {
ui->widget->setWiperMode(QCustomWiper::WIPERMODE_C);
}
}
3.效果示意图