After a day of tinkering today, I finally realized the Qlineedit control in the custom Qt.
This problem for beginners, the main points are the following several difficulties:
1. Inheriting Qlineedit controls
2.QSS set the relevant style of Qlineedit, can save a lot of code
3. Customizing related events
void Mousepressevent (Qmouseevent *event); When the mouse clicks, the Programming input box void Keypressevent (Qkeyevent *event); When you press ENTER, the void Mousemoveevent (Qmouseevent *event) is processed; When the mouse moves, the display effect
OK, let's step through this custom control.
First, the first step is to create a new class that inherits Qlineedit, and Qtcreator automatically implements some trivial code for you.
The second step is to use Photoshop to make a prominent highlight image, like the QQ signature box:.
The third step begins to implement the specific code.
First of all, the constructor needs to set some QSS style, this step can save a lot of code, specific style corresponding function, please the reader to study.
This->setstylesheet ("qlineedit{Background:rgba (0,0,0,0%); border:1px; FONT:10PT} " " qlineedit:hover{Border-image:url (:/btn_background.png);} " " Qlineedit:!hover{background:rgba (0,0,0,0%);} " " qlineedit:focus {background:white;border-image:none; border:1px groove lightgray; border-radius:2px} ");
Implement three inherited event functions:
void Qslineedit::keypressevent (Qkeyevent *event) { if (event->key () = = qt::key_enter-1) this-> Clearfocus (); Qlineedit::keypressevent (event);} void Qslineedit::mousepressevent (Qmouseevent *event) { this->setfocus (); This->setcursor (Qcursor (qt::ibeamcursor)); Qlineedit::mousepressevent (event);} void Qslineedit::mousemoveevent (Qmouseevent *event) { if (This->hasfocus ()) This->setcursor (QCursor ( Qt::ibeamcursor)); else this->setcursor (qcursor (qt::arrowcursor)); Qlineedit::mousemoveevent (event);}
This step basically achieves the full effect, but there is one problem to note: When you click elsewhere in the form, the custom control will not lose focus, and you will not be able to inherit focusoutevent; So one more event is implemented in the form mousepressedevent
void Dialog::mousepressevent (Qmouseevent *e) { leedit->clearfocus (); Qdialog::mousepressevent (e);}
So OK, open up very simple question, really think of my half-day event. Source code: Widgetedit.rar
Reprint Please specify source: http://www.cnblogs.com/xufeiyang/p/3310670.html
Qlineedit Imitation QQ Signature Box