From 5202754498c8514405b2ed8849ccbac2420cfd18 Mon Sep 17 00:00:00 2001 From: feiyangqingyun Date: Thu, 10 Oct 2019 13:42:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=8E=A7=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 32 ---- README.md | 2 + framelesswidget/framelesswidget.cpp | 191 +++++++++++++++++++++++ framelesswidget/framelesswidget.h | 66 ++++++++ framelesswidget/framelesswidget.pro | 23 +++ framelesswidget/frmframelesswidget.cpp | 38 +++++ framelesswidget/frmframelesswidget.h | 26 ++++ framelesswidget/frmframelesswidget.ui | 32 ++++ framelesswidget/main.cpp | 31 ++++ ipaddress/frmipaddress.cpp | 30 ++++ ipaddress/frmipaddress.h | 28 ++++ ipaddress/frmipaddress.ui | 64 ++++++++ ipaddress/ipaddress.cpp | 202 +++++++++++++++++++++++++ ipaddress/ipaddress.h | 81 ++++++++++ ipaddress/ipaddress.pro | 23 +++ ipaddress/main.cpp | 28 ++++ 16 files changed, 865 insertions(+), 32 deletions(-) delete mode 100644 .gitignore create mode 100644 framelesswidget/framelesswidget.cpp create mode 100644 framelesswidget/framelesswidget.h create mode 100644 framelesswidget/framelesswidget.pro create mode 100644 framelesswidget/frmframelesswidget.cpp create mode 100644 framelesswidget/frmframelesswidget.h create mode 100644 framelesswidget/frmframelesswidget.ui create mode 100644 framelesswidget/main.cpp create mode 100644 ipaddress/frmipaddress.cpp create mode 100644 ipaddress/frmipaddress.h create mode 100644 ipaddress/frmipaddress.ui create mode 100644 ipaddress/ipaddress.cpp create mode 100644 ipaddress/ipaddress.h create mode 100644 ipaddress/ipaddress.pro create mode 100644 ipaddress/main.cpp diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 259148f..0000000 --- a/.gitignore +++ /dev/null @@ -1,32 +0,0 @@ -# Prerequisites -*.d - -# Compiled Object files -*.slo -*.lo -*.o -*.obj - -# Precompiled Headers -*.gch -*.pch - -# Compiled Dynamic libraries -*.so -*.dylib -*.dll - -# Fortran module files -*.mod -*.smod - -# Compiled Static libraries -*.lai -*.la -*.a -*.lib - -# Executables -*.exe -*.out -*.app diff --git a/README.md b/README.md index 007d72e..103df38 100644 --- a/README.md +++ b/README.md @@ -15,3 +15,5 @@ Qt编写的一些开源的demo,包括串口调试助手、网络调试助手 | 9 | styledemo | 高仿PS黑色+扁平白色+淡蓝色风格主题 | | 10 | navbutton | 导航按钮控件 | | 11 | video_splite | 视频监控画面分割demo | +| 12 | framelesswidget | 通用无边框拖动拉伸类 | +| 13 | ipaddress | IP地址输入控件 | \ No newline at end of file diff --git a/framelesswidget/framelesswidget.cpp b/framelesswidget/framelesswidget.cpp new file mode 100644 index 0000000..f5b2d3d --- /dev/null +++ b/framelesswidget/framelesswidget.cpp @@ -0,0 +1,191 @@ +#include "framelesswidget.h" +#include "qevent.h" +#include "qdebug.h" + +FramelessWidget::FramelessWidget(QObject *parent) : QObject(parent) +{ + padding = 8; + widget = 0; + + pressed = false; + pressedLeft = false; + pressedRight = false; + pressedTop = false; + pressedBottom = false; + pressedLeftTop = false; + pressedRightTop = false; + pressedLeftBottom = false; + pressedRightBottom = false; + + //如果父类是窗体则直接设置 + if (parent->isWidgetType()) { + setWidget((QWidget *)parent); + } +} + +bool FramelessWidget::eventFilter(QObject *watched, QEvent *event) +{ + if (widget != 0 && watched == widget) { + if (event->type() == QEvent::Resize) { + //重新计算八个描点的区域,描点区域的作用还有就是计算鼠标坐标是否在某一个区域内 + int width = widget->width(); + int height = widget->height(); + + //左侧描点区域 + rectLeft = QRectF(0, padding, padding, height - padding * 2); + //上侧描点区域 + rectTop = QRectF(padding, 0, width - padding * 2, padding); + //右侧描点区域 + rectRight = QRectF(width - padding, padding, padding, height - padding * 2); + //下侧描点区域 + rectBottom = QRectF(padding, height - padding, width - padding * 2, padding); + + //左上角描点区域 + rectLeftTop = QRectF(0, 0, padding, padding); + //右上角描点区域 + rectRightTop = QRectF(width - padding, 0, padding, padding); + //左下角描点区域 + rectLeftBottom = QRectF(0, height - padding, padding, padding); + //右下角描点区域 + rectRightBottom = QRectF(width - padding, height - padding, padding, padding); + } else { + QMouseEvent *mouseEvent = (QMouseEvent *)event; + if (event->type() == QEvent::MouseButtonPress) { + //记住当前控件坐标和宽高以及鼠标按下的坐标 + rectX = widget->x(); + rectY = widget->y(); + rectW = widget->width(); + rectH = widget->height(); + lastPos = mouseEvent->pos(); + + //判断按下的手柄的区域位置 + if (rectLeft.contains(lastPos)) { + pressedLeft = true; + } else if (rectRight.contains(lastPos)) { + pressedRight = true; + } else if (rectTop.contains(lastPos)) { + pressedTop = true; + } else if (rectBottom.contains(lastPos)) { + pressedBottom = true; + } else if (rectLeftTop.contains(lastPos)) { + pressedLeftTop = true; + } else if (rectRightTop.contains(lastPos)) { + pressedRightTop = true; + } else if (rectLeftBottom.contains(lastPos)) { + pressedLeftBottom = true; + } else if (rectRightBottom.contains(lastPos)) { + pressedRightBottom = true; + } else { + pressed = true; + } + } else if (event->type() == QEvent::MouseMove) { + //设置对应鼠标形状 + QPoint point = mouseEvent->pos(); + if (rectLeft.contains(point)) { + widget->setCursor(Qt::SizeHorCursor); + } else if (rectRight.contains(point)) { + widget->setCursor(Qt::SizeHorCursor); + } else if (rectTop.contains(point)) { + widget->setCursor(Qt::SizeVerCursor); + } else if (rectBottom.contains(point)) { + widget->setCursor(Qt::SizeVerCursor); + } else if (rectLeftTop.contains(point)) { + widget->setCursor(Qt::SizeFDiagCursor); + } else if (rectRightTop.contains(point)) { + widget->setCursor(Qt::SizeBDiagCursor); + } else if (rectLeftBottom.contains(point)) { + widget->setCursor(Qt::SizeBDiagCursor); + } else if (rectRightBottom.contains(point)) { + widget->setCursor(Qt::SizeFDiagCursor); + } else { + widget->setCursor(Qt::ArrowCursor); + } + + //根据当前鼠标位置,计算XY轴移动了多少 + QPoint pos = mouseEvent->pos(); + int dx = pos.x() - lastPos.x(); + int dy = pos.y() - lastPos.y(); + + //根据按下处的位置判断是否是移动控件还是拉伸控件 + if (pressed) { + widget->move(widget->x() + dx, widget->y() + dy); + } else if (pressedLeft) { + int resizeW = widget->width() - dx; + if (widget->minimumWidth() <= resizeW) { + widget->setGeometry(widget->x() + dx, rectY, resizeW, rectH); + } + } else if (pressedRight) { + widget->setGeometry(rectX, rectY, rectW + dx, rectH); + } else if (pressedTop) { + int resizeH = widget->height() - dy; + if (widget->minimumHeight() <= resizeH) { + widget->setGeometry(rectX, widget->y() + dy, rectW, resizeH); + } + } else if (pressedBottom) { + widget->setGeometry(rectX, rectY, rectW, rectH + dy); + } else if (pressedLeftTop) { + int resizeW = widget->width() - dx; + int resizeH = widget->height() - dy; + if (widget->minimumWidth() <= resizeW) { + widget->setGeometry(widget->x() + dx, widget->y(), resizeW, resizeH); + } + if (widget->minimumHeight() <= resizeH) { + widget->setGeometry(widget->x(), widget->y() + dy, resizeW, resizeH); + } + } else if (pressedRightTop) { + int resizeW = rectW + dx; + int resizeH = widget->height() - dy; + if (widget->minimumHeight() <= resizeH) { + widget->setGeometry(widget->x(), widget->y() + dy, resizeW, resizeH); + } + } else if (pressedLeftBottom) { + int resizeW = widget->width() - dx; + int resizeH = rectH + dy; + if (widget->minimumWidth() <= resizeW) { + widget->setGeometry(widget->x() + dx, widget->y(), resizeW, resizeH); + } + if (widget->minimumHeight() <= resizeH) { + widget->setGeometry(widget->x(), widget->y(), resizeW, resizeH); + } + } else if (pressedRightBottom) { + int resizeW = rectW + dx; + int resizeH = rectH + dy; + widget->setGeometry(widget->x(), widget->y(), resizeW, resizeH); + } + } else if (event->type() == QEvent::MouseButtonRelease) { + //恢复所有 + pressed = false; + pressedLeft = false; + pressedRight = false; + pressedTop = false; + pressedBottom = false; + pressedLeftTop = false; + pressedRightTop = false; + pressedLeftBottom = false; + pressedRightBottom = false; + widget->setCursor(Qt::ArrowCursor); + } + } + } + + return QObject::eventFilter(watched, event); +} + +void FramelessWidget::setPadding(int padding) +{ + this->padding = padding; +} + +void FramelessWidget::setWidget(QWidget *widget) +{ + if (this->widget == 0) { + this->widget = widget; + //设置鼠标追踪为真 + this->widget->setMouseTracking(true); + //绑定事件过滤器 + this->widget->installEventFilter(this); + //设置无边框属性 + this->widget->setWindowFlags(Qt::FramelessWindowHint); + //this->widget->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint); + } +} diff --git a/framelesswidget/framelesswidget.h b/framelesswidget/framelesswidget.h new file mode 100644 index 0000000..f1ddfb6 --- /dev/null +++ b/framelesswidget/framelesswidget.h @@ -0,0 +1,66 @@ +#ifndef FRAMELESSWIDGET_H +#define FRAMELESSWIDGET_H + +/** + * 无边框窗体类 作者:feiyangqingyun(QQ:517216493) 2019-10-03 + * 1:可以指定需要无边框的widget + * 2:边框四周八个方位都可以自由拉伸 + * 3:可设置对应位置的边距,以便识别更大区域 + */ + +#include + +#ifdef quc +#if (QT_VERSION < QT_VERSION_CHECK(5,7,0)) +#include +#else +#include +#endif + +class QDESIGNER_WIDGET_EXPORT FramelessWidget : public QObject +#else +class FramelessWidget : public QObject +#endif + +{ + Q_OBJECT +public: + explicit FramelessWidget(QObject *parent = 0); + +protected: + bool eventFilter(QObject *watched, QEvent *event); + +private: + int padding; //边距 + QWidget *widget; //无边框窗体 + + bool pressed; //鼠标按下 + bool pressedLeft; //鼠标按下左侧 + bool pressedRight; //鼠标按下右侧 + bool pressedTop; //鼠标按下上侧 + bool pressedBottom; //鼠标按下下侧 + bool pressedLeftTop; //鼠标按下左上侧 + bool pressedRightTop; //鼠标按下右上侧 + bool pressedLeftBottom; //鼠标按下左下侧 + bool pressedRightBottom; //鼠标按下右下侧 + + int rectX, rectY, rectW, rectH; //窗体坐标+宽高 + QPoint lastPos; //鼠标按下处坐标 + + QRectF rectLeft; //左侧区域 + QRectF rectRight; //右侧区域 + QRectF rectTop; //上侧区域 + QRectF rectBottom; //下侧区域 + QRectF rectLeftTop; //左上侧区域 + QRectF rectRightTop; //右上侧区域 + QRectF rectLeftBottom; //左下侧区域 + QRectF rectRightBottom; //右下侧区域 + +public Q_SLOTS: + //设置边距 + void setPadding(int padding); + //设置要无边框的窗体 + void setWidget(QWidget *widget); +}; + +#endif // FRAMELESSWIDGET_H diff --git a/framelesswidget/framelesswidget.pro b/framelesswidget/framelesswidget.pro new file mode 100644 index 0000000..563427b --- /dev/null +++ b/framelesswidget/framelesswidget.pro @@ -0,0 +1,23 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2019-10-03T10:55:58 +# +#------------------------------------------------- + +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = framelesswidget +TEMPLATE = app +DESTDIR = $$PWD/../bin +CONFIG += warn_off + +SOURCES += main.cpp +SOURCES += frmframelesswidget.cpp +SOURCES += framelesswidget.cpp + +HEADERS += frmframelesswidget.h +HEADERS += framelesswidget.h + +FORMS += frmframelesswidget.ui diff --git a/framelesswidget/frmframelesswidget.cpp b/framelesswidget/frmframelesswidget.cpp new file mode 100644 index 0000000..67c8d58 --- /dev/null +++ b/framelesswidget/frmframelesswidget.cpp @@ -0,0 +1,38 @@ +#pragma execution_character_set("utf-8") + +#include "frmframelesswidget.h" +#include "ui_frmframelesswidget.h" +#include "qpushbutton.h" +#include "framelesswidget.h" + +frmFramelessWidget::frmFramelessWidget(QWidget *parent) : QWidget(parent), ui(new Ui::frmFramelessWidget) +{ + ui->setupUi(this); +} + +frmFramelessWidget::~frmFramelessWidget() +{ + delete ui; +} + +void frmFramelessWidget::on_btnOpen_clicked() +{ + QWidget *w = new QWidget; + w->setAttribute(Qt::WA_DeleteOnClose); + w->setWindowTitle("自由拉伸无边框窗体"); + w->resize(480, 320); + + //设置下背景颜色区别看 + QPalette palette = w->palette(); + palette.setBrush(QPalette::Background, QColor(162, 121, 197)); + w->setPalette(palette); + + QPushButton *btn = new QPushButton(w); + connect(btn, SIGNAL(clicked(bool)), w, SLOT(close())); + btn->setGeometry(10, 10, 100, 25); + btn->setText("关闭"); + + FramelessWidget *f = new FramelessWidget(w); + f->setWidget(w); + w->show(); +} diff --git a/framelesswidget/frmframelesswidget.h b/framelesswidget/frmframelesswidget.h new file mode 100644 index 0000000..c94665a --- /dev/null +++ b/framelesswidget/frmframelesswidget.h @@ -0,0 +1,26 @@ +#ifndef FRMFRAMELESSWIDGET_H +#define FRMFRAMELESSWIDGET_H + +#include + +namespace Ui +{ + class frmFramelessWidget; +} + +class frmFramelessWidget : public QWidget +{ + Q_OBJECT + +public: + explicit frmFramelessWidget(QWidget *parent = 0); + ~frmFramelessWidget(); + +private: + Ui::frmFramelessWidget *ui; + +private slots: + void on_btnOpen_clicked(); +}; + +#endif // FRMFRAMELESSWIDGET_H diff --git a/framelesswidget/frmframelesswidget.ui b/framelesswidget/frmframelesswidget.ui new file mode 100644 index 0000000..c1124da --- /dev/null +++ b/framelesswidget/frmframelesswidget.ui @@ -0,0 +1,32 @@ + + + frmFramelessWidget + + + + 0 + 0 + 500 + 300 + + + + Form + + + + + 10 + 10 + 181 + 23 + + + + 弹窗无边框窗体 + + + + + + diff --git a/framelesswidget/main.cpp b/framelesswidget/main.cpp new file mode 100644 index 0000000..39b7479 --- /dev/null +++ b/framelesswidget/main.cpp @@ -0,0 +1,31 @@ +#pragma execution_character_set("utf-8") + +#include "frmframelesswidget.h" +#include +#include + +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 + + frmFramelessWidget w; + w.setWindowTitle("无边框窗体类"); + w.show(); + + return a.exec(); +} diff --git a/ipaddress/frmipaddress.cpp b/ipaddress/frmipaddress.cpp new file mode 100644 index 0000000..dc72048 --- /dev/null +++ b/ipaddress/frmipaddress.cpp @@ -0,0 +1,30 @@ +#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); +} + +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(); +} diff --git a/ipaddress/frmipaddress.h b/ipaddress/frmipaddress.h new file mode 100644 index 0000000..dfda1a3 --- /dev/null +++ b/ipaddress/frmipaddress.h @@ -0,0 +1,28 @@ +#ifndef FRMADDRESS_H +#define FRMADDRESS_H + +#include + +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 diff --git a/ipaddress/frmipaddress.ui b/ipaddress/frmipaddress.ui new file mode 100644 index 0000000..1d32a4a --- /dev/null +++ b/ipaddress/frmipaddress.ui @@ -0,0 +1,64 @@ + + + frmIPAddress + + + + 0 + 0 + 500 + 300 + + + + Form + + + + + 9 + 9 + 481 + 71 + + + + + + + + + + 设置IP + + + + + + + 获取IP + + + + + + + 清空 + + + + + + + + + + IPAddress + QWidget +
ipaddress.h
+ 1 +
+
+ + +
diff --git a/ipaddress/ipaddress.cpp b/ipaddress/ipaddress.cpp new file mode 100644 index 0000000..0ed053e --- /dev/null +++ b/ipaddress/ipaddress.cpp @@ -0,0 +1,202 @@ +#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地址校验过滤 + QRegExp regExp("(2[0-5]{2}|2[0-4][0-9]|1?[0-9]{1,2})"); + QRegExpValidator *validator = new QRegExpValidator(regExp, this); + 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->setMargin(0); + verticalLayout->setSpacing(0); + verticalLayout->addWidget(frame); + + //将控件按照横向布局排列 + QHBoxLayout *layout = new QHBoxLayout(frame); + layout->setMargin(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; + } +} diff --git a/ipaddress/ipaddress.h b/ipaddress/ipaddress.h new file mode 100644 index 0000000..0a48b85 --- /dev/null +++ b/ipaddress/ipaddress.h @@ -0,0 +1,81 @@ +#ifndef IPADDRESS_H +#define IPADDRESS_H + +/** + * IP地址输入框控件 作者:feiyangqingyun(QQ:517216493) 2017-8-11 + * 1:可设置IP地址,自动填入框 + * 2:可清空IP地址 + * 3:支持按下小圆点自动切换 + * 4:支持退格键自动切换 + * 5:支持IP地址过滤 + * 6:可设置背景色/边框颜色/边框圆角角度 + */ + +#include + +class QLabel; +class QLineEdit; + +#ifdef quc +#if (QT_VERSION < QT_VERSION_CHECK(5,7,0)) +#include +#else +#include +#endif + +class QDESIGNER_WIDGET_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 diff --git a/ipaddress/ipaddress.pro b/ipaddress/ipaddress.pro new file mode 100644 index 0000000..4d28b9e --- /dev/null +++ b/ipaddress/ipaddress.pro @@ -0,0 +1,23 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2017-08-10T17:11:52 +# +#------------------------------------------------- + +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +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 diff --git a/ipaddress/main.cpp b/ipaddress/main.cpp new file mode 100644 index 0000000..2a49bb4 --- /dev/null +++ b/ipaddress/main.cpp @@ -0,0 +1,28 @@ +#include "frmipaddress.h" +#include +#include + +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(); +}