Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement IP address input Control by Qt

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to implement IP address input control by Qt". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I. Preface

This IP address input box control is estimated to be rotten, a random search on the Internet, to ensure a lot, probably also because this control is too easy, very suitable for beginners, the general idea is to use 4 qlineedit controls to put together, and then each input box set regular expression filter can only enter 3 digits, and then install event filter identification enter automatically jump to the next input box. On how to set regular expression filtering, this can be found by search, I do not understand this rule, it seems that there are special books on regular expressions, this may be very powerful.

Main functions:

IP address can be set and automatically filled in the box

Clearable IP address

Support automatic switching by pressing small dots

Automatic switching of backspace key is supported

Support for IP address filtering

Background color / border color / border fillet angle can be set

2. Code idea IPAddress::IPAddress (QWidget * parent): QWidget (parent) {bgColor = "# FFFFFF"; borderColor = "# A6B5B8"; borderRadius = 3; / / the label used to display dots is centered on labDot1 = new QLabel; labDot1- > setAlignment (Qt::AlignCenter); labDot1- > setText ("."); labDot2 = new QLabel; labDot2- > setAlignment (Qt::AlignCenter); labDot2- > setText ("."); labDot3 = new QLabel LabDot3- > setAlignment (Qt::AlignCenter); labDot3- > setText ("."); / / text box for entering IP addresses, centered on txtIP1 = new QLineEdit; txtIP1- > setObjectName ("txtIP1"); txtIP1- > setAlignment (Qt::AlignCenter); txtIP1- > setSizePolicy (QSizePolicy::Expanding, QSizePolicy::Expanding); connect (txtIP1, SIGNAL (textChanged (QString), this, SLOT (textChanged (QString); txtIP2 = new QLineEdit TxtIP2- > setObjectName ("txtIP2"); txtIP2- > setAlignment (Qt::AlignCenter); txtIP2- > setSizePolicy (QSizePolicy::Expanding, QSizePolicy::Expanding); connect (txtIP2, SIGNAL (textChanged (QString)), this, SLOT (textChanged (QString)); txtIP3 = new QLineEdit; txtIP3- > setObjectName ("txtIP3"); txtIP3- > setAlignment (Qt::AlignCenter); txtIP3- > setSizePolicy (QSizePolicy::Expanding, QSizePolicy::Expanding) Connect (txtIP3, SIGNAL (textChanged (QString)), this, SLOT (textChanged (QString)); txtIP4 = new QLineEdit; txtIP4- > setObjectName ("txtIP4"); txtIP4- > setAlignment (Qt::AlignCenter); txtIP4- > setSizePolicy (QSizePolicy::Expanding, QSizePolicy::Expanding); connect (txtIP4, SIGNAL (textChanged (QString)), this, SLOT (textChanged (QString) / / set IP address verification filter QRegExp regExp ("(2 [0-5] {2} | 2 [0-4] [0-9] | 1? [0-9] {1pm 2})"); QRegExpValidator * validator = new QRegExpValidator (regExp, this); txtIP1- > setValidator (validator); txtIP2- > setValidator (validator); txtIP3- > setValidator (validator); txtIP4- > setValidator (validator); / / bind event filter, press txtIP1- > installEventFilter (this) TxtIP2- > installEventFilter (this); txtIP3- > installEventFilter (this); txtIP4- > installEventFilter (this); QFrame * frame = new QFrame; frame- > setObjectName ("frameIP"); QStringList qss; qss.append ("QFrame#frameIP {border:1px solid% 1 borderMuffle radius)% 2px;}") .arg (borderColor) .arg (borderRadius); qss.append (QString ("QLabel {min-width:15px;background-color:%1;}") .arg (bgColor) Qss.append (QString ("QLineEdit {background-color:%1;border:none;}") .arg (bgColor)); qss.append (QString ("QLineEdit#txtIP1 {border-top-left-radius:%1px;border-bottom-left-radius:%1px;}") .arg (borderRadius)); qss.append (QString ("QLineEdit#txtIP4 {border-top-right-radius:%1px;border-bottom-right-radius:%1px;}") .arg (borderRadius) Frame- > setStyleSheet (qss.join (")); QVBoxLayout * verticalLayout = new QVBoxLayout (this); verticalLayout- > setMargin (0); verticalLayout- > setSpacing (0); verticalLayout- > addWidget (frame); / / arrange controls according to horizontal layout QHBoxLayout * layout = new QHBoxLayout (frame); layout- > setMargin (0); layout- > setSpacing (0); layout- > addWidget (txtIP1); layout- > addWidget (labDot1); layout- > addWidget (txtIP2) Layout- > addWidget (labDot2); layout- > addWidget (txtIP3); layout- > addWidget (labDot3); layout- > addWidget (txtIP4);} bool IPAddress::eventFilter (QObject * watched, QEvent * event) {if (event- > type () = QEvent::KeyPress) {QLineEdit * txt = (QLineEdit *) watched; if (txt = = txtIP1 | | txt = txtIP2 | | txt = txtIP3 | txt = txtIP4) {QKeyEvent * key = (QKeyEvent *) / / if the decimal point is currently pressed, move the focus to the next input box if (key- > text () = = ".") {this- > focusNextChild () } / / if the backspace key is pressed and the current text box has no content, the focus moves forward if (key- > key () = = Qt::Key_Backspace) {if (txt- > text (). Length () focusNextPrevChild (false);}} return QWidget::eventFilter (watched, event) Third, the effect picture

This is the end of the introduction of "how Qt implements the IP address input control". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report