1.定制自己的Ip QLineEdit控件类
2.将定制的QLineEdit布局在QFrame上
最后效果是这样子的
#pragma once
#include
#include
#include
#include
class IpPartLineEdit : public QLineEdit
{
Q_OBJECT
public:
IpPartLineEdit(QWidget *parent = 0);
~IpPartLineEdit(void);
void setNextTabEdit(QLineEdit *nexttab) { m_pNextLineEdit = nexttab; }
void setLastEdit(QLineEdit *lastEdit) { m_pLastEdit = lastEdit; }
signals:
void invalidInput(int);
// 提示信息信号
void showMessage(const QString &text);
protected:
virtual void focusInEvent(QFocusEvent *e);
virtual void keyPressEvent(QKeyEvent *event);
private slots:
void textEdited(const QString& text);
void editingFinished();
private:
QLineEdit *m_pNextLineEdit;
QLineEdit *m_pLastEdit;
};
class IpAddrLineEdit : public QFrame
{
Q_OBJECT
public:
explicit IpAddrLineEdit(QWidget* pParent = 0);
~IpAddrLineEdit() {}
void setIpText(const QString &text, const bool checkIpValidity = true);
QString getIpText(const bool autoFill = true);
void setUnitStyleSheet(const QString &styleSheet);
void setIpEnabled(bool bEnable);
signals:
//文本(IP)变化
void sigTextChanged(const QString& text);
//文本(IP)编辑
void SigTextEdited(const QString &text);
// 提示信息信号
void showMessageBox(const QString &text);
protected:
void enterEvent(QEvent * event);
void leaveEvent(QEvent * event);
private:
void initUI();
private slots:
void slotTextChanged(const QString& text);
void slotTextEdited(const QString &text);
private:
//IP字段
IpPartLineEdit *m_pIpPart1;
IpPartLineEdit *m_pIpPart2;
IpPartLineEdit *m_pIpPart3;
IpPartLineEdit *m_pIpPart4;
//逗号分隔符
QLabel *m_pLabelDot1;
QLabel *m_pLabelDot2;
QLabel *m_pLabelDot3;
};
//cpp实现
#include "test.h"
#include "qvalidator.h"
#include "qevent.h"
#include "qboxlayout.h"
#include "QApplication"
//IP控件实现
IpPartLineEdit::IpPartLineEdit(QWidget *parent /*= 0*/) :
QLineEdit(parent),
m_pNextLineEdit(nullptr),
m_pLastEdit(nullptr)
{
this->setMaxLength(3);
this->setFrame(false);
this->setAlignment(Qt::AlignCenter);
QFont font;
font.setFamily("Arial");
font.setPixelSize(12);
this->setFont(font);
QValidator *validator = new QIntValidator(0, 2550, this); //QLineEdit输入校验合法性
this->setValidator(validator);
connect(this, SIGNAL(textEdited(const QString&)), this, SLOT(textEdited(const QString&)));
connect(this, SIGNAL(editingFinished()), this, SLOT(editingFinished()));
}
IpPartLineEdit::~IpPartLineEdit(void)
{
}
void IpPartLineEdit::focusInEvent(QFocusEvent *e)
{
this->selectAll();
QLineEdit::focusInEvent(e);
}
void IpPartLineEdit::keyPressEvent(QKeyEvent *event)
{
switch (event->key())
{
case Qt::Key_Left:
{
if (this->cursorPosition() == 0 && m_pLastEdit)
{
m_pLastEdit->setFocus();
m_pLastEdit->deselect();
}
}
break;
case Qt::Key_Right:
{
if (this->cursorPosition() == this->text().size() && m_pNextLineEdit)
{
m_pNextLineEdit->setFocus();
m_pNextLineEdit->deselect();
m_pNextLineEdit->setCursorPosition(0);
}
}
break;
case Qt::Key_Backspace:
{
if (this->cursorPosition() == 0 && m_pLastEdit)
{
m_pLastEdit->setFocus();
m_pLastEdit->deselect();
}
}
break;
default:
break;
}
QLineEdit::keyPressEvent(event);
}
void IpPartLineEdit::textEdited(const QString& text)
{
int ipBit = text.toInt();
if (ipBit > 255)
{
this->setText("255");
emit showMessage(QString("%1不是有效项,请指定一个介于0和255之间的值。").arg(ipBit));
this->selectAll();
this->setFocus();
}
else if (text.size() == 3)
{
if (m_pNextLineEdit)
{
m_pNextLineEdit->setFocus();
m_pNextLineEdit->selectAll();
}
}
}
//编辑结束时
void IpPartLineEdit::editingFinished()
{
if (!this->text().isEmpty())
{
if (this->text().toInt() == 0)
{
this->setText("0");
}
}
}
//自定义IP控件类实现
IpAddrLineEdit::IpAddrLineEdit(QWidget* pParent /*= 0*/)
:QFrame(pParent)
{
initUI();
}
//UI初始化
void IpAddrLineEdit::initUI()
{
m_pIpPart1 = new IpPartLineEdit(this);
m_pIpPart2 = new IpPartLineEdit(this);
m_pIpPart3 = new IpPartLineEdit(this);
m_pIpPart4 = new IpPartLineEdit(this);
m_pLabelDot1 = new QLabel(this);
m_pLabelDot2 = new QLabel(this);
m_pLabelDot3 = new QLabel(this);
m_pLabelDot1->setText(" .");
m_pLabelDot1->setFixedWidth(5);
m_pLabelDot1->setAlignment(Qt::AlignCenter);
m_pLabelDot2->setText(" .");
m_pLabelDot2->setFixedWidth(5);
m_pLabelDot2->setAlignment(Qt::AlignCenter);
m_pLabelDot3->setText(" .");
m_pLabelDot3->setFixedWidth(5);
m_pLabelDot3->setAlignment(Qt::AlignCenter);
QWidget::setTabOrder(m_pIpPart1, m_pIpPart2);
QWidget::setTabOrder(m_pIpPart2, m_pIpPart3);
QWidget::setTabOrder(m_pIpPart3, m_pIpPart4);
m_pIpPart1->setNextTabEdit(m_pIpPart2); //table键进入下一个ip段
m_pIpPart2->setNextTabEdit(m_pIpPart3);
m_pIpPart3->setNextTabEdit(m_pIpPart4);
m_pIpPart2->setLastEdit(m_pIpPart1);
m_pIpPart3->setLastEdit(m_pIpPart2);
m_pIpPart4->setLastEdit(m_pIpPart3);
QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->setContentsMargins(0, 0, 1, 0);
mainLayout->setSpacing(2);
mainLayout->addWidget(m_pIpPart1);
mainLayout->addWidget(m_pLabelDot1);
mainLayout->addWidget(m_pIpPart2);
mainLayout->addWidget(m_pLabelDot2);
mainLayout->addWidget(m_pIpPart3);
mainLayout->addWidget(m_pLabelDot3);
mainLayout->addWidget(m_pIpPart4);
this->setLayout(mainLayout);
this->setStyleSheet("border:1px solid #999999");
this->setUnitStyleSheet("border:0px solid #FFFFFF;color:#080808");
connect(m_pIpPart1, SIGNAL(sigTextChanged(const QString&)), this, SLOT(slotTextChanged(const QString&)));
connect(m_pIpPart2, SIGNAL(sigTextChanged(const QString&)), this, SLOT(slotTextChanged(const QString&)));
connect(m_pIpPart3, SIGNAL(sigTextChanged(const QString&)), this, SLOT(slotTextChanged(const QString&)));
connect(m_pIpPart4, SIGNAL(sigTextChanged(const QString&)), this, SLOT(slotTextChanged(const QString&)));
connect(m_pIpPart1, SIGNAL(SigTextEdited(const QString&)), this, SLOT(slotTextEdited(const QString&)));
connect(m_pIpPart2, SIGNAL(SigTextEdited(const QString&)), this, SLOT(slotTextEdited(const QString&)));
connect(m_pIpPart3, SIGNAL(SigTextEdited(const QString&)), this, SLOT(slotTextEdited(const QString&)));
connect(m_pIpPart4, SIGNAL(SigTextEdited(const QString&)), this, SLOT(slotTextEdited(const QString&)));
connect(m_pIpPart1, SIGNAL(showMessageBox(const QString&)), this, SIGNAL(showMessageBox(const QString&)));
connect(m_pIpPart2, SIGNAL(showMessageBox(const QString&)), this, SIGNAL(showMessageBox(const QString&)));
connect(m_pIpPart3, SIGNAL(showMessageBox(const QString&)), this, SIGNAL(showMessageBox(const QString&)));
connect(m_pIpPart4, SIGNAL(showMessageBox(const QString&)), this, SIGNAL(showMessageBox(const QString&)));
}
//设置IP显示值
void IpAddrLineEdit::setIpText(const QString &text, const bool checkIpValidity/* = true*/)
{
QString ippart1, ippart2, ippart3, ippart4;
QString qstring_validate = text;
// IP地址验证
QRegExp regexp("((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)");
QRegExpValidator regexp_validator(regexp, this);
int nPos = 0;
QValidator::State state = regexp_validator.validate(qstring_validate, nPos);
// IP合法
if ((state == QValidator::Acceptable) || !checkIpValidity)
{
QStringList ippartlist = text.split(".");
int strcount = ippartlist.size();
int index = 0;
if (index < strcount)
{
ippart1 = ippartlist.at(index);
}
if (++index < strcount)
{
ippart2 = ippartlist.at(index);
}
if (++index < strcount)
{
ippart3 = ippartlist.at(index);
}
if (++index < strcount)
{
ippart4 = ippartlist.at(index);
}
}
m_pIpPart1->setText(ippart1);
m_pIpPart2->setText(ippart2);
m_pIpPart3->setText(ippart3);
m_pIpPart4->setText(ippart4);
}
//获取IP显示值
QString IpAddrLineEdit::getIpText(const bool autoFill/* = true*/)
{
QString ippart1, ippart2, ippart3, ippart4;
ippart1 = m_pIpPart1->text();
ippart2 = m_pIpPart2->text();
ippart3 = m_pIpPart3->text();
ippart4 = m_pIpPart4->text();
if (ippart1.isEmpty() && autoFill)
{
m_pIpPart1->setText("0");
}
if (ippart2.isEmpty() && autoFill)
{
m_pIpPart2->setText("0");
}
if (ippart3.isEmpty() && autoFill)
{
m_pIpPart3->setText("0");
}
if (ippart4.isEmpty() && autoFill)
{
m_pIpPart4->setText("0");
}
return QString("%1.%2.%3.%4")
.arg(ippart1)
.arg(ippart2)
.arg(ippart3)
.arg(ippart4);
}
//设置IP子控件的显示样式
void IpAddrLineEdit::setUnitStyleSheet(const QString &styleSheet)
{
m_pIpPart1->setStyleSheet(styleSheet);
m_pIpPart2->setStyleSheet(styleSheet);
m_pIpPart3->setStyleSheet(styleSheet);
m_pIpPart4->setStyleSheet(styleSheet);
m_pLabelDot1->setStyleSheet(styleSheet);
m_pLabelDot2->setStyleSheet(styleSheet);
m_pLabelDot3->setStyleSheet(styleSheet);
}
//IP改变时响应槽函数
void IpAddrLineEdit::slotTextChanged(const QString& text)
{
QString ippart1, ippart2, ippart3, ippart4;
ippart1 = m_pIpPart1->text();
ippart2 = m_pIpPart2->text();
ippart3 = m_pIpPart3->text();
ippart4 = m_pIpPart4->text();
QString ipaddr = QString("%1.%2.%3.%4")
.arg(ippart1)
.arg(ippart2)
.arg(ippart3)
.arg(ippart4);
emit SigTextEdited(ipaddr);
}
//IP编辑时响应槽函数
void IpAddrLineEdit::slotTextEdited(const QString &text)
{
QString ippart1, ippart2, ippart3, ippart4;
ippart1 = m_pIpPart1->text();
ippart2 = m_pIpPart2->text();
ippart3 = m_pIpPart3->text();
ippart4 = m_pIpPart4->text();
QString ipaddr = QString("%1.%2.%3.%4")
.arg(ippart1)
.arg(ippart2)
.arg(ippart3)
.arg(ippart4);
emit sigTextChanged(ipaddr);
}
//鼠标进入时设置一下边框的颜色
void IpAddrLineEdit::enterEvent(QEvent * event)
{
if (this->isEnabled())
{
this->setStyleSheet("border:1px solid FF0000");
}
QFrame::enterEvent(event);
}
//鼠标离开时又设置另一种颜色
void IpAddrLineEdit::leaveEvent(QEvent * event)
{
if (this->isEnabled())
{
this->setStyleSheet("border:1px solid #8B008B");
}
QFrame::leaveEvent(event);
}