说明
qt编写的ip地址输入控件,功能齐全,非常实用,使用提升法集成到自己项目的ui中即可。提供设置ip和获取ip两个接口。具体看代码,比较简单,注释应该比较详细了,下载地址放在文章尾部。
代码
ipinputcontrol.h
#ifndef IPINPUTCONTROL_H
#define IPINPUTCONTROL_H
#include <QWidget>
#include <QKeyEvent>
#include <QRegExpValidator>
QT_BEGIN_NAMESPACE
namespace Ui { class ipInputControl; }
QT_END_NAMESPACE
class ipInputControl : public QWidget
{
Q_OBJECT
public:
ipInputControl(QWidget *parent = nullptr);
~ipInputControl();
signals:
void warningIP(const QString&);
public slots:
QString getIP() const;
void setIP(const QString&);
private:
void keyPressEvent(QKeyEvent*);
bool eventFilter(QObject*, QEvent*);
void checkInput(const QString&);
private:
Ui::ipInputControl *ui;
};
#endif
ipinputcontrol.cpp
#include "ipinputcontrol.h"
#include "ui_ipinputcontrol.h"
#include <QDebug>
#define outlog qDebug()
ipInputControl::ipInputControl(QWidget *parent)
: QWidget(parent)
, ui(new Ui::ipInputControl)
{
ui->setupUi(this);
QRegExp regExp("(2[0-5]{2}|2[0-4][0-9]|1?[0-9]{1,2})");
QRegExpValidator *validator = new QRegExpValidator(regExp, this);
ui->lineEdit->setValidator(validator);
ui->lineEdit_2->setValidator(validator);
ui->lineEdit_3->setValidator(validator);
ui->lineEdit_4->setValidator(validator);
QWidget::setTabOrder(ui->lineEdit, ui->lineEdit_2);
QWidget::setTabOrder(ui->lineEdit_2, ui->lineEdit_3);
QWidget::setTabOrder(ui->lineEdit_3, ui->lineEdit_4);
QWidget::setTabOrder(ui->lineEdit_4, ui->lineEdit);
ui->lineEdit->installEventFilter(this);
ui->lineEdit_2->installEventFilter(this);
ui->lineEdit_3->installEventFilter(this);
ui->lineEdit_4->installEventFilter(this);
connect(ui->lineEdit, &QLineEdit::textChanged, this, &ipInputControl::checkInput);
connect(ui->lineEdit_2, &QLineEdit::textChanged, this, &ipInputControl::checkInput);
connect(ui->lineEdit_3, &QLineEdit::textChanged, this, &ipInputControl::checkInput);
connect(ui->lineEdit_4, &QLineEdit::textChanged, this, &ipInputControl::checkInput);
}
ipInputControl::~ipInputControl()
{
delete ui;
}
void ipInputControl::checkInput(const QString& text)
{
if(ui->lineEdit_4->hasFocus())
{
return ;
}
if(text.length() > 2)
{
QKeyEvent myEvent(QEvent::KeyPress, Qt::Key_Tab, Qt::NoModifier);
qApp->sendEvent(this, &myEvent);
}
else if(text.length() == 2)
{
int temp = text.toInt();
if(temp > 25)
{
QKeyEvent myEvent(QEvent::KeyPress, Qt::Key_Tab, Qt::NoModifier);
qApp->sendEvent(this, &myEvent);
}
}
}
void ipInputControl::keyPressEvent(QKeyEvent* e)
{
if(e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return)
{
QString ipSub1 = ui->lineEdit->text();
QString ipSub2 = ui->lineEdit_2->text();
QString ipSub3 = ui->lineEdit_3->text();
QString ipSub4 = ui->lineEdit_4->text();
if(ipSub1.isEmpty() || ipSub2.isEmpty() || ipSub3.isEmpty() || ipSub4.isEmpty())
{
emit warningIP("ip address input error.");
outlog<<"ip address input is error.";
}
else
{
QString ipString = ipSub1 + "." + ipSub2 + "." + ipSub3 + "." + ipSub4;
outlog << "ipString: " << ipString;
}
}
}
bool ipInputControl::eventFilter(QObject *watched, QEvent *event)
{
if (event->type() == QEvent::KeyPress)
{
QLineEdit *txt = (QLineEdit *)watched;
if (txt == ui->lineEdit || txt == ui->lineEdit_2 || txt == ui->lineEdit_3 || txt == ui->lineEdit_4)
{
QKeyEvent *key = (QKeyEvent *)event;
if (key->text() == ".")
{
if(ui->lineEdit->hasFocus())
ui->lineEdit_2->setFocus();
else if(ui->lineEdit_2->hasFocus())
ui->lineEdit_3->setFocus();
else if(ui->lineEdit_3->hasFocus())
ui->lineEdit_4->setFocus();
else if(ui->lineEdit_4->hasFocus())
ui->lineEdit->setFocus();
}
if (key->key() == Qt::Key_Backspace)
{
if (txt->text().length() <= 1)
{
if(ui->lineEdit_2->hasFocus())
ui->lineEdit->setFocus();
else if(ui->lineEdit_3->hasFocus())
ui->lineEdit_2->setFocus();
else if(ui->lineEdit_4->hasFocus())
ui->lineEdit_3->setFocus();
}
}
}
}
return QWidget::eventFilter(watched, event);
}
QString ipInputControl::getIP() const
{
QString ipSub1 = ui->lineEdit->text();
QString ipSub2 = ui->lineEdit_2->text();
QString ipSub3 = ui->lineEdit_3->text();
QString ipSub4 = ui->lineEdit_4->text();
if(ipSub1.isEmpty() || ipSub2.isEmpty() || ipSub3.isEmpty() || ipSub4.isEmpty())
{
return "";
}
return ipSub1 + "." + ipSub2 + "." + ipSub3 + "." + ipSub4;;
}
void ipInputControl::setIP(const QString& text)
{
QStringList ipList = text.split(".");
if(ipList.size() != 4)
{
emit warningIP("ip address format is error.");
outlog<<"ip address format is error.";
return;
}
ui->lineEdit->setText(ipList.at(0));
ui->lineEdit_2->setText(ipList.at(1));
ui->lineEdit_3->setText(ipList.at(2));
ui->lineEdit_4->setText(ipList.at(3));
}
|