Qlineedit does not have a signal to focus and lose focus, you need to customize an input box that inherits from Qlineedit, and rewrite the focusinevent and focusoutevent events
//Mylineedit_h#ifndef Mylineedit_h#defineMylineedit_h#include<QLineEdit>classMylineedit: Publicqlineedit{Q_object Public: Mylineedit (Qwidget*parent=0); ~Mylineedit ();protected: Virtual voidFocusinevent (Qfocusevent *e); Virtual voidFocusoutevent (Qfocusevent *e);};#endif //Mylineedit_h`//MyLineEdit.cpp#include"myLineEdit.h"Mylineedit::mylineedit (Qwidget*parent): Qlineedit (parent) {}mylineedit::~Mylineedit () {}voidMylineedit::focusinevent (Qfocusevent *e) {Qpalette P=Qpalette (); P.setcolor (Qpalette::base,qt::green); //Qpalette::base is valid for editable input boxes, there are other types, specific viewing documentsSetPalette (P);}voidMylineedit::focusoutevent (Qfocusevent *e) {Qpalette P1=Qpalette (); P1.setcolor (Qpalette::base,qt::white); SetPalette (p1);} `//Widget.cpp#include"widget.h"#include"ui_widget.h"#include"MyLineEdit.h"#include<QGridLayout>#include<QMessageBox>Widget::widget (Qwidget*parent): Qwidget (parent), UI (Newui::widget) {UI->SETUPUI ( This); Init ();} Widget::~Widget () {DeleteUI;}voidWidget::init () {lineEdit1=NewMylineedit ( This); LineEdit2=NewMylineedit ( This); GridLayout=Newqgridlayout; GridLayout->addwidget (LineEdit1,0,0); GridLayout->addwidget (LineEdit2,1,0); SetLayout (gridLayout);}
Http://www.cnblogs.com/hicjiajia/archive/2012/05/30/2526768.html
http://blog.csdn.net/yu275184637/article/details/22865015
Qt gets focus and loses focus processing event (Focus event)