pinkyuanxue的个人主页

一块来学习!!!

http://www.qtcn.org/bbs/u/126669  [收藏] [复制]

pinkyuanxue

奋斗.....

  • 23

    关注

  • 26

    粉丝

  • 39

    访客

  • 等级:新手上路
  • 总积分:9
  • 女,1990-01-28

最后登录:2013-07-11

更多资料

日志

用QLineEdit实现IP地址输入框

2012-04-01 11:40
转载:
://thenand.blogbus.com/logs/176521855.html


以前用QLineEdit输IP地址全都是用 setValidator 加正则表达式来限制,只这样要自己输入点号。
后来发现可以用 setInputMask 来格式化输入的内容,这时点号不用写了,而且还可以加了 setValidator 来约束。
最后领导要求这个IP输入框要和windows一样的效果。这下头痛了。
开始研究源代码:
第一种想法:从 setInputMask 函数入手。看了一下 QLineEdit 类的源代码,发现他封装的好低层,想要重载这个函数来修改基本不可能。
第二种想法:自己对 QLineEdit 显示效果进行重绘,重载 paintEvent 函数,先均匀的绘出三个点出来,看了一下效果,很好,很满意,可是最后发现文本框中的光标没有了。然后看了一下 paintEvent 的源代码,它的光标绘画用了 QTextLayout 类,这个类在 QLineEdit 里被套了好几层,根本不对外开放,无折,火啊。
一点办法也没有,感觉脖子被折断了。
最后在网络中搜索,一直也没有结果,不过还是给我搜到了一些零碎,用 QLineEdit 套四个子 QLineEdit 来实现。


下面就是用这个思路来实现的代码,用四个子 QLineEdit 输入IP地址的四个段,在父 QLineEdit 中 draw 出三个点。过滤键盘事件,重载 焦点 与 paint 事件,重载了相应的接口函数,完成了 Windows 下IP地址输入框所有的功能,连续输入,删除,段之间的切换。
和 Windows 系统的IP地址输入框的区别是:
1.输入的值错误时,Windows下会弹出对话框,这儿不会,只是不让输入。
2.父 QLineEdit 还是能正常显示文本的,只是在它显示文本时,会擦除这三个点,并把四个子文本框隐藏。
上代码
.h/****************************************************************************Specialkeywords:zzw2011-4-212011**政务资源目录体系系统产品&工程建设领域项目**Copyright(C)2010by浙江建达科技股份有限公司**************************************************************************/#ifndefIPLINEEDIT_H#defineIPLINEEDIT_H#include<QtGui/QLineEdit>classIPLineEdit:publicQLineEdit{Q_OBJECTpublic:IPLineEdit(QWidget*parent=0);IPLineEdit(constQString&contents,QWidget*parent=0);~IPLineEdit();QStringtext()const;publicslots:voidclear();voidsetText(constQString&text);protected:booleventFilter(QObject*obj,QEvent*event);voidpaintEvent(QPaintEvent*ev);voidfocusInEvent(QFocusEvent*e);private:///屏蔽的函数voidsetAlignment(Qt::Alignmentflag);voidsetTextMargins(intleft,inttop,intright,intbottom);voidsetTextMargins(constQMargins&margins);private:voidbuildingSubLine();private:/*!四个输入地址的文本框*/QLineEdit*m_pLineEdit[4];};#endif

.cpp
/****************************************************************************Specialkeywords:zzw2011-4-212011**政务资源目录体系系统产品&工程建设领域项目**Copyright(C)2010by浙江建达科技股份有限公司**************************************************************************/#include<QPainter>#include<QStyleOptionFrameV2>#include<QKeyEvent>#include"iplineedit.h"IPLineEdit::IPLineEdit(QWidget*parent):QLineEdit(parent){buildingSubLine();}IPLineEdit::IPLineEdit(constQString&contents,QWidget*parent):QLineEdit(parent){buildingSubLine();setText(contents);}IPLineEdit::~IPLineEdit(){for(inti=0;i<4;i++)deletem_pLineEdit;}voidIPLineEdit::buildingSubLine(){//QRegExprx("((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-4]|[01]?\\d\\d?)");#if0///掩码的情况QRegExprx0("(2[0-1]\\d|22[0-3]|1\\d\\d|[1-9]\\d|[1-9])");///第一项目的范围为1-223QRegExpValidator*validator0=newQRegExpValidator(rx0,this);#endifQRegExprx("(2[0-4]\\d|25[0-5]|1\\d\\d|[1-9]?\\d)");///排除0\\d的情况QRegExpValidator*validator=newQRegExpValidator(rx,this);for(inti=0;i<4;i++){m_pLineEdit=newQLineEdit(this);m_pLineEdit->setFrame(false);m_pLineEdit->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);m_pLineEdit->setMaxLength(3);//m_pLineEdit->setMinimumSize(25,20);//#if0///掩码的情况//if(i==0)//m_pLineEdit->setValidator(validator0);//else//#endifm_pLineEdit->setValidator(validator);m_pLineEdit->show();m_pLineEdit->installEventFilter(this);}}boolIPLineEdit::eventFilter(QObject*obj,QEvent*event){if(children().contains(obj)&&event->type()==QEvent::KeyPress){QKeyEvent*keyEvent=static_cast<QKeyEvent*>(event);QLineEdit*line=qobject_cast<QLineEdit*>(obj);QStringtext=line->text();constQRegExpValidator*validator=NULL;intpos=0;switch(keyEvent->key()){caseQt::Key_0:caseQt::Key_1:caseQt::Key_2:caseQt::Key_3:caseQt::Key_4:caseQt::Key_5:caseQt::Key_6:caseQt::Key_7:caseQt::Key_8:caseQt::Key_9:text+=keyEvent->text();validator=qobject_cast<constQRegExpValidator*>(line->validator());if(validator->validate(text,pos)==QValidator::Acceptable&&text.length()==3)for(inti=0;i<3;i++){if(m_pLineEdit==line){line->insert(keyEvent->text());m_pLineEdit[i+1]->setFocus();m_pLineEdit[i+1]->selectAll();returntrue;}}returnQObject::eventFilter(obj,event);caseQt::Key_Backspace:if(text.isEmpty())for(inti=3;i>0;i--){if(m_pLineEdit==line){m_pLineEdit[i-1]->setFocus();m_pLineEdit[i-1]->setCursorPosition(m_pLineEdit[i-1]->text().length());m_pLineEdit[i-1]->backspace();returntrue;}}returnQObject::eventFilter(obj,event);caseQt::Key_Left:if(line->cursorPosition()==0)for(inti=3;i>0;i--){if(m_pLineEdit==line){m_pLineEdit[i-1]->setFocus();m_pLineEdit[i-1]->setCursorPosition(m_pLineEdit[i-1]->text().length());returntrue;}}returnQObject::eventFilter(obj,event);caseQt::Key_Right:if(line->cursorPosition()==text.length())for(inti=0;i<3;i++){if(m_pLineEdit==line){m_pLineEdit[i+1]->setFocus();m_pLineEdit[i+1]->setCursorPosition(0);returntrue;}}return;









引用地址:
snailbing 发表于08:03:59 | 编辑


  
分类:Qt 学习笔记|回复:0|浏览:2899|全站可见|转载
 

Powered by phpwind v8.7 Certificate Copyright Time now is:05-15 12:43
©2005-2016 QTCN开发网 版权所有 Gzip disabled