Qt 2D Drawing Gradient Fill (three gradient modes)

Source: Internet
Author: User

There are three gradient modes available in QT, namely linear gradient, circular gradient, and conical gradient. If you can skillfully apply them, you can design a dazzling filling effect.


Linear gradient:

1. Change the function as follows:

void Dialog::p aintevent (qpaintevent *)
{
qpainter painter (this);
Qlineargradient lineargradient (100,150,300,150);
From point (100,150) to end (300,150), determine a line
Lineargradient.setcolorat (0,qt::red);
Lineargradient.setcolorat (0.2,qt::black);
Lineargradient.setcolorat (0.4,qt::yellow);
Lineargradient.setcolorat (0.6,qt::white);
Lineargradient.setcolorat (0.8,qt::green);
Lineargradient.setcolorat (1,qt::blue);
Set the line start point to 0, the end point to 1, and then the segment setting color
Painter.setbrush (lineargradient);
Painter.drawrect (100,100,200,100);
Draw a rectangle with a linear gradient line just above the horizontal centerline of the rectangle
}

The effect is as follows:

Circular gradient:

1. Change the function contents as follows:

Qradialgradient radialgradient (200,100,100,200,100);
The parameters are the Circle Center (200,100), RADIUS 100, and focus (200,100) of the circular gradient respectively.
This causes the focus and center to overlap, resulting in an outward gradient from the center.
Radialgradient.setcolorat (0,qt::black);
Radialgradient.setcolorat (1,qt::yellow);
The gradient is from the focus to the entire circle, with the focus at the starting point 0 and the circle boundary at 1
qpainter painter (this);
Painter.setbrush (radialgradient);
Painter.drawellipse (100,0,200,200);
Draw the circle so that it coincides with the circle above the circular gradient

The effect is as follows:

2. To change the effect of the fill, simply change the position of the focus and the color position of the gradient.

Change focus Position: Qradialgradient radialgradient (200,100,100,100,100);

The effect is as follows:

Tapered gradient:

1. Change the function contents as follows:

Taper gradient
Qconicalgradient conicalgradient (50,50,0);
Center point is (50,50), starting angle is 0
Conicalgradient.setcolorat (0,qt::green);
Conicalgradient.setcolorat (1,qt::white);
Counter-clockwise from the 0-degree angle of the center of the circle
qpainter painter (this);
Painter.setbrush (conicalgradient);
Painter.drawellipse (0,0,100,100);

The effect is as follows:

2. You can change the start angle to change the fill effect

Qconicalgradient conicalgradient (50,50,30);

The start angle is set to 30 degrees, and the effect is as follows:

In fact, three kinds of gradient settings are focus and gradient color position, if you want to design a beautiful gradient effect, but also have art skills Ah!

Http://www.cnblogs.com/bingcaihuang/archive/2010/12/01/1893522.html

Qt 2D Drawing Gradient Fill (three gradient modes)

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.