QT中封装的IP地址的widget
QT没有提供一个完整的IP地址控件,
1. 可以使用QLineEdit简单的实现
下面是代码:
下面是调用方法
1. 可以使用QLineEdit简单的实现
QRegExp regExp(
"
((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)
"
);
QRegExpValidator * pValidator = new QRegExpValidator(regExp, this );
QLineEdit * lineEdit = new QLineEdit( this );
lineEdit -> setValidator( new QRegExpValidator(pValidator, this ));
lineEdit -> setInputMask( " 000.000.000.000; " );
但是上面的输入时没有window的IP地址控件好用。所以自己就用4个QLineEdit封装了一个,用起来好多了,
QRegExpValidator * pValidator = new QRegExpValidator(regExp, this );
QLineEdit * lineEdit = new QLineEdit( this );
lineEdit -> setValidator( new QRegExpValidator(pValidator, this ));
lineEdit -> setInputMask( " 000.000.000.000; " );
下面是代码:
//
MyIpPartLineEdit.h
#pragma once
#include < QLineEdit >
class QWidget;
class QFocusEvent;
class QKeyEvent;
class MyIpPartLineEdit : public QLineEdit
{
Q_OBJECT
public :
MyIpPartLineEdit(QWidget * parent = 0 );
~ MyIpPartLineEdit( void );
void set_nexttab_edit(QLineEdit * nexttab) { next_tab_ = nexttab; }
protected :
virtual void focusInEvent(QFocusEvent * e);
virtual void keyPressEvent(QKeyEvent * event );
private slots:
void text_edited( const QString & text);
private :
QLineEdit * next_tab_;
};
#pragma once
#include < QLineEdit >
class QWidget;
class QFocusEvent;
class QKeyEvent;
class MyIpPartLineEdit : public QLineEdit
{
Q_OBJECT
public :
MyIpPartLineEdit(QWidget * parent = 0 );
~ MyIpPartLineEdit( void );
void set_nexttab_edit(QLineEdit * nexttab) { next_tab_ = nexttab; }
protected :
virtual void focusInEvent(QFocusEvent * e);
virtual void keyPressEvent(QKeyEvent * event );
private slots:
void text_edited( const QString & text);
private :
QLineEdit * next_tab_;
};
//
MyIpPartLineEdit.cpp
#include " MyIpPartLineEdit.h "
#include < QIntValidator >
#include < QKeyEvent >
MyIpPartLineEdit::MyIpPartLineEdit(QWidget * parent /* = 0 */ )
: QLineEdit(parent)
{
next_tab_ = 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(text_edited( const QString & )));
}
MyIpPartLineEdit:: ~ MyIpPartLineEdit( void )
{
}
void MyIpPartLineEdit::focusInEvent(QFocusEvent * e)
{
this -> selectAll();
QLineEdit::focusInEvent(e);
}
void MyIpPartLineEdit::keyPressEvent(QKeyEvent * event )
{
if ( event -> key() == Qt::Key_Period)
{
if (next_tab_)
{
next_tab_ -> setFocus();
next_tab_ -> selectAll();
}
}
QLineEdit::keyPressEvent( event );
}
void MyIpPartLineEdit::text_edited( const QString & text)
{
QIntValidator v( 0 , 255 , this );
QString ipaddr = text;
int pos = 0 ;
QValidator::State state = v.validate(ipaddr, pos);
if (state == QValidator::Acceptable)
{
if (ipaddr.size() > 1 )
{
if (ipaddr.size() == 2 )
{
int ipnum = ipaddr.toInt();
if (ipnum > 25 )
{
if (next_tab_)
{
next_tab_ -> setFocus();
next_tab_ -> selectAll();
}
}
}
else
{
if (next_tab_)
{
next_tab_ -> setFocus();
next_tab_ -> selectAll();
}
}
}
}
}
#include " MyIpPartLineEdit.h "
#include < QIntValidator >
#include < QKeyEvent >
MyIpPartLineEdit::MyIpPartLineEdit(QWidget * parent /* = 0 */ )
: QLineEdit(parent)
{
next_tab_ = 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(text_edited( const QString & )));
}
MyIpPartLineEdit:: ~ MyIpPartLineEdit( void )
{
}
void MyIpPartLineEdit::focusInEvent(QFocusEvent * e)
{
this -> selectAll();
QLineEdit::focusInEvent(e);
}
void MyIpPartLineEdit::keyPressEvent(QKeyEvent * event )
{
if ( event -> key() == Qt::Key_Period)
{
if (next_tab_)
{
next_tab_ -> setFocus();
next_tab_ -> selectAll();
}
}
QLineEdit::keyPressEvent( event );
}
void MyIpPartLineEdit::text_edited( const QString & text)
{
QIntValidator v( 0 , 255 , this );
QString ipaddr = text;
int pos = 0 ;
QValidator::State state = v.validate(ipaddr, pos);
if (state == QValidator::Acceptable)
{
if (ipaddr.size() > 1 )
{
if (ipaddr.size() == 2 )
{
int ipnum = ipaddr.toInt();
if (ipnum > 25 )
{
if (next_tab_)
{
next_tab_ -> setFocus();
next_tab_ -> selectAll();
}
}
}
else
{
if (next_tab_)
{
next_tab_ -> setFocus();
next_tab_ -> selectAll();
}
}
}
}
}
//
MyIpAddrEdit.h
#pragma once
#include < QWidget >
class QLineEdit;
class QLabel;
class MyIpPartLineEdit;
class MyIpAddrEdit : public QWidget
{
Q_OBJECT
public :
MyIpAddrEdit(QWidget * pParent = 0 );
~ MyIpAddrEdit();
void settext( const QString & text);
QString text();
void setStyleSheet( const QString & styleSheet);
signals:
void textchanged( const QString & text);
void textedited( const QString & text);
private slots:
void textchangedslot( const QString & text);
void texteditedslot( const QString & text);
private :
MyIpPartLineEdit * ip_part1_;
MyIpPartLineEdit * ip_part2_;
MyIpPartLineEdit * ip_part3_;
QLineEdit * ip_part4_;
QLabel * labeldot1_;
QLabel * labeldot2_;
QLabel * labeldot3_;
};
#pragma once
#include < QWidget >
class QLineEdit;
class QLabel;
class MyIpPartLineEdit;
class MyIpAddrEdit : public QWidget
{
Q_OBJECT
public :
MyIpAddrEdit(QWidget * pParent = 0 );
~ MyIpAddrEdit();
void settext( const QString & text);
QString text();
void setStyleSheet( const QString & styleSheet);
signals:
void textchanged( const QString & text);
void textedited( const QString & text);
private slots:
void textchangedslot( const QString & text);
void texteditedslot( const QString & text);
private :
MyIpPartLineEdit * ip_part1_;
MyIpPartLineEdit * ip_part2_;
MyIpPartLineEdit * ip_part3_;
QLineEdit * ip_part4_;
QLabel * labeldot1_;
QLabel * labeldot2_;
QLabel * labeldot3_;
};
//
MyIpAddrEdit.cpp
#include " MyIpAddrEdit.h "
#include < QRegExpValidator >
#include < QLabel >
#include " MyIpPartLineEdit.h "
MyIpAddrEdit::MyIpAddrEdit(QWidget * pParent /* = 0 */ )
: QWidget(pParent)
{
ip_part1_ = new MyIpPartLineEdit( this );
ip_part2_ = new MyIpPartLineEdit( this );
ip_part3_ = new MyIpPartLineEdit( this );
ip_part4_ = new MyIpPartLineEdit( this );
labeldot1_ = new QLabel( this );
labeldot2_ = new QLabel( this );
labeldot3_ = new QLabel( this );
ip_part1_ -> setGeometry(QRect( 0 , 0 , 30 , 20 ));
ip_part2_ -> setGeometry(QRect( 30 , 0 , 30 , 20 ));
ip_part3_ -> setGeometry(QRect( 60 , 0 , 30 , 20 ));
ip_part4_ -> setGeometry(QRect( 90 , 0 , 30 , 20 ));
labeldot1_ -> setText( " . " );
labeldot1_ -> setGeometry(QRect( 27 , 1 , 6 , 18 ));
labeldot1_ -> setAlignment(Qt::AlignCenter);
labeldot2_ -> setText( " . " );
labeldot2_ -> setGeometry(QRect( 57 , 1 , 6 , 18 ));
labeldot2_ -> setAlignment(Qt::AlignCenter);
labeldot3_ -> setText( " . " );
labeldot3_ -> setGeometry(QRect( 87 , 1 , 6 , 18 ));
labeldot3_ -> setAlignment(Qt::AlignCenter);
QWidget::setTabOrder(ip_part1_, ip_part2_);
QWidget::setTabOrder(ip_part2_, ip_part3_);
QWidget::setTabOrder(ip_part3_, ip_part4_);
ip_part1_ -> set_nexttab_edit(ip_part2_);
ip_part2_ -> set_nexttab_edit(ip_part3_);
ip_part3_ -> set_nexttab_edit(ip_part4_);
connect(ip_part1_, SIGNAL(textChanged( const QString & )), this , SLOT(textchangedslot( const QString & )));
connect(ip_part2_, SIGNAL(textChanged( const QString & )), this , SLOT(textchangedslot( const QString & )));
connect(ip_part3_, SIGNAL(textChanged( const QString & )), this , SLOT(textchangedslot( const QString & )));
connect(ip_part4_, SIGNAL(textChanged( const QString & )), this , SLOT(textchangedslot( const QString & )));
connect(ip_part1_, SIGNAL(textEdited ( const QString & )), this , SLOT(texteditedslot( const QString & )));
connect(ip_part2_, SIGNAL(textEdited ( const QString & )), this , SLOT(texteditedslot( const QString & )));
connect(ip_part3_, SIGNAL(textEdited ( const QString & )), this , SLOT(texteditedslot( const QString & )));
connect(ip_part4_, SIGNAL(textEdited ( const QString & )), this , SLOT(texteditedslot( const QString & )));
}
MyIpAddrEdit:: ~ MyIpAddrEdit()
{
}
void MyIpAddrEdit::textchangedslot( const QString & /* text */ )
{
QString ippart1, ippart2, ippart3, ippart4;
ippart1 = ip_part1_ -> text();
ippart2 = ip_part2_ -> text();
ippart3 = ip_part3_ -> text();
ippart4 = ip_part4_ -> text();
QString ipaddr = QString( " %1.%2.%3.%4 " )
.arg(ippart1)
.arg(ippart2)
.arg(ippart3)
.arg(ippart4);
emit textchanged(ipaddr);
}
void MyIpAddrEdit::texteditedslot( const QString & /* text */ )
{
QString ippart1, ippart2, ippart3, ippart4;
ippart1 = ip_part1_ -> text();
ippart2 = ip_part2_ -> text();
ippart3 = ip_part3_ -> text();
ippart4 = ip_part4_ -> text();
QString ipaddr = QString( " %1.%2.%3.%4 " )
.arg(ippart1)
.arg(ippart2)
.arg(ippart3)
.arg(ippart4);
emit textedited(ipaddr);
}
void MyIpAddrEdit::settext( const QString & text)
{
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)
{
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);
}
}
ip_part1_ -> setText(ippart1);
ip_part2_ -> setText(ippart2);
ip_part3_ -> setText(ippart3);
ip_part4_ -> setText(ippart4);
}
QString MyIpAddrEdit::text()
{
QString ippart1, ippart2, ippart3, ippart4;
ippart1 = ip_part1_ -> text();
ippart2 = ip_part2_ -> text();
ippart3 = ip_part3_ -> text();
ippart4 = ip_part4_ -> text();
return QString( " %1.%2.%3.%4 " )
.arg(ippart1)
.arg(ippart2)
.arg(ippart3)
.arg(ippart4);
}
void MyIpAddrEdit::setStyleSheet( const QString & styleSheet)
{
ip_part1_ -> setStyleSheet(styleSheet);
ip_part2_ -> setStyleSheet(styleSheet);
ip_part3_ -> setStyleSheet(styleSheet);
ip_part4_ -> setStyleSheet(styleSheet);
}
#include " MyIpAddrEdit.h "
#include < QRegExpValidator >
#include < QLabel >
#include " MyIpPartLineEdit.h "
MyIpAddrEdit::MyIpAddrEdit(QWidget * pParent /* = 0 */ )
: QWidget(pParent)
{
ip_part1_ = new MyIpPartLineEdit( this );
ip_part2_ = new MyIpPartLineEdit( this );
ip_part3_ = new MyIpPartLineEdit( this );
ip_part4_ = new MyIpPartLineEdit( this );
labeldot1_ = new QLabel( this );
labeldot2_ = new QLabel( this );
labeldot3_ = new QLabel( this );
ip_part1_ -> setGeometry(QRect( 0 , 0 , 30 , 20 ));
ip_part2_ -> setGeometry(QRect( 30 , 0 , 30 , 20 ));
ip_part3_ -> setGeometry(QRect( 60 , 0 , 30 , 20 ));
ip_part4_ -> setGeometry(QRect( 90 , 0 , 30 , 20 ));
labeldot1_ -> setText( " . " );
labeldot1_ -> setGeometry(QRect( 27 , 1 , 6 , 18 ));
labeldot1_ -> setAlignment(Qt::AlignCenter);
labeldot2_ -> setText( " . " );
labeldot2_ -> setGeometry(QRect( 57 , 1 , 6 , 18 ));
labeldot2_ -> setAlignment(Qt::AlignCenter);
labeldot3_ -> setText( " . " );
labeldot3_ -> setGeometry(QRect( 87 , 1 , 6 , 18 ));
labeldot3_ -> setAlignment(Qt::AlignCenter);
QWidget::setTabOrder(ip_part1_, ip_part2_);
QWidget::setTabOrder(ip_part2_, ip_part3_);
QWidget::setTabOrder(ip_part3_, ip_part4_);
ip_part1_ -> set_nexttab_edit(ip_part2_);
ip_part2_ -> set_nexttab_edit(ip_part3_);
ip_part3_ -> set_nexttab_edit(ip_part4_);
connect(ip_part1_, SIGNAL(textChanged( const QString & )), this , SLOT(textchangedslot( const QString & )));
connect(ip_part2_, SIGNAL(textChanged( const QString & )), this , SLOT(textchangedslot( const QString & )));
connect(ip_part3_, SIGNAL(textChanged( const QString & )), this , SLOT(textchangedslot( const QString & )));
connect(ip_part4_, SIGNAL(textChanged( const QString & )), this , SLOT(textchangedslot( const QString & )));
connect(ip_part1_, SIGNAL(textEdited ( const QString & )), this , SLOT(texteditedslot( const QString & )));
connect(ip_part2_, SIGNAL(textEdited ( const QString & )), this , SLOT(texteditedslot( const QString & )));
connect(ip_part3_, SIGNAL(textEdited ( const QString & )), this , SLOT(texteditedslot( const QString & )));
connect(ip_part4_, SIGNAL(textEdited ( const QString & )), this , SLOT(texteditedslot( const QString & )));
}
MyIpAddrEdit:: ~ MyIpAddrEdit()
{
}
void MyIpAddrEdit::textchangedslot( const QString & /* text */ )
{
QString ippart1, ippart2, ippart3, ippart4;
ippart1 = ip_part1_ -> text();
ippart2 = ip_part2_ -> text();
ippart3 = ip_part3_ -> text();
ippart4 = ip_part4_ -> text();
QString ipaddr = QString( " %1.%2.%3.%4 " )
.arg(ippart1)
.arg(ippart2)
.arg(ippart3)
.arg(ippart4);
emit textchanged(ipaddr);
}
void MyIpAddrEdit::texteditedslot( const QString & /* text */ )
{
QString ippart1, ippart2, ippart3, ippart4;
ippart1 = ip_part1_ -> text();
ippart2 = ip_part2_ -> text();
ippart3 = ip_part3_ -> text();
ippart4 = ip_part4_ -> text();
QString ipaddr = QString( " %1.%2.%3.%4 " )
.arg(ippart1)
.arg(ippart2)
.arg(ippart3)
.arg(ippart4);
emit textedited(ipaddr);
}
void MyIpAddrEdit::settext( const QString & text)
{
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)
{
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);
}
}
ip_part1_ -> setText(ippart1);
ip_part2_ -> setText(ippart2);
ip_part3_ -> setText(ippart3);
ip_part4_ -> setText(ippart4);
}
QString MyIpAddrEdit::text()
{
QString ippart1, ippart2, ippart3, ippart4;
ippart1 = ip_part1_ -> text();
ippart2 = ip_part2_ -> text();
ippart3 = ip_part3_ -> text();
ippart4 = ip_part4_ -> text();
return QString( " %1.%2.%3.%4 " )
.arg(ippart1)
.arg(ippart2)
.arg(ippart3)
.arg(ippart4);
}
void MyIpAddrEdit::setStyleSheet( const QString & styleSheet)
{
ip_part1_ -> setStyleSheet(styleSheet);
ip_part2_ -> setStyleSheet(styleSheet);
ip_part3_ -> setStyleSheet(styleSheet);
ip_part4_ -> setStyleSheet(styleSheet);
}
下面是调用方法
#include
<
QApplication
>
#include < QDialog >
#include " MyIpAddrEdit.h "
int main( int argc, char * argv[])
{
QApplication app(argc, argv);
QDialog * my_dialog = new QDialog;
MyIpAddrEdit * ipAddr = new MyIpAddrEdit(my_dialog);
ipAddr -> settext( " 127.0.0.1 " );
my_dialog -> show();
return app.exec();
}
#include < QDialog >
#include " MyIpAddrEdit.h "
int main( int argc, char * argv[])
{
QApplication app(argc, argv);
QDialog * my_dialog = new QDialog;
MyIpAddrEdit * ipAddr = new MyIpAddrEdit(my_dialog);
ipAddr -> settext( " 127.0.0.1 " );
my_dialog -> show();
return app.exec();
}