QT programming gets mouse move events

Source: Internet
Author: User

Want to do the following effect: When the mouse to move to the button, the button becomes larger, the icon for a large, the mouse is not on the button when the button becomes small, the icon is small, the feeling is more cool


Implementation principle: Set the listening properties for each button

void Mainhomeform::init () {    //For button registration event    Ui->systemsetbutton->installeventfilter (this);    Ui->zonesetbutton->installeventfilter (this);}

System Listener Listener Button object bool Mainhomeform::eventfilter (Qobject *target, qevent *e) {if (target = = Ui->systemsetbutton) {            if (e->type () = = Qevent::enter) {ui->systemsetbutton->resize (163,91);            Qicon *mouseonicon = new Qicon (":/new/prefix1/back/system set 2.png");        Ui->systemsetbutton->seticon (*mouseonicon);            } else if (e->type () = = Qevent::leave) {ui->systemsetbutton->resize (115,60);            Qicon *mouseofficon = new Qicon (":/new/prefix1/back/system settings. png");        Ui->systemsetbutton->seticon (*mouseofficon); }} else if (target = = Ui->zonesetbutton) {if (e->type () = = Qevent::enter) {Ui-&gt ;            Zonesetbutton->resize (163,91);            Qicon *mouseonicon = new Qicon (":/new/prefix1/back/Area Control 2.png");        Ui->zonesetbutton->seticon (*mouseonicon); } else if (e->type () = = Qevent::leave) {Ui->zonesetButton->resize (115,60);            Qicon *mouseofficon = new Qicon (":/new/prefix1/back/Zone control. png");        Ui->zonesetbutton->seticon (*mouseofficon); }    }}

OK, first the binding button of the Installeventfilter is the current form, in the form of the EventFilter event, see which button moved to the top, you can.


Another is that when the mouse is pressed down, move to the top of the button will change:

Track mouse movement events when the mouse moves to the middle button, change the icon size and content void Mainhomeform::mousemoveevent (Qmouseevent *e) {    e->accept ();    if (Enterbtn (E->pos (), Ui->systemsetbutton))    {        ui->systemsetbutton->setsizeincrement (163,91) ;        Qicon *mouseonicon = new Qicon (":/new/prefix1/back/system set 2.png");        Ui->systemsetbutton->seticon (*mouseonicon);    }} Write your own function to determine if the mouse is within a button area bool Mainhomeform::enterbtn (Qpoint pp, Qtoolbutton *btn) {    int height = btn->height ();    int width = btn->width ();    Qpoint Btnminpos = Btn->pos ();    Qpoint Btnmaxpos = Btn->pos ();    Btnmaxpos.setx (Btn->pos (). x () +width);    Btnmaxpos.sety (Btn->pos (). Y () +height);    if (pp.x () >= btnminpos.x () && pp.y () >= btnminpos.y ()        && pp.x () <= btnmaxpos.x () & & Pp.y () <= btnmaxpos.y ())        return true;    else        return false;}


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.