Qt gets focus and loses focus processing event (Focus event)

Source: Internet
Author: User

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-&GT;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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.