QT 5 Mini Practice Pure code Making custom buttons

Source: Internet
Author: User

We all know that the QT designers directly drag the button is rectangular with rounded corners of the pattern, then how do we set the custom button?

To design a button, we have to know what property the button has, first, the button must have a position

Second, the button must have a name. And how he reacts when we click the button. So we define two classes,

a qrect _rect; (or qpoint more appropriate)

The second one is QString _text;

Since there is a reaction, then we first think of is the mouse event, add a bool _press;

as well void Mousepressevent (Qmouseevent *), and mousereleaseevent (Qmouseevent *);

First we need to initialize the location inside the constructor.

Mybutton::mybutton (const QString text,qwidget *parent)

constructor, parent class is Widget

Qrect (0,0,90,25); Position is 0, 0 length is 90, width is 25

_text (text); or _text = text;

In general, we are in the painevent inside the thing, Draw button is also so, according to your favorite style, such as bloggers like the oval.

void MyButton::p ainevent (qpainevent *)

{

Qpainter p (this);

P.drawellipse (_rect); There are 4 parameters, namely position and length width, if the length and width are equal to a standard circle.

P.drawtext (_rect,_text,qtextoption (Qt::aligncenter);

Set the text to have three parameters, namely object, text, text position

}

If we need to click the left mouse button to change color, we can use ture or false to handle

void Mybutton::mousepressevent (Qmouseevent *)

{

_press = true;

This->update (); Click to update, call Painevent

}

void Mybutton::mousereleaseevent (Qmouseevent *)

{

_press = false;

This->update (); Click to update, call Painevent

}

void MyButton::p ainevent (qpainevent *)

{   

if (_press ==false)

P.setbrush (Qt::d Arkyello); Paint your favorite colors with a brush

Else

P.setbrush (Qt::d arkgreen);

}

QT 5 Mini Practice Pure code Making custom buttons

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.