日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

QT中封装的IP地址的widget

發布時間:2025/4/5 c/c++ 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 QT中封装的IP地址的widget 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
QT沒有提供一個完整的IP地址控件,

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封裝了一個,用起來好多了,
下面是代碼:
// 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_;
};
// 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();
??????????????? }????
??????????? }
??????? }
??? }
}

// 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_;
};

// 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 <QApplication>
#include <QDialog>
#include "MyIpAddrEdit.h"

int main(int argc, char *argv[])
{
??? QApplication app(argc, argv);

??? MyIpAddrEdit *ipAddr = new MyIpAddrEdit();
??? ipAddr->settext("127.0.0.1");
??? ipAddr->show();

??? return app.exec();
}

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的QT中封装的IP地址的widget的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。