Skip to content

Commit

Permalink
重新上传
Browse files Browse the repository at this point in the history
  • Loading branch information
feiyangqingyun committed Jan 16, 2021
1 parent 9cf0de0 commit e8195be
Show file tree
Hide file tree
Showing 149 changed files with 1,542 additions and 6,216 deletions.
Binary file added 0snap/miniblink.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
| 38 | designer | QtDesigner4源码 |
| 39 | netserver | 网络中转服务器 |
| 40 | mpvdemo | 视频流播放mpv内核 |
| 41 | miniblink | miniblink示例 |

### 二、学习群
1. **Qt交流大会群 853086607(雨田哥)**
Expand Down Expand Up @@ -92,4 +93,5 @@
![avatar](https://github.com/feiyangqingyun/QWidgetDemo/raw/master/0snap/key.png)
![avatar](https://github.com/feiyangqingyun/QWidgetDemo/raw/master/0snap/live.png)
![avatar](https://github.com/feiyangqingyun/QWidgetDemo/raw/master/0snap/netserver.jpg)
![avatar](https://github.com/feiyangqingyun/QWidgetDemo/raw/master/0snap/designer.png)
![avatar](https://github.com/feiyangqingyun/QWidgetDemo/raw/master/0snap/designer.png)
![avatar](https://github.com/feiyangqingyun/QWidgetDemo/raw/master/0snap/miniblink.jpg)
69 changes: 44 additions & 25 deletions battery/battery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Battery::Battery(QWidget *parent) : QWidget(parent)
alarmValue = 30;
step = 0.5;

borderWidth = 5;
borderRadius = 8;
bgRadius = 5;
headRadius = 3;
Expand Down Expand Up @@ -57,15 +58,15 @@ void Battery::drawBorder(QPainter *painter)
{
painter->save();

qreal headWidth = width() / 10;
qreal batteryWidth = width() - headWidth;
double headWidth = width() / 15;
double batteryWidth = width() - headWidth;

//绘制电池边框
QPointF topLeft(5, 5);
QPointF bottomRight(batteryWidth, height() - 5);
QPointF topLeft(borderWidth, borderWidth);
QPointF bottomRight(batteryWidth, height() - borderWidth);
batteryRect = QRectF(topLeft, bottomRight);

painter->setPen(QPen(borderColorStart, 5));
painter->setPen(QPen(borderColorStart, borderWidth));
painter->setBrush(Qt::NoBrush);
painter->drawRoundedRect(batteryRect, borderRadius, borderRadius);

Expand All @@ -74,6 +75,10 @@ void Battery::drawBorder(QPainter *painter)

void Battery::drawBg(QPainter *painter)
{
if (value == minValue) {
return;
}

painter->save();

QLinearGradient batteryGradient(QPointF(0, 0), QPointF(0, height()));
Expand All @@ -86,10 +91,10 @@ void Battery::drawBg(QPainter *painter)
}

int margin = qMin(width(), height()) / 20;
qreal unit = (batteryRect.width() - (margin * 2)) / 100;
qreal width = currentValue * unit;
double unit = (batteryRect.width() - (margin * 2)) / 100;
double width = currentValue * unit;
QPointF topLeft(batteryRect.topLeft().x() + margin, batteryRect.topLeft().y() + margin);
QPointF bottomRight(width + margin + 5, batteryRect.bottomRight().y() - margin);
QPointF bottomRight(width + margin + borderWidth, batteryRect.bottomRight().y() - margin);
QRectF rect(topLeft, bottomRight);

painter->setPen(Qt::NoPen);
Expand Down Expand Up @@ -122,13 +127,11 @@ void Battery::updateValue()
{
if (isForward) {
currentValue -= step;

if (currentValue <= value) {
timer->stop();
}
} else {
currentValue += step;

if (currentValue >= value) {
timer->stop();
}
Expand All @@ -137,31 +140,36 @@ void Battery::updateValue()
this->update();
}

qreal Battery::getMinValue() const
double Battery::getMinValue() const
{
return this->minValue;
}

qreal Battery::getMaxValue() const
double Battery::getMaxValue() const
{
return this->maxValue;
}

qreal Battery::getValue() const
double Battery::getValue() const
{
return this->value;
}

qreal Battery::getAlarmValue() const
double Battery::getAlarmValue() const
{
return this->alarmValue;
}

qreal Battery::getStep() const
double Battery::getStep() const
{
return this->step;
}

int Battery::getBorderWidth() const
{
return this->borderWidth;
}

int Battery::getBorderRadius() const
{
return this->borderRadius;
Expand Down Expand Up @@ -217,7 +225,7 @@ QSize Battery::minimumSizeHint() const
return QSize(30, 10);
}

void Battery::setRange(qreal minValue, qreal maxValue)
void Battery::setRange(double minValue, double maxValue)
{
//如果最小值大于或者等于最大值则不设置
if (minValue >= maxValue) {
Expand All @@ -240,20 +248,20 @@ void Battery::setRange(qreal minValue, qreal maxValue)

void Battery::setRange(int minValue, int maxValue)
{
setRange((qreal)minValue, (qreal)maxValue);
setRange((double)minValue, (double)maxValue);
}

void Battery::setMinValue(qreal minValue)
void Battery::setMinValue(double minValue)
{
setRange(minValue, maxValue);
}

void Battery::setMaxValue(qreal maxValue)
void Battery::setMaxValue(double maxValue)
{
setRange(minValue, maxValue);
}

void Battery::setValue(qreal value)
void Battery::setValue(double value)
{
//值和当前值一致则无需处理
if (value == this->value) {
Expand All @@ -272,21 +280,24 @@ void Battery::setValue(qreal value)
} else if (value < currentValue) {
isForward = true;
} else {
this->value = value;
this->update();
return;
}

this->value = value;
this->update();
emit valueChanged(value);
timer->stop();
timer->start();
}

void Battery::setValue(int value)
{
setValue((qreal)value);
setValue((double)value);
}

void Battery::setAlarmValue(qreal alarmValue)
void Battery::setAlarmValue(double alarmValue)
{
if (this->alarmValue != alarmValue) {
this->alarmValue = alarmValue;
Expand All @@ -296,10 +307,10 @@ void Battery::setAlarmValue(qreal alarmValue)

void Battery::setAlarmValue(int alarmValue)
{
setAlarmValue((qreal)alarmValue);
setAlarmValue((double)alarmValue);
}

void Battery::setStep(qreal step)
void Battery::setStep(double step)
{
if (this->step != step) {
this->step = step;
Expand All @@ -309,7 +320,15 @@ void Battery::setStep(qreal step)

void Battery::setStep(int step)
{
setStep((qreal)step);
setStep((double)step);
}

void Battery::setBorderWidth(int borderWidth)
{
if (this->borderWidth != borderWidth) {
this->borderWidth = borderWidth;
this->update();
}
}

void Battery::setBorderRadius(int borderRadius)
Expand Down
105 changes: 52 additions & 53 deletions battery/battery.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,20 @@
#include <QWidget>

#ifdef quc
#if (QT_VERSION < QT_VERSION_CHECK(5,7,0))
#include <QtDesigner/QDesignerExportWidget>
#else
#include <QtUiPlugin/QDesignerExportWidget>
#endif

class QDESIGNER_WIDGET_EXPORT Battery : public QWidget
class Q_DECL_EXPORT Battery : public QWidget
#else
class Battery : public QWidget
#endif

{
Q_OBJECT
Q_PROPERTY(qreal minValue READ getMinValue WRITE setMinValue)
Q_PROPERTY(qreal maxValue READ getMaxValue WRITE setMaxValue)
Q_PROPERTY(qreal value READ getValue WRITE setValue)
Q_PROPERTY(qreal alarmValue READ getAlarmValue WRITE setAlarmValue)
Q_PROPERTY(double minValue READ getMinValue WRITE setMinValue)
Q_PROPERTY(double maxValue READ getMaxValue WRITE setMaxValue)
Q_PROPERTY(double value READ getValue WRITE setValue)
Q_PROPERTY(double alarmValue READ getAlarmValue WRITE setAlarmValue)

Q_PROPERTY(qreal step READ getStep WRITE setStep)
Q_PROPERTY(double step READ getStep WRITE setStep)
Q_PROPERTY(int borderWidth READ getBorderWidth WRITE setBorderWidth)
Q_PROPERTY(int borderRadius READ getBorderRadius WRITE setBorderRadius)
Q_PROPERTY(int bgRadius READ getBgRadius WRITE setBgRadius)
Q_PROPERTY(int headRadius READ getHeadRadius WRITE setHeadRadius)
Expand Down Expand Up @@ -60,74 +55,78 @@ private slots:
void updateValue();

private:
qreal minValue; //最小值
qreal maxValue; //最大值
qreal value; //目标电量
qreal alarmValue; //电池电量警戒值
double minValue; //最小值
double maxValue; //最大值
double value; //目标电量
double alarmValue; //电池电量警戒值

qreal step; //每次移动的步长
int borderRadius; //边框圆角角度
int bgRadius; //背景进度圆角角度
int headRadius; //头部圆角角度
double step; //每次移动的步长
int borderWidth; //边框粗细
int borderRadius; //边框圆角角度
int bgRadius; //背景进度圆角角度
int headRadius; //头部圆角角度

QColor borderColorStart; //边框渐变开始颜色
QColor borderColorEnd; //边框渐变结束颜色
QColor borderColorStart; //边框渐变开始颜色
QColor borderColorEnd; //边框渐变结束颜色

QColor alarmColorStart; //电池低电量时的渐变开始颜色
QColor alarmColorEnd; //电池低电量时的渐变结束颜色
QColor alarmColorStart; //电池低电量时的渐变开始颜色
QColor alarmColorEnd; //电池低电量时的渐变结束颜色

QColor normalColorStart; //电池正常电量时的渐变开始颜色
QColor normalColorEnd; //电池正常电量时的渐变结束颜色
QColor normalColorStart; //电池正常电量时的渐变开始颜色
QColor normalColorEnd; //电池正常电量时的渐变结束颜色

bool isForward; //是否往前移
qreal currentValue; //当前电量
QRectF batteryRect; //电池主体区域
QTimer *timer; //绘制定时器
bool isForward; //是否往前移
double currentValue; //当前电量
QRectF batteryRect; //电池主体区域
QTimer *timer; //绘制定时器

public:
qreal getMinValue() const;
qreal getMaxValue() const;
qreal getValue() const;
qreal getAlarmValue() const;
double getMinValue() const;
double getMaxValue() const;
double getValue() const;
double getAlarmValue() const;

qreal getStep() const;
int getBorderRadius() const;
int getBgRadius() const;
int getHeadRadius() const;
double getStep() const;
int getBorderWidth() const;
int getBorderRadius() const;
int getBgRadius() const;
int getHeadRadius() const;

QColor getBorderColorStart()const;
QColor getBorderColorEnd() const;
QColor getBorderColorStart() const;
QColor getBorderColorEnd() const;

QColor getAlarmColorStart() const;
QColor getAlarmColorEnd() const;
QColor getAlarmColorStart() const;
QColor getAlarmColorEnd() const;

QColor getNormalColorStart()const;
QColor getNormalColorEnd() const;
QColor getNormalColorStart() const;
QColor getNormalColorEnd() const;

QSize sizeHint() const;
QSize minimumSizeHint() const;
QSize sizeHint() const;
QSize minimumSizeHint() const;

public Q_SLOTS:
//设置范围值
void setRange(qreal minValue, qreal maxValue);
void setRange(double minValue, double maxValue);
void setRange(int minValue, int maxValue);

//设置最大最小值
void setMinValue(qreal minValue);
void setMaxValue(qreal maxValue);
void setMinValue(double minValue);
void setMaxValue(double maxValue);

//设置电池电量值
void setValue(qreal value);
void setValue(double value);
void setValue(int value);

//设置电池电量警戒值
void setAlarmValue(qreal alarmValue);
void setAlarmValue(double alarmValue);
void setAlarmValue(int alarmValue);

//设置步长
void setStep(qreal step);
void setStep(double step);
void setStep(int step);

//设置边框粗细
void setBorderWidth(int borderWidth);
//设置边框圆角角度
void setBorderRadius(int borderRadius);
//设置背景圆角角度
Expand All @@ -148,7 +147,7 @@ public Q_SLOTS:
void setNormalColorEnd(const QColor &normalColorEnd);

Q_SIGNALS:
void valueChanged(qreal value);
void valueChanged(double value);
};

#endif // BATTERY_H
Loading

0 comments on commit e8195be

Please sign in to comment.