Qlineedit Imitation QQ Signature Box

Source: Internet
Author: User

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

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.