彻底改版2.0

This commit is contained in:
feiyangqingyun
2021-11-17 16:41:30 +08:00
parent a7f4347959
commit ebfd531a91
2622 changed files with 8915 additions and 7263 deletions

BIN
control/0snap/battery.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 126 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 56 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

BIN
control/0snap/ipaddress.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 58 KiB

BIN
control/0snap/navbutton.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 55 KiB

BIN
control/0snap/savelog.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 80 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 54 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 45 KiB

BIN
control/0snap/zhtopy.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

407
control/battery/battery.cpp Normal file
View File

@@ -0,0 +1,407 @@
#pragma execution_character_set("utf-8")
#include "battery.h"
#include "qpainter.h"
#include "qtimer.h"
#include "qdebug.h"
Battery::Battery(QWidget *parent) : QWidget(parent)
{
minValue = 0;
maxValue = 100;
value = 0;
alarmValue = 30;
step = 0.5;
borderWidth = 5;
borderRadius = 8;
bgRadius = 5;
headRadius = 3;
borderColorStart = QColor(100, 100, 100);
borderColorEnd = QColor(80, 80, 80);
alarmColorStart = QColor(250, 118, 113);
alarmColorEnd = QColor(204, 38, 38);
normalColorStart = QColor(50, 205, 51);
normalColorEnd = QColor(60, 179, 133);
isForward = false;
currentValue = 0;
timer = new QTimer(this);
timer->setInterval(10);
connect(timer, SIGNAL(timeout()), this, SLOT(updateValue()));
}
Battery::~Battery()
{
if (timer->isActive()) {
timer->stop();
}
}
void Battery::paintEvent(QPaintEvent *)
{
//绘制准备工作,启用反锯齿
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
//绘制边框
drawBorder(&painter);
//绘制背景
drawBg(&painter);
//绘制头部
drawHead(&painter);
}
void Battery::drawBorder(QPainter *painter)
{
painter->save();
double headWidth = width() / 15;
double batteryWidth = width() - headWidth;
//绘制电池边框
QPointF topLeft(borderWidth, borderWidth);
QPointF bottomRight(batteryWidth, height() - borderWidth);
batteryRect = QRectF(topLeft, bottomRight);
painter->setPen(QPen(borderColorStart, borderWidth));
painter->setBrush(Qt::NoBrush);
painter->drawRoundedRect(batteryRect, borderRadius, borderRadius);
painter->restore();
}
void Battery::drawBg(QPainter *painter)
{
if (value == minValue) {
return;
}
painter->save();
QLinearGradient batteryGradient(QPointF(0, 0), QPointF(0, height()));
if (currentValue <= alarmValue) {
batteryGradient.setColorAt(0.0, alarmColorStart);
batteryGradient.setColorAt(1.0, alarmColorEnd);
} else {
batteryGradient.setColorAt(0.0, normalColorStart);
batteryGradient.setColorAt(1.0, normalColorEnd);
}
int margin = qMin(width(), height()) / 20;
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 + borderWidth, batteryRect.bottomRight().y() - margin);
QRectF rect(topLeft, bottomRight);
painter->setPen(Qt::NoPen);
painter->setBrush(batteryGradient);
painter->drawRoundedRect(rect, bgRadius, bgRadius);
painter->restore();
}
void Battery::drawHead(QPainter *painter)
{
painter->save();
QPointF headRectTopLeft(batteryRect.topRight().x(), height() / 3);
QPointF headRectBottomRight(width(), height() - height() / 3);
QRectF headRect(headRectTopLeft, headRectBottomRight);
QLinearGradient headRectGradient(headRect.topLeft(), headRect.bottomLeft());
headRectGradient.setColorAt(0.0, borderColorStart);
headRectGradient.setColorAt(1.0, borderColorEnd);
painter->setPen(Qt::NoPen);
painter->setBrush(headRectGradient);
painter->drawRoundedRect(headRect, headRadius, headRadius);
painter->restore();
}
void Battery::updateValue()
{
if (isForward) {
currentValue -= step;
if (currentValue <= value) {
currentValue = value;
timer->stop();
}
} else {
currentValue += step;
if (currentValue >= value) {
currentValue = value;
timer->stop();
}
}
this->update();
}
double Battery::getMinValue() const
{
return this->minValue;
}
double Battery::getMaxValue() const
{
return this->maxValue;
}
double Battery::getValue() const
{
return this->value;
}
double Battery::getAlarmValue() const
{
return this->alarmValue;
}
double Battery::getStep() const
{
return this->step;
}
int Battery::getBorderWidth() const
{
return this->borderWidth;
}
int Battery::getBorderRadius() const
{
return this->borderRadius;
}
int Battery::getBgRadius() const
{
return this->bgRadius;
}
int Battery::getHeadRadius() const
{
return this->headRadius;
}
QColor Battery::getBorderColorStart() const
{
return this->borderColorStart;
}
QColor Battery::getBorderColorEnd() const
{
return this->borderColorEnd;
}
QColor Battery::getAlarmColorStart() const
{
return this->alarmColorStart;
}
QColor Battery::getAlarmColorEnd() const
{
return this->alarmColorEnd;
}
QColor Battery::getNormalColorStart() const
{
return this->normalColorStart;
}
QColor Battery::getNormalColorEnd() const
{
return this->normalColorEnd;
}
QSize Battery::sizeHint() const
{
return QSize(150, 80);
}
QSize Battery::minimumSizeHint() const
{
return QSize(30, 10);
}
void Battery::setRange(double minValue, double maxValue)
{
//如果最小值大于或者等于最大值则不设置
if (minValue >= maxValue) {
return;
}
this->minValue = minValue;
this->maxValue = maxValue;
//如果目标值不在范围值内,则重新设置目标值
//值小于最小值则取最小值,大于最大值则取最大值
if (value < minValue) {
setValue(minValue);
} else if (value > maxValue) {
setValue(maxValue);
}
this->update();
}
void Battery::setRange(int minValue, int maxValue)
{
setRange((double)minValue, (double)maxValue);
}
void Battery::setMinValue(double minValue)
{
setRange(minValue, maxValue);
}
void Battery::setMaxValue(double maxValue)
{
setRange(minValue, maxValue);
}
void Battery::setValue(double value)
{
//值和当前值一致则无需处理
if (value == this->value) {
return;
}
//值小于最小值则取最小值,大于最大值则取最大值
if (value < minValue) {
value = minValue;
} else if (value > maxValue) {
value = maxValue;
}
if (value > currentValue) {
isForward = false;
} 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((double)value);
}
void Battery::setAlarmValue(double alarmValue)
{
if (this->alarmValue != alarmValue) {
this->alarmValue = alarmValue;
this->update();
}
}
void Battery::setAlarmValue(int alarmValue)
{
setAlarmValue((double)alarmValue);
}
void Battery::setStep(double step)
{
if (this->step != step) {
this->step = step;
this->update();
}
}
void Battery::setStep(int step)
{
setStep((double)step);
}
void Battery::setBorderWidth(int borderWidth)
{
if (this->borderWidth != borderWidth) {
this->borderWidth = borderWidth;
this->update();
}
}
void Battery::setBorderRadius(int borderRadius)
{
if (this->borderRadius != borderRadius) {
this->borderRadius = borderRadius;
this->update();
}
}
void Battery::setBgRadius(int bgRadius)
{
if (this->bgRadius != bgRadius) {
this->bgRadius = bgRadius;
this->update();
}
}
void Battery::setHeadRadius(int headRadius)
{
if (this->headRadius != headRadius) {
this->headRadius = headRadius;
this->update();
}
}
void Battery::setBorderColorStart(const QColor &borderColorStart)
{
if (this->borderColorStart != borderColorStart) {
this->borderColorStart = borderColorStart;
this->update();
}
}
void Battery::setBorderColorEnd(const QColor &borderColorEnd)
{
if (this->borderColorEnd != borderColorEnd) {
this->borderColorEnd = borderColorEnd;
this->update();
}
}
void Battery::setAlarmColorStart(const QColor &alarmColorStart)
{
if (this->alarmColorStart != alarmColorStart) {
this->alarmColorStart = alarmColorStart;
this->update();
}
}
void Battery::setAlarmColorEnd(const QColor &alarmColorEnd)
{
if (this->alarmColorEnd != alarmColorEnd) {
this->alarmColorEnd = alarmColorEnd;
this->update();
}
}
void Battery::setNormalColorStart(const QColor &normalColorStart)
{
if (this->normalColorStart != normalColorStart) {
this->normalColorStart = normalColorStart;
this->update();
}
}
void Battery::setNormalColorEnd(const QColor &normalColorEnd)
{
if (this->normalColorEnd != normalColorEnd) {
this->normalColorEnd = normalColorEnd;
this->update();
}
}

154
control/battery/battery.h Normal file
View File

@@ -0,0 +1,154 @@
#ifndef BATTERY_H
#define BATTERY_H
/**
* 电池电量控件 作者:feiyangqingyun(QQ:517216493) 2016-10-23
* 1. 可设置电池电量,动态切换电池电量变化。
* 2. 可设置电池电量警戒值。
* 3. 可设置电池电量正常颜色和报警颜色。
* 4. 可设置边框渐变颜色。
* 5. 可设置电量变化时每次移动的步长。
* 6. 可设置边框圆角角度、背景进度圆角角度、头部圆角角度。
*/
#include <QWidget>
#ifdef quc
class Q_DECL_EXPORT Battery : public QWidget
#else
class Battery : public QWidget
#endif
{
Q_OBJECT
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(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)
Q_PROPERTY(QColor borderColorStart READ getBorderColorStart WRITE setBorderColorStart)
Q_PROPERTY(QColor borderColorEnd READ getBorderColorEnd WRITE setBorderColorEnd)
Q_PROPERTY(QColor alarmColorStart READ getAlarmColorStart WRITE setAlarmColorStart)
Q_PROPERTY(QColor alarmColorEnd READ getAlarmColorEnd WRITE setAlarmColorEnd)
Q_PROPERTY(QColor normalColorStart READ getNormalColorStart WRITE setNormalColorStart)
Q_PROPERTY(QColor normalColorEnd READ getNormalColorEnd WRITE setNormalColorEnd)
public:
explicit Battery(QWidget *parent = 0);
~Battery();
protected:
void paintEvent(QPaintEvent *);
void drawBorder(QPainter *painter);
void drawBg(QPainter *painter);
void drawHead(QPainter *painter);
private slots:
void updateValue();
private:
double minValue; //最小值
double maxValue; //最大值
double value; //目标电量
double alarmValue; //电池电量警戒值
double step; //每次移动的步长
int borderWidth; //边框粗细
int borderRadius; //边框圆角角度
int bgRadius; //背景进度圆角角度
int headRadius; //头部圆角角度
QColor borderColorStart; //边框渐变开始颜色
QColor borderColorEnd; //边框渐变结束颜色
QColor alarmColorStart; //电池低电量时的渐变开始颜色
QColor alarmColorEnd; //电池低电量时的渐变结束颜色
QColor normalColorStart; //电池正常电量时的渐变开始颜色
QColor normalColorEnd; //电池正常电量时的渐变结束颜色
bool isForward; //是否往前移
double currentValue; //当前电量
QRectF batteryRect; //电池主体区域
QTimer *timer; //绘制定时器
public:
double getMinValue() const;
double getMaxValue() const;
double getValue() const;
double getAlarmValue() const;
double getStep() const;
int getBorderWidth() const;
int getBorderRadius() const;
int getBgRadius() const;
int getHeadRadius() const;
QColor getBorderColorStart() const;
QColor getBorderColorEnd() const;
QColor getAlarmColorStart() const;
QColor getAlarmColorEnd() const;
QColor getNormalColorStart() const;
QColor getNormalColorEnd() const;
QSize sizeHint() const;
QSize minimumSizeHint() const;
public Q_SLOTS:
//设置范围值
void setRange(double minValue, double maxValue);
void setRange(int minValue, int maxValue);
//设置最大最小值
void setMinValue(double minValue);
void setMaxValue(double maxValue);
//设置电池电量值
void setValue(double value);
void setValue(int value);
//设置电池电量警戒值
void setAlarmValue(double alarmValue);
void setAlarmValue(int alarmValue);
//设置步长
void setStep(double step);
void setStep(int step);
//设置边框粗细
void setBorderWidth(int borderWidth);
//设置边框圆角角度
void setBorderRadius(int borderRadius);
//设置背景圆角角度
void setBgRadius(int bgRadius);
//设置头部圆角角度
void setHeadRadius(int headRadius);
//设置边框渐变颜色
void setBorderColorStart(const QColor &borderColorStart);
void setBorderColorEnd(const QColor &borderColorEnd);
//设置电池电量报警时的渐变颜色
void setAlarmColorStart(const QColor &alarmColorStart);
void setAlarmColorEnd(const QColor &alarmColorEnd);
//设置电池电量正常时的渐变颜色
void setNormalColorStart(const QColor &normalColorStart);
void setNormalColorEnd(const QColor &normalColorEnd);
Q_SIGNALS:
void valueChanged(double value);
};
#endif // BATTERY_H

View File

@@ -0,0 +1,17 @@
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
greaterThan(QT_MAJOR_VERSION, 5): QT += core5compat
TARGET = battery
TEMPLATE = app
DESTDIR = $$PWD/../bin
CONFIG += warn_off
SOURCES += main.cpp
SOURCES += frmbattery.cpp
SOURCES += battery.cpp
HEADERS += frmbattery.h
HEADERS += battery.h
FORMS += frmbattery.ui

View File

@@ -0,0 +1,21 @@
#pragma execution_character_set("utf-8")
#include "frmbattery.h"
#include "ui_frmbattery.h"
frmBattery::frmBattery(QWidget *parent) : QWidget(parent), ui(new Ui::frmBattery)
{
ui->setupUi(this);
this->initForm();
}
frmBattery::~frmBattery()
{
delete ui;
}
void frmBattery::initForm()
{
connect(ui->horizontalSlider, SIGNAL(valueChanged(int)), ui->battery, SLOT(setValue(int)));
ui->horizontalSlider->setValue(30);
}

View File

@@ -0,0 +1,25 @@
#ifndef FRMBATTERY_H
#define FRMBATTERY_H
#include <QWidget>
namespace Ui {
class frmBattery;
}
class frmBattery : public QWidget
{
Q_OBJECT
public:
explicit frmBattery(QWidget *parent = 0);
~frmBattery();
private:
Ui::frmBattery *ui;
private slots:
void initForm();
};
#endif // FRMBATTERY_H

View File

@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>frmBattery</class>
<widget class="QWidget" name="frmBattery">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<widget class="Battery" name="battery" native="true">
<property name="geometry">
<rect>
<x>9</x>
<y>9</y>
<width>482</width>
<height>257</height>
</rect>
</property>
</widget>
<widget class="QSlider" name="horizontalSlider">
<property name="geometry">
<rect>
<x>9</x>
<y>272</y>
<width>481</width>
<height>19</height>
</rect>
</property>
<property name="maximum">
<number>100</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="invertedControls">
<bool>false</bool>
</property>
</widget>
</widget>
<customwidgets>
<customwidget>
<class>Battery</class>
<extends>QWidget</extends>
<header>battery.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

31
control/battery/main.cpp Normal file
View File

@@ -0,0 +1,31 @@
#pragma execution_character_set("utf-8")
#include "frmbattery.h"
#include <QApplication>
#include <QTextCodec>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setFont(QFont("Microsoft Yahei", 9));
#if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
#if _MSC_VER
QTextCodec *codec = QTextCodec::codecForName("gbk");
#else
QTextCodec *codec = QTextCodec::codecForName("utf-8");
#endif
QTextCodec::setCodecForLocale(codec);
QTextCodec::setCodecForCStrings(codec);
QTextCodec::setCodecForTr(codec);
#else
QTextCodec *codec = QTextCodec::codecForName("utf-8");
QTextCodec::setCodecForLocale(codec);
#endif
frmBattery w;
w.setWindowTitle("电池电量控件");
w.show();
return a.exec();
}

12
control/control.pro Normal file
View File

@@ -0,0 +1,12 @@
TEMPLATE = subdirs
SUBDIRS += battery
SUBDIRS += devicebutton
SUBDIRS += devicesizetable
SUBDIRS += imageswitch
SUBDIRS += ipaddress
SUBDIRS += lightbutton
SUBDIRS += navbutton
SUBDIRS += savelog
SUBDIRS += saveruntime
SUBDIRS += smoothcurve
SUBDIRS += zhtopy

View File

@@ -0,0 +1,217 @@
#pragma execution_character_set("utf-8")
#include "devicebutton.h"
#include "qpainter.h"
#include "qevent.h"
#include "qtimer.h"
#include "qdebug.h"
DeviceButton::DeviceButton(QWidget *parent) : QWidget(parent)
{
canMove = false;
text = "1";
buttonStyle = ButtonStyle_Police;
buttonColor = ButtonColor_Green;
type = "police";
imgName = QString(":/image/devicebutton/devicebutton_green_%1.png").arg(type);
isDark = false;
timer = new QTimer(this);
timer->setInterval(500);
connect(timer, SIGNAL(timeout()), this, SLOT(checkAlarm()));
this->installEventFilter(this);
}
DeviceButton::~DeviceButton()
{
if (timer->isActive()) {
timer->stop();
}
}
void DeviceButton::paintEvent(QPaintEvent *)
{
double width = this->width();
double height = this->height();
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
//绘制背景图
QImage img(imgName);
if (!img.isNull()) {
img = img.scaled(width, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
painter.drawImage(0, 0, img);
}
//计算字体
QFont font;
font.setPixelSize(height * 0.37);
font.setBold(true);
//自动计算文字绘制区域,绘制防区号
QRectF rect = this->rect();
if (buttonStyle == ButtonStyle_Police) {
double y = (30 * height / 60);
rect = QRectF(0, y, width, height - y);
} else if (buttonStyle == ButtonStyle_Bubble) {
double y = (8 * height / 60);
rect = QRectF(0, 0, width, height - y);
} else if (buttonStyle == ButtonStyle_Bubble2) {
double y = (13 * height / 60);
rect = QRectF(0, 0, width, height - y);
font.setPixelSize(width * 0.33);
} else if (buttonStyle == ButtonStyle_Msg) {
double y = (17 * height / 60);
rect = QRectF(0, 0, width, height - y);
} else if (buttonStyle == ButtonStyle_Msg2) {
double y = (17 * height / 60);
rect = QRectF(0, 0, width, height - y);
}
//绘制文字标识
painter.setFont(font);
painter.setPen(Qt::white);
painter.drawText(rect, Qt::AlignCenter, text);
}
bool DeviceButton::eventFilter(QObject *watched, QEvent *event)
{
if (canMove) {
static QPoint lastPoint;
static bool isPressed = false;
if (event->type() == QEvent::MouseButtonPress) {
QMouseEvent *e = static_cast<QMouseEvent *>(event);
if (this->rect().contains(e->pos()) && (e->button() == Qt::LeftButton)) {
lastPoint = e->pos();
isPressed = true;
}
} else if (event->type() == QEvent::MouseMove && isPressed) {
QMouseEvent *e = static_cast<QMouseEvent *>(event);
int dx = e->pos().x() - lastPoint.x();
int dy = e->pos().y() - lastPoint.y();
this->move(this->x() + dx, this->y() + dy);
return true;
} else if (event->type() == QEvent::MouseButtonRelease && isPressed) {
isPressed = false;
}
}
if (event->type() == QEvent::MouseButtonPress) {
emit clicked();
} else if (event->type() == QEvent::MouseButtonDblClick) {
emit doubleClicked();
}
return QWidget::eventFilter(watched, event);
}
bool DeviceButton::getCanMove() const
{
return this->canMove;
}
QString DeviceButton::getText() const
{
return this->text;
}
DeviceButton::ButtonStyle DeviceButton::getButtonStyle() const
{
return this->buttonStyle;
}
DeviceButton::ButtonColor DeviceButton::getButtonColor() const
{
return this->buttonColor;
}
QSize DeviceButton::sizeHint() const
{
return QSize(50, 50);
}
QSize DeviceButton::minimumSizeHint() const
{
return QSize(10, 10);
}
void DeviceButton::checkAlarm()
{
if (isDark) {
imgName = QString(":/image/devicebutton/devicebutton_black_%1.png").arg(type);
} else {
imgName = QString(":/image/devicebutton/devicebutton_red_%1.png").arg(type);
}
isDark = !isDark;
this->update();
}
void DeviceButton::setCanMove(bool canMove)
{
this->canMove = canMove;
}
void DeviceButton::setText(const QString &text)
{
if (this->text != text) {
this->text = text;
this->update();
}
}
void DeviceButton::setButtonStyle(const DeviceButton::ButtonStyle &buttonStyle)
{
this->buttonStyle = buttonStyle;
if (buttonStyle == ButtonStyle_Circle) {
type = "circle";
} else if (buttonStyle == ButtonStyle_Police) {
type = "police";
} else if (buttonStyle == ButtonStyle_Bubble) {
type = "bubble";
} else if (buttonStyle == ButtonStyle_Bubble2) {
type = "bubble2";
} else if (buttonStyle == ButtonStyle_Msg) {
type = "msg";
} else if (buttonStyle == ButtonStyle_Msg2) {
type = "msg2";
} else {
type = "circle";
}
setButtonColor(buttonColor);
}
void DeviceButton::setButtonColor(const DeviceButton::ButtonColor &buttonColor)
{
this->buttonColor = buttonColor;
isDark = false;
if (timer->isActive()) {
timer->stop();
}
if (buttonColor == ButtonColor_Green) {
imgName = QString(":/image/devicebutton/devicebutton_green_%1.png").arg(type);
} else if (buttonColor == ButtonColor_Blue) {
imgName = QString(":/image/devicebutton/devicebutton_blue_%1.png").arg(type);
} else if (buttonColor == ButtonColor_Gray) {
imgName = QString(":/image/devicebutton/devicebutton_gray_%1.png").arg(type);
} else if (buttonColor == ButtonColor_Black) {
imgName = QString(":/image/devicebutton/devicebutton_black_%1.png").arg(type);
} else if (buttonColor == ButtonColor_Purple) {
imgName = QString(":/image/devicebutton/devicebutton_purple_%1.png").arg(type);
} else if (buttonColor == ButtonColor_Yellow) {
imgName = QString(":/image/devicebutton/devicebutton_yellow_%1.png").arg(type);
} else if (buttonColor == ButtonColor_Red) {
checkAlarm();
if (!timer->isActive()) {
timer->start();
}
}
this->update();
}

View File

@@ -0,0 +1,101 @@
#ifndef DEVICEBUTTON_H
#define DEVICEBUTTON_H
/**
* 设备按钮控件 作者:feiyangqingyun(QQ:517216493) 2018-07-02
* 1. 可设置按钮样式 圆形、警察、气泡、气泡2、消息、消息2。
* 2. 可设置按钮颜色 布防、撤防、报警、旁路、故障。
* 3. 可设置报警切换。
* 4. 可设置显示的防区号。
* 5. 可设置是否可鼠标拖动。
* 6. 发出单击和双击信号。
*/
#include <QWidget>
#ifdef quc
class Q_DECL_EXPORT DeviceButton : public QWidget
#else
class DeviceButton : public QWidget
#endif
{
Q_OBJECT
Q_ENUMS(ButtonStyle)
Q_ENUMS(ButtonColor)
Q_PROPERTY(bool canMove READ getCanMove WRITE setCanMove)
Q_PROPERTY(QString text READ getText WRITE setText)
Q_PROPERTY(ButtonStyle buttonStyle READ getButtonStyle WRITE setButtonStyle)
Q_PROPERTY(ButtonColor buttonColor READ getButtonColor WRITE setButtonColor)
public:
//设备按钮样式
enum ButtonStyle {
ButtonStyle_Circle = 0, //圆形
ButtonStyle_Police = 1, //警察
ButtonStyle_Bubble = 2, //气泡
ButtonStyle_Bubble2 = 3, //气泡2
ButtonStyle_Msg = 4, //消息
ButtonStyle_Msg2 = 5 //消息2
};
//设备按钮颜色
enum ButtonColor {
ButtonColor_Green = 0, //绿色 激活状态
ButtonColor_Blue = 1, //蓝色 在线状态
ButtonColor_Red = 2, //红色 报警状态
ButtonColor_Gray = 3, //灰色 离线状态
ButtonColor_Black = 4, //黑色 故障状态
ButtonColor_Purple = 5, //紫色 其他状态
ButtonColor_Yellow = 6 //黄色 其他状态
};
explicit DeviceButton(QWidget *parent = 0);
~DeviceButton();
protected:
void paintEvent(QPaintEvent *);
bool eventFilter(QObject *watched, QEvent *event);
private:
bool canMove; //是否可移动
QString text; //显示文字
ButtonStyle buttonStyle; //按钮样式
ButtonColor buttonColor; //按钮颜色
QString type; //图片末尾类型
QString imgName; //背景图片名称
bool isDark; //是否加深报警
QTimer *timer; //报警闪烁定时器
private slots:
void checkAlarm(); //切换报警状态
public:
bool getCanMove() const;
QString getText() const;
ButtonStyle getButtonStyle() const;
ButtonColor getButtonColor() const;
QSize sizeHint() const;
QSize minimumSizeHint() const;
public Q_SLOTS:
//设置可移动
void setCanMove(bool canMove);
//设置显示文字
void setText(const QString &text);
//设置样式
void setButtonStyle(const ButtonStyle &buttonStyle);
//设置颜色
void setButtonColor(const ButtonColor &buttonColor);
Q_SIGNALS:
void clicked();
void doubleClicked();
};
#endif //DEVICEBUTTON_H

View File

@@ -0,0 +1,19 @@
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
greaterThan(QT_MAJOR_VERSION, 5): QT += core5compat
TARGET = devicebutton
TEMPLATE = app
DESTDIR = $$PWD/../bin
CONFIG += warn_off
SOURCES += main.cpp
SOURCES += frmdevicebutton.cpp
SOURCES += devicebutton.cpp
HEADERS += frmdevicebutton.h
HEADERS += devicebutton.h
FORMS += frmdevicebutton.ui
RESOURCES += main.qrc

View File

@@ -0,0 +1,71 @@
#include "frmdevicebutton.h"
#include "ui_frmdevicebutton.h"
#include "devicebutton.h"
#include "qdebug.h"
frmDeviceButton::frmDeviceButton(QWidget *parent) : QWidget(parent), ui(new Ui::frmDeviceButton)
{
ui->setupUi(this);
this->initForm();
}
frmDeviceButton::~frmDeviceButton()
{
delete ui;
}
void frmDeviceButton::initForm()
{
//设置背景地图
ui->labMap->setStyleSheet("border-image:url(:/image/bg_call.jpg);");
btn1 = new DeviceButton(ui->labMap);
btn1->setText("#1");
btn1->setGeometry(5, 5, 35, 35);
btn2 = new DeviceButton(ui->labMap);
btn2->setText("#2");
btn2->setGeometry(45, 5, 35, 35);
btn3 = new DeviceButton(ui->labMap);
btn3->setText("#3");
btn3->setGeometry(85, 5, 35, 35);
btnStyle << ui->btnCircle << ui->btnPolice << ui->btnBubble << ui->btnBubble2 << ui->btnMsg << ui->btnMsg2;
foreach (QPushButton *btn, btnStyle) {
connect(btn, SIGNAL(clicked(bool)), this, SLOT(changeStyle()));
}
btnColor << ui->btnGreen << ui->btnBlue << ui->btnRed << ui->btnGray << ui->btnBlack << ui->btnPurple << ui->btnYellow;
foreach (QPushButton *btn, btnColor) {
connect(btn, SIGNAL(clicked(bool)), this, SLOT(changeColor()));
}
}
void frmDeviceButton::changeStyle()
{
QPushButton *btn = (QPushButton *)sender();
int index = btnStyle.indexOf(btn);
DeviceButton::ButtonStyle style = (DeviceButton::ButtonStyle)index;
btn1->setButtonStyle(style);
btn2->setButtonStyle(style);
btn3->setButtonStyle(style);
}
void frmDeviceButton::changeColor()
{
QPushButton *btn = (QPushButton *)sender();
int index = btnColor.indexOf(btn);
DeviceButton::ButtonColor style = (DeviceButton::ButtonColor)index;
btn1->setButtonColor(style);
btn2->setButtonColor(style);
btn3->setButtonColor(style);
}
void frmDeviceButton::on_ckCanMove_stateChanged(int arg1)
{
bool canMove = (arg1 != 0);
btn1->setCanMove(canMove);
btn2->setCanMove(canMove);
btn3->setCanMove(canMove);
}

View File

@@ -0,0 +1,36 @@
#ifndef FRMDEVICEBUTTON_H
#define FRMDEVICEBUTTON_H
#include <QWidget>
class DeviceButton;
class QPushButton;
namespace Ui {
class frmDeviceButton;
}
class frmDeviceButton : public QWidget
{
Q_OBJECT
public:
explicit frmDeviceButton(QWidget *parent = 0);
~frmDeviceButton();
private slots:
void initForm();
void changeStyle();
void changeColor();
void on_ckCanMove_stateChanged(int arg1);
private:
Ui::frmDeviceButton *ui;
DeviceButton *btn1;
DeviceButton *btn2;
DeviceButton *btn3;
QList<QPushButton *> btnStyle;
QList<QPushButton *> btnColor;
};
#endif // FRMDEVICEBUTTON_H

View File

@@ -0,0 +1,174 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>frmDeviceButton</class>
<widget class="QWidget" name="frmDeviceButton">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="labMap">
<property name="frameShape">
<enum>QFrame::Box</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Sunken</enum>
</property>
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QFrame" name="frame">
<property name="maximumSize">
<size>
<width>100</width>
<height>16777215</height>
</size>
</property>
<property name="frameShape">
<enum>QFrame::Box</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Sunken</enum>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QPushButton" name="btnCircle">
<property name="text">
<string>圆形</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnPolice">
<property name="text">
<string>警察</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnBubble">
<property name="text">
<string>气泡</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnBubble2">
<property name="text">
<string>气泡2</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnMsg">
<property name="text">
<string>消息</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnMsg2">
<property name="text">
<string>消息2</string>
</property>
</widget>
</item>
<item>
<widget class="Line" name="line">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnGreen">
<property name="text">
<string>绿色</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnBlue">
<property name="text">
<string>蓝色</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnRed">
<property name="text">
<string>红色</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnGray">
<property name="text">
<string>灰色</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnBlack">
<property name="text">
<string>黑色</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnPurple">
<property name="text">
<string>紫色</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnYellow">
<property name="text">
<string>黄色</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="ckCanMove">
<property name="text">
<string>可移动</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>

Binary file not shown.

After

Width:  |  Height:  |  Size: 203 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 551 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 517 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1015 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 400 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 404 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 551 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 517 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 883 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 397 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 404 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 551 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 517 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 818 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 397 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 404 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 551 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 517 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 397 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 404 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 580 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 547 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 782 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 400 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 412 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 551 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 517 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 917 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 397 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 404 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 580 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 547 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 782 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 400 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 412 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

View File

@@ -0,0 +1,31 @@
#pragma execution_character_set("utf-8")
#include "frmdevicebutton.h"
#include <QApplication>
#include <QTextCodec>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setFont(QFont("Microsoft Yahei", 9));
#if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
#if _MSC_VER
QTextCodec *codec = QTextCodec::codecForName("gbk");
#else
QTextCodec *codec = QTextCodec::codecForName("utf-8");
#endif
QTextCodec::setCodecForLocale(codec);
QTextCodec::setCodecForCStrings(codec);
QTextCodec::setCodecForTr(codec);
#else
QTextCodec *codec = QTextCodec::codecForName("utf-8");
QTextCodec::setCodecForLocale(codec);
#endif
frmDeviceButton w;
w.setWindowTitle("设备按钮控件");
w.show();
return a.exec();
}

View File

@@ -0,0 +1,47 @@
<RCC>
<qresource prefix="/">
<file>image/bg_call.jpg</file>
<file>image/devicebutton/devicebutton_black_bubble.png</file>
<file>image/devicebutton/devicebutton_black_bubble2.png</file>
<file>image/devicebutton/devicebutton_black_circle.png</file>
<file>image/devicebutton/devicebutton_black_msg.png</file>
<file>image/devicebutton/devicebutton_black_msg2.png</file>
<file>image/devicebutton/devicebutton_black_police.png</file>
<file>image/devicebutton/devicebutton_blue_bubble.png</file>
<file>image/devicebutton/devicebutton_blue_bubble2.png</file>
<file>image/devicebutton/devicebutton_blue_circle.png</file>
<file>image/devicebutton/devicebutton_blue_msg.png</file>
<file>image/devicebutton/devicebutton_blue_msg2.png</file>
<file>image/devicebutton/devicebutton_blue_police.png</file>
<file>image/devicebutton/devicebutton_gray_bubble.png</file>
<file>image/devicebutton/devicebutton_gray_bubble2.png</file>
<file>image/devicebutton/devicebutton_gray_circle.png</file>
<file>image/devicebutton/devicebutton_gray_msg.png</file>
<file>image/devicebutton/devicebutton_gray_msg2.png</file>
<file>image/devicebutton/devicebutton_gray_police.png</file>
<file>image/devicebutton/devicebutton_green_bubble.png</file>
<file>image/devicebutton/devicebutton_green_bubble2.png</file>
<file>image/devicebutton/devicebutton_green_circle.png</file>
<file>image/devicebutton/devicebutton_green_msg.png</file>
<file>image/devicebutton/devicebutton_green_msg2.png</file>
<file>image/devicebutton/devicebutton_green_police.png</file>
<file>image/devicebutton/devicebutton_red_bubble.png</file>
<file>image/devicebutton/devicebutton_red_bubble2.png</file>
<file>image/devicebutton/devicebutton_red_circle.png</file>
<file>image/devicebutton/devicebutton_red_msg.png</file>
<file>image/devicebutton/devicebutton_red_msg2.png</file>
<file>image/devicebutton/devicebutton_red_police.png</file>
<file>image/devicebutton/devicebutton_purple_bubble.png</file>
<file>image/devicebutton/devicebutton_purple_bubble2.png</file>
<file>image/devicebutton/devicebutton_purple_circle.png</file>
<file>image/devicebutton/devicebutton_purple_msg.png</file>
<file>image/devicebutton/devicebutton_purple_msg2.png</file>
<file>image/devicebutton/devicebutton_purple_police.png</file>
<file>image/devicebutton/devicebutton_yellow_bubble.png</file>
<file>image/devicebutton/devicebutton_yellow_bubble2.png</file>
<file>image/devicebutton/devicebutton_yellow_circle.png</file>
<file>image/devicebutton/devicebutton_yellow_msg.png</file>
<file>image/devicebutton/devicebutton_yellow_msg2.png</file>
<file>image/devicebutton/devicebutton_yellow_police.png</file>
</qresource>
</RCC>

View File

@@ -0,0 +1,291 @@
#pragma execution_character_set("utf-8")
#include "devicesizetable.h"
#include "qprocess.h"
#include "qtablewidget.h"
#include "qheaderview.h"
#include "qfileinfo.h"
#include "qdir.h"
#include "qprogressbar.h"
#include "qtimer.h"
#include "qdebug.h"
#ifdef Q_OS_WIN
#include "windows.h"
#endif
#define GB (1024 * 1024 * 1024)
#define MB (1024 * 1024)
#define KB (1024)
DeviceSizeTable::DeviceSizeTable(QWidget *parent) : QTableWidget(parent)
{
bgColor = QColor(255, 255, 255);
chunkColor1 = QColor(100, 184, 255);
chunkColor2 = QColor(24, 189, 155);
chunkColor3 = QColor(255, 107, 107);
textColor1 = QColor(10, 10, 10);
textColor2 = QColor(255, 255, 255);
textColor3 = QColor(255, 255, 255);
process = new QProcess(this);
connect(process, SIGNAL(readyRead()), this, SLOT(readData()));
this->clear();
//设置列数和列宽
this->setColumnCount(5);
this->setColumnWidth(0, 100);
this->setColumnWidth(1, 120);
this->setColumnWidth(2, 120);
this->setColumnWidth(3, 120);
this->setColumnWidth(4, 120);
this->setStyleSheet("QTableWidget::item{padding:0px;}");
QStringList headText;
headText << "盘符" << "已用空间" << "可用空间" << "总大小" << "已用百分比" ;
this->setHorizontalHeaderLabels(headText);
this->setSelectionBehavior(QAbstractItemView::SelectRows);
this->setEditTriggers(QAbstractItemView::NoEditTriggers);
this->setSelectionMode(QAbstractItemView::SingleSelection);
this->verticalHeader()->setVisible(true);
this->horizontalHeader()->setStretchLastSection(true);
QMetaObject::invokeMethod(this, "load", Qt::QueuedConnection);
//QTimer::singleShot(10, this, SLOT(load()));
}
QColor DeviceSizeTable::getBgColor() const
{
return this->bgColor;
}
QColor DeviceSizeTable::getChunkColor1() const
{
return this->chunkColor1;
}
QColor DeviceSizeTable::getChunkColor2() const
{
return this->chunkColor2;
}
QColor DeviceSizeTable::getChunkColor3() const
{
return this->chunkColor3;
}
QColor DeviceSizeTable::getTextColor1() const
{
return this->textColor1;
}
QColor DeviceSizeTable::getTextColor2() const
{
return this->textColor2;
}
QColor DeviceSizeTable::getTextColor3() const
{
return this->textColor3;
}
void DeviceSizeTable::load()
{
//清空原有数据
int row = this->rowCount();
for (int i = 0; i < row; i++) {
this->removeRow(0);
}
#ifdef Q_OS_WIN
QFileInfoList list = QDir::drives();
foreach (QFileInfo dir, list) {
QString dirName = dir.absolutePath();
LPCWSTR lpcwstrDriver = (LPCWSTR)dirName.utf16();
ULARGE_INTEGER liFreeBytesAvailable, liTotalBytes, liTotalFreeBytes;
if (GetDiskFreeSpaceEx(lpcwstrDriver, &liFreeBytesAvailable, &liTotalBytes, &liTotalFreeBytes)) {
QString use = QString::number((double)(liTotalBytes.QuadPart - liTotalFreeBytes.QuadPart) / GB, 'f', 1);
use += "G";
QString free = QString::number((double) liTotalFreeBytes.QuadPart / GB, 'f', 1);
free += "G";
QString all = QString::number((double) liTotalBytes.QuadPart / GB, 'f', 1);
all += "G";
int percent = 100 - ((double)liTotalFreeBytes.QuadPart / liTotalBytes.QuadPart) * 100;
insertSize(dirName, use, free, all, percent);
}
}
#else
process->start("df -h");
#endif
}
void DeviceSizeTable::setBgColor(const QColor &bgColor)
{
if (this->bgColor != bgColor) {
this->bgColor = bgColor;
this->load();
}
}
void DeviceSizeTable::setChunkColor1(const QColor &chunkColor1)
{
if (this->chunkColor1 != chunkColor1) {
this->chunkColor1 = chunkColor1;
this->load();
}
}
void DeviceSizeTable::setChunkColor2(const QColor &chunkColor2)
{
if (this->chunkColor2 != chunkColor2) {
this->chunkColor2 = chunkColor2;
this->load();
}
}
void DeviceSizeTable::setChunkColor3(const QColor &chunkColor3)
{
if (this->chunkColor3 != chunkColor3) {
this->chunkColor3 = chunkColor3;
this->load();
}
}
void DeviceSizeTable::setTextColor1(const QColor &textColor1)
{
if (this->textColor1 != textColor1) {
this->textColor1 = textColor1;
this->load();
}
}
void DeviceSizeTable::setTextColor2(const QColor &textColor2)
{
if (this->textColor2 != textColor2) {
this->textColor2 = textColor2;
this->load();
}
}
void DeviceSizeTable::setTextColor3(const QColor &textColor3)
{
if (this->textColor3 != textColor3) {
this->textColor3 = textColor3;
this->load();
}
}
void DeviceSizeTable::readData()
{
while (!process->atEnd()) {
QString result = QLatin1String(process->readLine());
#ifdef __arm__
if (result.startsWith("/dev/root")) {
checkSize(result, "本地存储");
} else if (result.startsWith("/dev/mmcblk")) {
checkSize(result, "本地存储");
} else if (result.startsWith("/dev/mmcblk1p")) {
checkSize(result, "SD卡");
QStringList list = result.split(" ");
emit sdcardReceive(list.at(0));
} else if (result.startsWith("/dev/sd")) {
checkSize(result, "U盘");
QStringList list = result.split(" ");
emit udiskReceive(list.at(0));
}
#else
if (result.startsWith("/dev/sd")) {
checkSize(result, "");
QStringList list = result.split(" ");
emit udiskReceive(list.at(0));
}
#endif
}
}
void DeviceSizeTable::checkSize(const QString &result, const QString &name)
{
QString dev, use, free, all;
int percent = 0;
QStringList list = result.split(" ");
int index = 0;
for (int i = 0; i < list.count(); i++) {
QString s = list.at(i).trimmed();
if (s == "") {
continue;
}
index++;
if (index == 1) {
dev = s;
} else if (index == 2) {
all = s;
} else if (index == 3) {
use = s;
} else if (index == 4) {
free = s;
} else if (index == 5) {
percent = s.left(s.length() - 1).toInt();
break;
}
}
if (name.length() > 0) {
dev = name;
}
insertSize(dev, use, free, all, percent);
}
void DeviceSizeTable::insertSize(const QString &name, const QString &use, const QString &free, const QString &all, int percent)
{
int row = this->rowCount();
this->insertRow(row);
QTableWidgetItem *itemname = new QTableWidgetItem(name);
QTableWidgetItem *itemuse = new QTableWidgetItem(use);
itemuse->setTextAlignment(Qt::AlignCenter);
QTableWidgetItem *itemfree = new QTableWidgetItem(free);
itemfree->setTextAlignment(Qt::AlignCenter);
QTableWidgetItem *itemall = new QTableWidgetItem(all);
itemall->setTextAlignment(Qt::AlignCenter);
this->setItem(row, 0, itemname);
this->setItem(row, 1, itemuse);
this->setItem(row, 2, itemfree);
this->setItem(row, 3, itemall);
QProgressBar *bar = new QProgressBar;
bar->setRange(0, 100);
bar->setValue(percent);
QString qss = QString("QProgressBar{background:%1;border-width:0px;border-radius:0px;text-align:center;}"
"QProgressBar::chunk{border-radius:0px;}").arg(bgColor.name());
if (percent < 50) {
qss += QString("QProgressBar{color:%1;}QProgressBar::chunk{background:%2;}").arg(textColor1.name()).arg(chunkColor1.name());
} else if (percent < 90) {
qss += QString("QProgressBar{color:%1;}QProgressBar::chunk{background:%2;}").arg(textColor2.name()).arg(chunkColor2.name());
} else {
qss += QString("QProgressBar{color:%1;}QProgressBar::chunk{background:%2;}").arg(textColor3.name()).arg(chunkColor3.name());
}
bar->setStyleSheet(qss);
this->setCellWidget(row, 4, bar);
}
QSize DeviceSizeTable::sizeHint() const
{
return QSize(500, 300);
}
QSize DeviceSizeTable::minimumSizeHint() const
{
return QSize(200, 150);
}

View File

@@ -0,0 +1,86 @@
#ifndef DEVICESIZETABLE_H
#define DEVICESIZETABLE_H
/**
* 本地存储空间大小控件 作者:feiyangqingyun(QQ:517216493) 2016-11-30
* 1. 可自动加载本地存储设备的总容量/已用容量。
* 2. 进度条显示已用容量。
* 3. 支持所有操作系统。
* 4. 增加U盘或者SD卡到达信号。
*/
#include <QTableWidget>
class QProcess;
#ifdef quc
class Q_DECL_EXPORT DeviceSizeTable : public QTableWidget
#else
class DeviceSizeTable : public QTableWidget
#endif
{
Q_OBJECT
Q_PROPERTY(QColor bgColor READ getBgColor WRITE setBgColor)
Q_PROPERTY(QColor chunkColor1 READ getChunkColor1 WRITE setChunkColor1)
Q_PROPERTY(QColor chunkColor2 READ getChunkColor2 WRITE setChunkColor2)
Q_PROPERTY(QColor chunkColor3 READ getChunkColor3 WRITE setChunkColor3)
Q_PROPERTY(QColor textColor1 READ getTextColor1 WRITE setTextColor1)
Q_PROPERTY(QColor textColor2 READ getTextColor2 WRITE setTextColor2)
Q_PROPERTY(QColor textColor3 READ getTextColor3 WRITE setTextColor3)
public:
explicit DeviceSizeTable(QWidget *parent = 0);
private:
QProcess *process; //执行命令进程
QColor bgColor; //背景颜色
QColor chunkColor1; //进度颜色1
QColor chunkColor2; //进度颜色2
QColor chunkColor3; //进度颜色3
QColor textColor1; //文字颜色1
QColor textColor2; //文字颜色2
QColor textColor3; //文字颜色3
private slots:
void readData();
void checkSize(const QString &result, const QString &name);
void insertSize(const QString &name, const QString &use, const QString &free, const QString &all, int percent);
public:
QColor getBgColor() const;
QColor getChunkColor1() const;
QColor getChunkColor2() const;
QColor getChunkColor3() const;
QColor getTextColor1() const;
QColor getTextColor2() const;
QColor getTextColor3() const;
QSize sizeHint() const;
QSize minimumSizeHint() const;
public Q_SLOTS:
//载入容量
void load();
//设置背景颜色
void setBgColor(const QColor &bgColor);
//设置进度颜色
void setChunkColor1(const QColor &chunkColor1);
void setChunkColor2(const QColor &chunkColor2);
void setChunkColor3(const QColor &chunkColor3);
//设置文字颜色
void setTextColor1(const QColor &textColor1);
void setTextColor2(const QColor &textColor2);
void setTextColor3(const QColor &textColor3);
Q_SIGNALS:
void sdcardReceive(const QString &sdcardName);
void udiskReceive(const QString &udiskName);
};
#endif // DEVICESIZETABLE_H

View File

@@ -0,0 +1,17 @@
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
greaterThan(QT_MAJOR_VERSION, 5): QT += core5compat
TARGET = devicesizetable
TEMPLATE = app
DESTDIR = $$PWD/../bin
CONFIG += warn_off
SOURCES += main.cpp
SOURCES += frmdevicesizetable.cpp
SOURCES += devicesizetable.cpp
HEADERS += frmdevicesizetable.h
HEADERS += devicesizetable.h
FORMS += frmdevicesizetable.ui

View File

@@ -0,0 +1,15 @@
#pragma execution_character_set("utf-8")
#include "frmdevicesizetable.h"
#include "ui_frmdevicesizetable.h"
frmDeviceSizeTable::frmDeviceSizeTable(QWidget *parent) : QWidget(parent), ui(new Ui::frmDeviceSizeTable)
{
ui->setupUi(this);
ui->tableWidget->verticalHeader()->setDefaultSectionSize(25);
}
frmDeviceSizeTable::~frmDeviceSizeTable()
{
delete ui;
}

View File

@@ -0,0 +1,22 @@
#ifndef FRMDEVICESIZETABLE_H
#define FRMDEVICESIZETABLE_H
#include <QWidget>
namespace Ui {
class frmDeviceSizeTable;
}
class frmDeviceSizeTable : public QWidget
{
Q_OBJECT
public:
explicit frmDeviceSizeTable(QWidget *parent = 0);
~frmDeviceSizeTable();
private:
Ui::frmDeviceSizeTable *ui;
};
#endif // FRMDEVICESIZETABLE_H

View File

@@ -0,0 +1,122 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>frmDeviceSizeTable</class>
<widget class="QWidget" name="frmDeviceSizeTable">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="DeviceSizeTable" name="tableWidget">
<row/>
<row/>
<row/>
<row/>
<row/>
<column/>
<column/>
<column/>
<column/>
<column/>
<item row="0" column="0"/>
<item row="0" column="1">
<property name="textAlignment">
<set>AlignCenter</set>
</property>
</item>
<item row="0" column="2">
<property name="textAlignment">
<set>AlignCenter</set>
</property>
</item>
<item row="0" column="3">
<property name="textAlignment">
<set>AlignCenter</set>
</property>
</item>
<item row="1" column="0"/>
<item row="1" column="1">
<property name="textAlignment">
<set>AlignCenter</set>
</property>
</item>
<item row="1" column="2">
<property name="textAlignment">
<set>AlignCenter</set>
</property>
</item>
<item row="1" column="3">
<property name="textAlignment">
<set>AlignCenter</set>
</property>
</item>
<item row="2" column="0"/>
<item row="2" column="1">
<property name="textAlignment">
<set>AlignCenter</set>
</property>
</item>
<item row="2" column="2">
<property name="textAlignment">
<set>AlignCenter</set>
</property>
</item>
<item row="2" column="3">
<property name="textAlignment">
<set>AlignCenter</set>
</property>
</item>
<item row="3" column="0"/>
<item row="3" column="1">
<property name="textAlignment">
<set>AlignCenter</set>
</property>
</item>
<item row="3" column="2">
<property name="textAlignment">
<set>AlignCenter</set>
</property>
</item>
<item row="3" column="3">
<property name="textAlignment">
<set>AlignCenter</set>
</property>
</item>
<item row="4" column="0"/>
<item row="4" column="1">
<property name="textAlignment">
<set>AlignCenter</set>
</property>
</item>
<item row="4" column="2">
<property name="textAlignment">
<set>AlignCenter</set>
</property>
</item>
<item row="4" column="3">
<property name="textAlignment">
<set>AlignCenter</set>
</property>
</item>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>DeviceSizeTable</class>
<extends>QTableWidget</extends>
<header>devicesizetable.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

View File

@@ -0,0 +1,31 @@
#pragma execution_character_set("utf-8")
#include "frmdevicesizetable.h"
#include <QApplication>
#include <QTextCodec>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setFont(QFont("Microsoft Yahei", 9));
#if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
#if _MSC_VER
QTextCodec *codec = QTextCodec::codecForName("gbk");
#else
QTextCodec *codec = QTextCodec::codecForName("utf-8");
#endif
QTextCodec::setCodecForLocale(codec);
QTextCodec::setCodecForCStrings(codec);
QTextCodec::setCodecForTr(codec);
#else
QTextCodec *codec = QTextCodec::codecForName("utf-8");
QTextCodec::setCodecForLocale(codec);
#endif
frmDeviceSizeTable w;
w.setWindowTitle("磁盘容量");
w.show();
return a.exec();
}

View File

@@ -0,0 +1,28 @@
#pragma execution_character_set("utf-8")
#include "frmimageswitch.h"
#include "ui_frmimageswitch.h"
frmImageSwitch::frmImageSwitch(QWidget *parent) : QWidget(parent), ui(new Ui::frmImageSwitch)
{
ui->setupUi(this);
this->initForm();
}
frmImageSwitch::~frmImageSwitch()
{
delete ui;
}
void frmImageSwitch::initForm()
{
ui->imageSwitch1->setChecked(true);
ui->imageSwitch2->setChecked(true);
ui->imageSwitch3->setChecked(true);
ui->imageSwitch1->setFixedSize(87, 30);
ui->imageSwitch2->setFixedSize(87, 30);
ui->imageSwitch3->setFixedSize(87, 30);
ui->imageSwitch1->setButtonStyle(ImageSwitch::ButtonStyle_1);
ui->imageSwitch2->setButtonStyle(ImageSwitch::ButtonStyle_2);
ui->imageSwitch3->setButtonStyle(ImageSwitch::ButtonStyle_3);
}

View File

@@ -0,0 +1,25 @@
#ifndef FRMIMAGESWITCH_H
#define FRMIMAGESWITCH_H
#include <QWidget>
namespace Ui {
class frmImageSwitch;
}
class frmImageSwitch : public QWidget
{
Q_OBJECT
public:
explicit frmImageSwitch(QWidget *parent = 0);
~frmImageSwitch();
private:
Ui::frmImageSwitch *ui;
private slots:
void initForm();
};
#endif // FRMIMAGESWITCH_H

View File

@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>frmImageSwitch</class>
<widget class="QWidget" name="frmImageSwitch">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="2">
<widget class="ImageSwitch" name="imageSwitch3" native="true"/>
</item>
<item row="0" column="0">
<widget class="ImageSwitch" name="imageSwitch1" native="true"/>
</item>
<item row="0" column="1">
<widget class="ImageSwitch" name="imageSwitch2" native="true"/>
</item>
<item row="1" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="3">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>ImageSwitch</class>
<extends>QWidget</extends>
<header>imageswitch.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 638 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 696 B

View File

@@ -0,0 +1,91 @@
#pragma execution_character_set("utf-8")
#include "imageswitch.h"
#include "qpainter.h"
#include "qdebug.h"
ImageSwitch::ImageSwitch(QWidget *parent) : QWidget(parent)
{
isChecked = false;
buttonStyle = ButtonStyle_2;
imgOffFile = ":/image/imageswitch/btncheckoff2.png";
imgOnFile = ":/image/imageswitch/btncheckon2.png";
imgFile = imgOffFile;
}
void ImageSwitch::mousePressEvent(QMouseEvent *)
{
imgFile = isChecked ? imgOffFile : imgOnFile;
isChecked = !isChecked;
this->update();
}
void ImageSwitch::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHints(QPainter::SmoothPixmapTransform);
QImage img(imgFile);
img = img.scaled(this->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
//按照比例自动居中绘制
int pixX = rect().center().x() - img.width() / 2;
int pixY = rect().center().y() - img.height() / 2;
QPoint point(pixX, pixY);
painter.drawImage(point, img);
}
bool ImageSwitch::getChecked() const
{
return isChecked;
}
ImageSwitch::ButtonStyle ImageSwitch::getButtonStyle() const
{
return this->buttonStyle;
}
QSize ImageSwitch::sizeHint() const
{
return QSize(87, 28);
}
QSize ImageSwitch::minimumSizeHint() const
{
return QSize(87, 28);
}
void ImageSwitch::setChecked(bool isChecked)
{
if (this->isChecked != isChecked) {
this->isChecked = isChecked;
imgFile = isChecked ? imgOnFile : imgOffFile;
this->update();
}
}
void ImageSwitch::setButtonStyle(const ImageSwitch::ButtonStyle &buttonStyle)
{
if (this->buttonStyle != buttonStyle) {
this->buttonStyle = buttonStyle;
if (buttonStyle == ButtonStyle_1) {
imgOffFile = ":/image/imageswitch/btncheckoff1.png";
imgOnFile = ":/image/imageswitch/btncheckon1.png";
this->resize(87, 28);
} else if (buttonStyle == ButtonStyle_2) {
imgOffFile = ":/image/imageswitch/btncheckoff2.png";
imgOnFile = ":/image/imageswitch/btncheckon2.png";
this->resize(87, 28);
} else if (buttonStyle == ButtonStyle_3) {
imgOffFile = ":/image/imageswitch/btncheckoff3.png";
imgOnFile = ":/image/imageswitch/btncheckon3.png";
this->resize(96, 38);
}
imgFile = isChecked ? imgOnFile : imgOffFile;
setChecked(isChecked);
this->update();
updateGeometry();
}
}

View File

@@ -0,0 +1,59 @@
#ifndef IMAGESWITCH_H
#define IMAGESWITCH_H
/**
* 图片开关控件 作者:feiyangqingyun(QQ:517216493) 2016-11-25
* 1. 自带三种开关按钮样式。
* 2. 可自定义开关图片。
*/
#include <QWidget>
#ifdef quc
class Q_DECL_EXPORT ImageSwitch : public QWidget
#else
class ImageSwitch : public QWidget
#endif
{
Q_OBJECT
Q_ENUMS(ButtonStyle)
Q_PROPERTY(bool isChecked READ getChecked WRITE setChecked)
Q_PROPERTY(ButtonStyle buttonStyle READ getButtonStyle WRITE setButtonStyle)
public:
enum ButtonStyle {
ButtonStyle_1 = 0, //开关样式1
ButtonStyle_2 = 1, //开关样式2
ButtonStyle_3 = 2 //开关样式3
};
explicit ImageSwitch(QWidget *parent = 0);
protected:
void mousePressEvent(QMouseEvent *);
void paintEvent(QPaintEvent *event);
private:
bool isChecked;
ButtonStyle buttonStyle;
QString imgOffFile;
QString imgOnFile;
QString imgFile;
public:
bool getChecked() const;
ButtonStyle getButtonStyle() const;
QSize sizeHint() const;
QSize minimumSizeHint() const;
public Q_SLOTS:
//设置是否选中
void setChecked(bool isChecked);
//设置按钮样式
void setButtonStyle(const ImageSwitch::ButtonStyle &buttonStyle);
};
#endif // IMAGESWITCH_H

View File

@@ -0,0 +1,19 @@
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
greaterThan(QT_MAJOR_VERSION, 5): QT += core5compat
TARGET = imageswitch
TEMPLATE = app
DESTDIR = $$PWD/../bin
CONFIG += warn_off
SOURCES += main.cpp
SOURCES += frmimageswitch.cpp
SOURCES += imageswitch.cpp
HEADERS += frmimageswitch.h
HEADERS += imageswitch.h
FORMS += frmimageswitch.ui
RESOURCES += main.qrc

View File

@@ -0,0 +1,31 @@
#pragma execution_character_set("utf-8")
#include "frmimageswitch.h"
#include <QApplication>
#include <QTextCodec>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setFont(QFont("Microsoft Yahei", 9));
#if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
#if _MSC_VER
QTextCodec *codec = QTextCodec::codecForName("gbk");
#else
QTextCodec *codec = QTextCodec::codecForName("utf-8");
#endif
QTextCodec::setCodecForLocale(codec);
QTextCodec::setCodecForCStrings(codec);
QTextCodec::setCodecForTr(codec);
#else
QTextCodec *codec = QTextCodec::codecForName("utf-8");
QTextCodec::setCodecForLocale(codec);
#endif
frmImageSwitch w;
w.setWindowTitle("图片背景开关");
w.show();
return a.exec();
}

View File

@@ -0,0 +1,10 @@
<RCC>
<qresource prefix="/">
<file>image/imageswitch/btncheckoff1.png</file>
<file>image/imageswitch/btncheckoff2.png</file>
<file>image/imageswitch/btncheckoff3.png</file>
<file>image/imageswitch/btncheckon1.png</file>
<file>image/imageswitch/btncheckon2.png</file>
<file>image/imageswitch/btncheckon3.png</file>
</qresource>
</RCC>

View File

@@ -0,0 +1,31 @@
#pragma execution_character_set("utf-8")
#include "frmipaddress.h"
#include "ui_frmipaddress.h"
#include "qdebug.h"
frmIPAddress::frmIPAddress(QWidget *parent) : QWidget(parent), ui(new Ui::frmIPAddress)
{
ui->setupUi(this);
on_btnSetIP_clicked();
}
frmIPAddress::~frmIPAddress()
{
delete ui;
}
void frmIPAddress::on_btnSetIP_clicked()
{
ui->widgetIP->setIP("192.168.1.56");
}
void frmIPAddress::on_btnGetIP_clicked()
{
qDebug() << ui->widgetIP->getIP();
}
void frmIPAddress::on_btnClear_clicked()
{
ui->widgetIP->clear();
}

View File

@@ -0,0 +1,27 @@
#ifndef FRMADDRESS_H
#define FRMADDRESS_H
#include <QWidget>
namespace Ui {
class frmIPAddress;
}
class frmIPAddress : public QWidget
{
Q_OBJECT
public:
explicit frmIPAddress(QWidget *parent = 0);
~frmIPAddress();
private:
Ui::frmIPAddress *ui;
private slots:
void on_btnSetIP_clicked();
void on_btnGetIP_clicked();
void on_btnClear_clicked();
};
#endif // FRMADDRESS_H

View File

@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>frmIPAddress</class>
<widget class="QWidget" name="frmIPAddress">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<widget class="QWidget" name="layoutWidget">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>281</width>
<height>71</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0" colspan="3">
<widget class="IPAddress" name="widgetIP" native="true"/>
</item>
<item row="1" column="0">
<widget class="QPushButton" name="btnSetIP">
<property name="text">
<string>填入IP</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QPushButton" name="btnGetIP">
<property name="text">
<string>获取IP</string>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QPushButton" name="btnClear">
<property name="text">
<string>清空</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>
<customwidget>
<class>IPAddress</class>
<extends>QWidget</extends>
<header>ipaddress.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

View File

@@ -0,0 +1,210 @@
#pragma execution_character_set("utf-8")
#include "ipaddress.h"
#include "qlabel.h"
#include "qlineedit.h"
#include "qboxlayout.h"
#include "qregexp.h"
#include "qvalidator.h"
#include "qevent.h"
#include "qdebug.h"
IPAddress::IPAddress(QWidget *parent) : QWidget(parent)
{
bgColor = "#FFFFFF";
borderColor = "#A6B5B8";
borderRadius = 3;
//用于显示小圆点的标签,居中对齐
labDot1 = new QLabel;
labDot1->setAlignment(Qt::AlignCenter);
labDot1->setText(".");
labDot2 = new QLabel;
labDot2->setAlignment(Qt::AlignCenter);
labDot2->setText(".");
labDot3 = new QLabel;
labDot3->setAlignment(Qt::AlignCenter);
labDot3->setText(".");
//用于输入IP地址的文本框,居中对齐
txtIP1 = new QLineEdit;
txtIP1->setObjectName("txtIP1");
txtIP1->setAlignment(Qt::AlignCenter);
txtIP1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
connect(txtIP1, SIGNAL(textChanged(QString)), this, SLOT(textChanged(QString)));
txtIP2 = new QLineEdit;
txtIP2->setObjectName("txtIP2");
txtIP2->setAlignment(Qt::AlignCenter);
txtIP2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
connect(txtIP2, SIGNAL(textChanged(QString)), this, SLOT(textChanged(QString)));
txtIP3 = new QLineEdit;
txtIP3->setObjectName("txtIP3");
txtIP3->setAlignment(Qt::AlignCenter);
txtIP3->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
connect(txtIP3, SIGNAL(textChanged(QString)), this, SLOT(textChanged(QString)));
txtIP4 = new QLineEdit;
txtIP4->setObjectName("txtIP4");
txtIP4->setAlignment(Qt::AlignCenter);
txtIP4->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
connect(txtIP4, SIGNAL(textChanged(QString)), this, SLOT(textChanged(QString)));
//设置IP地址校验过滤
QString pattern = "(2[0-5]{2}|2[0-4][0-9]|1?[0-9]{1,2})";
//确切的说 QRegularExpression QRegularExpressionValidator 从5.0 5.1开始就有
#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))
QRegularExpression regExp(pattern);
QRegularExpressionValidator *validator = new QRegularExpressionValidator(regExp, this);
#else
QRegExp regExp(pattern);
QRegExpValidator *validator = new QRegExpValidator(regExp, this);
#endif
txtIP1->setValidator(validator);
txtIP2->setValidator(validator);
txtIP3->setValidator(validator);
txtIP4->setValidator(validator);
//绑定事件过滤器,识别键盘按下
txtIP1->installEventFilter(this);
txtIP2->installEventFilter(this);
txtIP3->installEventFilter(this);
txtIP4->installEventFilter(this);
QFrame *frame = new QFrame;
frame->setObjectName("frameIP");
QStringList qss;
qss.append(QString("QFrame#frameIP{border:1px solid %1;border-radius:%2px;}").arg(borderColor).arg(borderRadius));
qss.append(QString("QLabel{min-width:15px;background-color:%1;}").arg(bgColor));
qss.append(QString("QLineEdit{background-color:%1;border:none;}").arg(bgColor));
qss.append(QString("QLineEdit#txtIP1{border-top-left-radius:%1px;border-bottom-left-radius:%1px;}").arg(borderRadius));
qss.append(QString("QLineEdit#txtIP4{border-top-right-radius:%1px;border-bottom-right-radius:%1px;}").arg(borderRadius));
frame->setStyleSheet(qss.join(""));
QVBoxLayout *verticalLayout = new QVBoxLayout(this);
verticalLayout->setContentsMargins(0, 0, 0, 0);
verticalLayout->setSpacing(0);
verticalLayout->addWidget(frame);
//将控件按照横向布局排列
QHBoxLayout *layout = new QHBoxLayout(frame);
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(0);
layout->addWidget(txtIP1);
layout->addWidget(labDot1);
layout->addWidget(txtIP2);
layout->addWidget(labDot2);
layout->addWidget(txtIP3);
layout->addWidget(labDot3);
layout->addWidget(txtIP4);
}
bool IPAddress::eventFilter(QObject *watched, QEvent *event)
{
if (event->type() == QEvent::KeyPress) {
QLineEdit *txt = (QLineEdit *)watched;
if (txt == txtIP1 || txt == txtIP2 || txt == txtIP3 || txt == txtIP4) {
QKeyEvent *key = (QKeyEvent *)event;
//如果当前按下了小数点则移动焦点到下一个输入框
if (key->text() == ".") {
this->focusNextChild();
}
//如果按下了退格键并且当前文本框已经没有了内容则焦点往前移
if (key->key() == Qt::Key_Backspace) {
if (txt->text().length() <= 1) {
this->focusNextPrevChild(false);
}
}
}
}
return QWidget::eventFilter(watched, event);
}
void IPAddress::textChanged(const QString &text)
{
int len = text.length();
int value = text.toInt();
//判断当前是否输入完成一个网段,是的话则自动移动到下一个输入框
if (len == 3) {
if (value >= 100 && value <= 255) {
this->focusNextChild();
}
}
//拼接成完整IP地址
ip = QString("%1.%2.%3.%4").arg(txtIP1->text()).arg(txtIP2->text()).arg(txtIP3->text()).arg(txtIP4->text());
}
QString IPAddress::getIP() const
{
return this->ip;
}
QSize IPAddress::sizeHint() const
{
return QSize(250, 20);
}
QSize IPAddress::minimumSizeHint() const
{
return QSize(30, 10);
}
void IPAddress::setIP(const QString &ip)
{
//先检测IP地址是否合法
QRegExp regExp("((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)");
if (!regExp.exactMatch(ip)) {
return;
}
if (this->ip != ip) {
this->ip = ip;
//将IP地址填入各个网段
QStringList list = ip.split(".");
txtIP1->setText(list.at(0));
txtIP2->setText(list.at(1));
txtIP3->setText(list.at(2));
txtIP4->setText(list.at(3));
}
}
void IPAddress::clear()
{
txtIP1->clear();
txtIP2->clear();
txtIP3->clear();
txtIP4->clear();
txtIP1->setFocus();
}
void IPAddress::setBgColor(const QString &bgColor)
{
if (this->bgColor != bgColor) {
this->bgColor = bgColor;
}
}
void IPAddress::setBorderColor(const QString &borderColor)
{
if (this->borderColor != borderColor) {
this->borderColor = borderColor;
}
}
void IPAddress::setBorderRadius(int borderRadius)
{
if (this->borderRadius != borderRadius) {
this->borderRadius = borderRadius;
}
}

View File

@@ -0,0 +1,76 @@
#ifndef IPADDRESS_H
#define IPADDRESS_H
/**
* IP地址输入框控件 作者:feiyangqingyun(QQ:517216493) 2017-08-11
* 1. 可设置IP地址自动填入框。
* 2. 可清空IP地址。
* 3. 支持按下小圆点自动切换。
* 4. 支持退格键自动切换。
* 5. 支持IP地址过滤。
* 6. 可设置背景色、边框颜色、边框圆角角度。
*/
#include <QWidget>
class QLabel;
class QLineEdit;
#ifdef quc
class Q_DECL_EXPORT IPAddress : public QWidget
#else
class IPAddress : public QWidget
#endif
{
Q_OBJECT
Q_PROPERTY(QString ip READ getIP WRITE setIP)
public:
explicit IPAddress(QWidget *parent = 0);
protected:
bool eventFilter(QObject *watched, QEvent *event);
private:
QLabel *labDot1; //第一个小圆点
QLabel *labDot2; //第二个小圆点
QLabel *labDot3; //第三个小圆点
QLineEdit *txtIP1; //IP地址网段输入框1
QLineEdit *txtIP2; //IP地址网段输入框2
QLineEdit *txtIP3; //IP地址网段输入框3
QLineEdit *txtIP4; //IP地址网段输入框4
QString ip; //IP地址
QString bgColor; //背景颜色
QString borderColor;//边框颜色
int borderRadius; //边框圆角角度
private slots:
void textChanged(const QString &text);
public:
//获取IP地址
QString getIP() const;
QSize sizeHint() const;
QSize minimumSizeHint() const;
public Q_SLOTS:
//设置IP地址
void setIP(const QString &ip);
//清空
void clear();
//设置背景颜色
void setBgColor(const QString &bgColor);
//设置边框颜色
void setBorderColor(const QString &borderColor);
//设置边框圆角角度
void setBorderRadius(int borderRadius);
};
#endif // IPADDRESS_H

View File

@@ -0,0 +1,17 @@
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
greaterThan(QT_MAJOR_VERSION, 5): QT += core5compat
TARGET = ipaddress
TEMPLATE = app
DESTDIR = $$PWD/../bin
CONFIG += warn_off
SOURCES += main.cpp
SOURCES += frmipaddress.cpp
SOURCES += ipaddress.cpp
HEADERS += frmipaddress.h
HEADERS += ipaddress.h
FORMS += frmipaddress.ui

View File

@@ -0,0 +1,28 @@
#include "frmipaddress.h"
#include <QApplication>
#include <QTextCodec>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setFont(QFont("Microsoft Yahei", 9));
#if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
#if _MSC_VER
QTextCodec *codec = QTextCodec::codecForName("gbk");
#else
QTextCodec *codec = QTextCodec::codecForName("utf-8");
#endif
QTextCodec::setCodecForLocale(codec);
QTextCodec::setCodecForCStrings(codec);
QTextCodec::setCodecForTr(codec);
#else
QTextCodec *codec = QTextCodec::codecForName("utf-8");
QTextCodec::setCodecForLocale(codec);
#endif
frmIPAddress w;
w.show();
return a.exec();
}

View File

@@ -0,0 +1,56 @@
#pragma execution_character_set("utf-8")
#include "frmlightbutton.h"
#include "ui_frmlightbutton.h"
#include "qdatetime.h"
#include "qtimer.h"
frmLightButton::frmLightButton(QWidget *parent) : QWidget(parent), ui(new Ui::frmLightButton)
{
ui->setupUi(this);
this->initForm();
}
frmLightButton::~frmLightButton()
{
delete ui;
}
void frmLightButton::initForm()
{
ui->lightButton2->setBgColor(QColor(255, 107, 107));
ui->lightButton3->setBgColor(QColor(24, 189, 155));
type = 0;
QTimer *timer = new QTimer(this);
timer->setInterval(1000);
connect(timer, SIGNAL(timeout()), this, SLOT(updateValue()));
timer->start();
updateValue();
//以下方法启动报警
//ui->lightButton1->setAlarmColor(QColor(255, 0, 0));
//ui->lightButton1->setNormalColor(QColor(0, 0, 0));
//ui->lightButton1->startAlarm();
}
void frmLightButton::updateValue()
{
if (type == 0) {
ui->lightButton1->setLightGreen();
ui->lightButton2->setLightRed();
ui->lightButton3->setLightBlue();
type = 1;
} else if (type == 1) {
ui->lightButton1->setLightBlue();
ui->lightButton2->setLightGreen();
ui->lightButton3->setLightRed();
type = 2;
} else if (type == 2) {
ui->lightButton1->setLightRed();
ui->lightButton2->setLightBlue();
ui->lightButton3->setLightGreen();
type = 0;
}
}

View File

@@ -0,0 +1,27 @@
#ifndef FRMLIGHTBUTTON_H
#define FRMLIGHTBUTTON_H
#include <QWidget>
namespace Ui {
class frmLightButton;
}
class frmLightButton : public QWidget
{
Q_OBJECT
public:
explicit frmLightButton(QWidget *parent = 0);
~frmLightButton();
private:
Ui::frmLightButton *ui;
int type;
private slots:
void initForm();
void updateValue();
};
#endif // FRMLIGHTBUTTON_H

Some files were not shown because too many files have changed in this diff Show More