[Qt] IP 주소 입력란 구현
15685 단어 qt
//ipaddredit.h
#ifndef IPADDREDIT_H
#define IPADDREDIT_H
#include
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_H
//ipaddredit.cpp
#include
#include
#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 &/*text*/)
{
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 &/*text*/)
{
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(".");
//int iCount = ipAddrList.size();
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
#ifndef IPPARTLINEEDIT_H
#define IPPARTLINEEDIT_H
#include
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_H
//ippartlineedit.cpp
#include
#include
#include "ippartlineedit.h"
CIpPartLineEdit::CIpPartLineEdit(QWidget *parent)
: QLineEdit(parent)
{
m_preEdit = NULL;
m_nextEdit = NULL;
this->setMaxLength(3);
//this->setFrame(false);
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();
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Qt How to use connect between incompatible signal and slotIn this I want to call a function, that function will receive a point . But this function should be invoked by a timer's...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.