0%

封装了一个ip地址的输入框。网络上下载了份代码,找不到哪里的了。经过修改之后,尽力让它的行为和windows的IP地址输入框的行为看起来像些。代码如下:

//ipaddredit.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#ifndef IPADDREDIT_H
#define IPADDREDIT_H

#include <QWidget>

class QLineEdit;
class QLabel;
class CIpPartLineEdit;

class CIpAddrEdit : public QWidget
{
Q_OBJECT

public:
CIpAddrEdit(QWidget *parent = 0);
~CIpAddrEdit();

void m_vSetText(const QString &text);
QString m_strGetText();
void m_vSetStyleSheet(const QString &styleSheet);

signals:
void sigTextChanged(const QString &text);
void sigTextEdited(const QString &text);

private slots:
void slot_text_Changed(const QString &text);
void slot_text_Edited(const QString &text);

private:
CIpPartLineEdit * m_pIpPart1;
CIpPartLineEdit * m_pIpPart2;
CIpPartLineEdit * m_pIpPart3;
CIpPartLineEdit * m_pIpPart4;

QLabel * m_pDot1;
QLabel * m_pDot2;
QLabel * m_pDot3;
};

#endif

//ipaddredit.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <QRegExpValidator>
#include <QLabel>

#include "ippartlineedit.h"
#include "ipaddredit.h"

CIpAddrEdit::CIpAddrEdit(QWidget *parent) :
QWidget(parent)
{
m_pIpPart1 = new CIpPartLineEdit(this);
m_pIpPart2 = new CIpPartLineEdit(this);
m_pIpPart3 = new CIpPartLineEdit(this);
m_pIpPart4 = new CIpPartLineEdit(this);

m_pDot1 = new QLabel(this);
m_pDot2 = new QLabel(this);
m_pDot3 = new QLabel(this);

m_pIpPart1->setGeometry(QRect(0, 0, 42, 27));
m_pIpPart2->setGeometry(QRect(70, 0, 42, 27));
m_pIpPart3->setGeometry(QRect(140, 0, 42, 27));
m_pIpPart4->setGeometry(QRect(210, 0, 42, 27));

m_pDot1->setText("-");
m_pDot1->setGeometry(QRect(48, 5, 16, 16));
m_pDot1->setAlignment(Qt::AlignCenter);

m_pDot2->setText("-");
m_pDot2->setGeometry(QRect(118, 5, 16, 16));
m_pDot2->setAlignment(Qt::AlignCenter);

m_pDot3->setText("-");
m_pDot3->setGeometry(QRect(188, 5, 16, 16));
m_pDot3->setAlignment(Qt::AlignCenter);

QWidget::setTabOrder(m_pIpPart1, m_pIpPart2);
QWidget::setTabOrder(m_pIpPart2, m_pIpPart3);
QWidget::setTabOrder(m_pIpPart3, m_pIpPart4);

m_pIpPart1->m_vSetNextEdit(m_pIpPart2);
m_pIpPart2->m_vSetNextEdit(m_pIpPart3);
m_pIpPart3->m_vSetNextEdit(m_pIpPart4);

m_pIpPart2->m_vSetPreEdit(m_pIpPart1);
m_pIpPart3->m_vSetPreEdit(m_pIpPart2);
m_pIpPart4->m_vSetPreEdit(m_pIpPart3);

connect(m_pIpPart1, SIGNAL(textChanged(const QString&)), this, SLOT(slot_text_Changed(const QString&)));
connect(m_pIpPart2, SIGNAL(textChanged(const QString&)), this, SLOT(slot_text_Changed(const QString&)));
connect(m_pIpPart3, SIGNAL(textChanged(const QString&)), this, SLOT(slot_text_Changed(const QString&)));
connect(m_pIpPart4, SIGNAL(textChanged(const QString&)), this, SLOT(slot_text_Changed(const QString&)));

connect(m_pIpPart1, SIGNAL(textEdited(const QString&)), this, SLOT(slot_text_Edited(const QString&)));
connect(m_pIpPart2, SIGNAL(textEdited(const QString&)), this, SLOT(slot_text_Edited(const QString&)));
connect(m_pIpPart3, SIGNAL(textEdited(const QString&)), this, SLOT(slot_text_Edited(const QString&)));
connect(m_pIpPart4, SIGNAL(textEdited(const QString&)), this, SLOT(slot_text_Edited(const QString&)));
}

CIpAddrEdit::~CIpAddrEdit()
{

}

void CIpAddrEdit::slot_text_Changed(const QString &)
{
QString strIpPart1 = m_pIpPart1->text();
QString strIpPart2 = m_pIpPart1->text();
QString strIpPart3 = m_pIpPart1->text();
QString strIpPart4 = m_pIpPart1->text();

QString strIpAddr = QString("%1.%2.%3.%4")
.arg(strIpPart1)
.arg(strIpPart2)
.arg(strIpPart3)
.arg(strIpPart4);

emit sigTextChanged(strIpAddr);
}

void CIpAddrEdit::slot_text_Edited(const QString &)
{
QString strIpPart1 = m_pIpPart1->text();
QString strIpPart2 = m_pIpPart1->text();
QString strIpPart3 = m_pIpPart1->text();
QString strIpPart4 = m_pIpPart1->text();

QString strIpAddr = QString("%1.%2.%3.%4")
.arg(strIpPart1)
.arg(strIpPart2)
.arg(strIpPart3)
.arg(strIpPart4);

emit sigTextEdited(strIpAddr);
}

void CIpAddrEdit::m_vSetText(const QString &text)
{
QString strIpAddr = text;

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 iPos = 0;

QValidator::State state = regexp_validator.validate(strIpAddr, iPos);
if(state != QValidator::Acceptable)
{
return ;
}

QStringList ipAddrList = text.split(".");


QString strIpPart1 = ipAddrList.at(0);
QString strIpPart2 = ipAddrList.at(1);
QString strIpPart3 = ipAddrList.at(2);
QString strIpPart4 = ipAddrList.at(3);

m_pIpPart1->setText(strIpPart1);
m_pIpPart2->setText(strIpPart2);
m_pIpPart3->setText(strIpPart3);
m_pIpPart4->setText(strIpPart4);
}

QString CIpAddrEdit::m_strGetText()
{
QString strIpPart1 = m_pIpPart1->text();
QString strIpPart2 = m_pIpPart2->text();
QString strIpPart3 = m_pIpPart3->text();
QString strIpPart4 = m_pIpPart4->text();

return QString("%1.%2.%3.%4")
.arg(strIpPart1)
.arg(strIpPart2)
.arg(strIpPart3)
.arg(strIpPart4);
}

void CIpAddrEdit::m_vSetStyleSheet(const QString &styleSheet)
{
m_pIpPart1->setStyleSheet(styleSheet);
m_pIpPart2->setStyleSheet(styleSheet);
m_pIpPart3->setStyleSheet(styleSheet);
m_pIpPart4->setStyleSheet(styleSheet);
}

//ippartlineedit.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#ifndef IPPARTLINEEDIT_H
#define IPPARTLINEEDIT_H

#include <QLineEdit>

class QWidget;
class QFocusEvent;
class QKeyEvent;

class CIpPartLineEdit : public QLineEdit
{
Q_OBJECT

public:
CIpPartLineEdit(QWidget *parent = 0);
~CIpPartLineEdit();

void m_vSetPreEdit(QLineEdit *preEdit);
void m_vSetNextEdit(QLineEdit *nextEdit);

protected:
void focusInEvent(QFocusEvent *event);
void keyPressEvent(QKeyEvent *event);

private slots:
void slot_text_Edited(const QString & text);

private:
QLineEdit *m_preEdit;
QLineEdit *m_nextEdit;
};

#endif

//ippartlineedit.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <QIntValidator>
#include <QKeyEvent>

#include "ippartlineedit.h"

CIpPartLineEdit::CIpPartLineEdit(QWidget *parent)
: QLineEdit(parent)
{
m_preEdit = NULL;
m_nextEdit = NULL;

this->setMaxLength(3);

this->setAlignment(Qt::AlignCenter);

QValidator *validator = new QIntValidator(0, 255, this);
this->setValidator(validator);

connect(this, SIGNAL(textEdited(const QString&)),
this, SLOT(slot_text_Edited(const QString&)));
}

CIpPartLineEdit::~CIpPartLineEdit()
{

}

void CIpPartLineEdit::m_vSetPreEdit(QLineEdit *preEdit)
{
m_preEdit = preEdit;
}

void CIpPartLineEdit::m_vSetNextEdit(QLineEdit *nextEdit)
{
m_nextEdit = nextEdit;
}

void CIpPartLineEdit::focusInEvent(QFocusEvent *event)
{
this->selectAll();
QLineEdit::focusInEvent(event);
}

void CIpPartLineEdit::keyPressEvent(QKeyEvent *event)
{
if(event->key() == Qt::Key_Period)
{
if(m_nextEdit &&
(this->text().size()!=0) &&
(!this->hasSelectedText()))
{
m_nextEdit->setFocus();
m_nextEdit->selectAll();
}
}
if(event->key() == Qt::Key_Backspace)
{
if(m_preEdit &&
(this->text().size() == 0))
{
m_preEdit->setFocus();
m_preEdit->setCursorPosition( m_preEdit->text().size() );
}
}
QLineEdit::keyPressEvent(event);
}

void CIpPartLineEdit::slot_text_Edited(const QString & text)
{
QIntValidator v(0, 255, this);
QString ipAddr = text;
int iPos = 0;

QValidator::State state = v.validate(ipAddr, iPos);
if(state != QValidator::Acceptable)
{
return ;
}
if(ipAddr.size() <= 1)
{
return ;
}

if(ipAddr.size() == 2)
{
int iIpNum = ipAddr.toInt();
if(iIpNum > 25)
{
if(m_nextEdit)
{
m_nextEdit->setFocus();
m_nextEdit->selectAll();
}
}
}
else
{
if(m_nextEdit)
{
m_nextEdit->setFocus();
m_nextEdit->selectAll();
}
}
}