您好,登錄后才能下訂單哦!
本篇內容介紹了“Qt如何實現IP地址輸入控件”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
這個IP地址輸入框控件,估計寫爛了,網上隨便一搜索,保證一大堆,估計也是因為這個控件太容易了,非常適合新手練手,一般的思路都是用4個qlineedit控件拼起來,然后每個輸入框設置正則表達式過濾只能輸入3位數字,然后安裝事件過濾器識別回車自動跳到下一個輸入框。關于如何設置正則表達式過濾,這個可以搜索查到,本人也不大懂這個規則,貌似還有專門的書籍專門介紹正則表達式,可能這塊非常強大。
主要功能:
可設置IP地址,自動填入框
可清空IP地址
支持按下小圓點自動切換
支持退格鍵自動切換
支持IP地址過濾
可設置背景色/邊框顏色/邊框圓角角度
IPAddress::IPAddress(QWidget *parent) : QWidget(parent) { bgColor = "#FFFFFF"; borderColor = "#A6B5B8"; borderRadius = 3; //用于顯示小圓點的標簽,居中對齊 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("."); //用于輸入IP地址的文本框,居中對齊 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))); //設置IP地址校驗過濾 QRegExp regExp("(2[0-5]{2}|2[0-4][0-9]|1?[0-9]{1,2})"); QRegExpValidator *validator = new QRegExpValidator(regExp, this); txtIP1->setValidator(validator); txtIP2->setValidator(validator); txtIP3->setValidator(validator); txtIP4->setValidator(validator); //綁定事件過濾器,識別鍵盤按下 txtIP1->installEventFilter(this); txtIP2->installEventFilter(this); txtIP3->installEventFilter(this); txtIP4->installEventFilter(this); QFrame *frame = new QFrame; frame->setObjectName("frameIP"); QStringList qss; qss.append(QString("QFrame#frameIP{border:1px solid %1;border-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); //將控件按照橫向布局排列 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 *)event; //如果當前按下了小數點則移動焦點到下一個輸入框 if (key->text() == ".") { this->focusNextChild(); } //如果按下了退格鍵并且當前文本框已經沒有了內容則焦點往前移 if (key->key() == Qt::Key_Backspace) { if (txt->text().length() <= 1) { this->focusNextPrevChild(false); } } } } return QWidget::eventFilter(watched, event); }
“Qt如何實現IP地址輸入控件”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。