SS動畫提示框,可以用在用戶輸入信息時候的提醒功能上
如下:
實現代碼
html:
css:
輯導語:如何才能解決原型設計中輸入框校驗的問題?本篇文章里,作者就對如何繪制可以校驗的輸入框的相應流程進行了梳理,一起來看一下吧,也許會對你有所幫助。
在原型設計中,輸入框校驗效果設計是一件令人頭疼的事情,但是可以通過使用bootstrap元件庫中的輸入框設置,可以輕松解決輸入框校驗的問題。
預覽地址:http://atomstudio.cn/demos/bootstrap_input
需要安裝XSTAR2022.1.22版(或更高版本)
1)打開軟件,選擇bootstrap元件庫,在元件列表中將輸入框拖拽到編輯區。
2)選中編輯區的輸入框組件,右側顯示出輸入框設置面板。
3)在輸入框設置面板中勾選驗證成功提示和驗證失敗提示。
4)編輯提示文本內容
5)設置前綴、后綴
6)設置提示樣式
7)設置驗證規則
可選規則包括:
8)根據需要設置完成后,進行預覽、導出或者分享。
本文由 @balabala 原創發布于人人都是產品經理,未經許可,禁止轉載。
題圖來自Unsplash,基于 CC0 協議。
這個IP地址輸入框控件,估計寫爛了,網上隨便一搜索,保證一大堆,估計也是因為這個控件太容易了,非常適合新手練手,一般的思路都是用4個qlineedit控件拼起來,然后每個輸入框設置正則表達式過濾只能輸入3位數字,然后安裝事件過濾器識別回車自動跳到下一個輸入框。關于如何設置正則表達式過濾,這個可以搜索查到,本人也不大懂這個規則,貌似還有專門的書籍專門介紹正則表達式,可能這塊非常強大。
開源地址:https://gitee.com/feiyangqingyun/QWidgetDemo https://github.com/feiyangqingyun/QWidgetDemo
#ifndef IPADDRESS_H
#define IPADDRESS_H
/**
* IP地址輸入框控件 作者:feiyangqingyun(QQ:517216493) 2017-8-11
* 1:可設置IP地址,自動填入框
* 2:可清空IP地址
* 3:支持按下小圓點自動切換
* 4:支持退格鍵自動切換
* 5:支持IP地址過濾
* 6:可設置背景色/邊框顏色/邊框圓角角度
*/
#include <QWidget>
class QLabel;
class QLineEdit;
#ifdef quc
#if (QT_VERSION < QT_VERSION_CHECK(5,7,0))
#include <QtDesigner/QDesignerExportWidget>
#else
#include <QtUiPlugin/QDesignerExportWidget>
#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
【領QT開發教程學習資料,點擊下方鏈接莬費領取↓↓,先碼住不迷路~】
點擊這里:Qt資料領取(視頻教程+文檔+代碼+項目實戰)
#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;
}
}
原文鏈接:https://www.cnblogs.com/feiyangqingyun/p/11665022.html
【領QT開發教程學習資料,點擊下方鏈接莬費領取↓↓,先碼住不迷路~】
點擊這里:「鏈接」
*請認真填寫需求信息,我們會在24小時內與您取得聯系。