QT Learning Pathway (27): Gradient fill

Source: Internet
Author: User

In front of the relevant knowledge about anti-aliasing, here is a gradient. Gradients are a common feature in drawing, simply by mixing several colors together, allowing them to transition naturally, rather than suddenly becoming another color. Gradient algorithm is more complex, poorly written words efficiency will be very low, fortunately, many graphics systems have built-in gradient function, QT is no exception. Gradients are generally used in padding, so the gradient is set inside the Qbrush.

QT provides three gradient brushes, namely linear gradients (qlineargradient), radiant gradients (qradialgradient), angular gradients (qconicalgradient). As shown in the following illustration (the picture is from C + + GUI programming with QT4, 2nd Edition):

Let's take a look at the use of linear gradient qlineargradient.

void PaintedWidget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
QLinearGradient linearGradient(60, 50, 200, 200);
linearGradient.setColorAt(0.2, Qt::white);
linearGradient.setColorAt(0.6, Qt::green);
linearGradient.setColorAt(1.0, Qt::black);
painter.setBrush(QBrush(linearGradient));
painter.drawEllipse(50, 50, 200, 150);
}

As in the previous plane, the code in the PaintEvent () function is only given here.

First we open the anti-aliasing and then create a Qlineargradient object instance. The Qlineargradient constructor has four parameters, namely, x1, Y1, x2, Y2, which is the starting and ending points of the gradient. Here, we start with the gradient (60, 50), to (200, 200).

The color of the gradient is specified in the Setcolorat () function. The following is the signature of this function:

void QGradient::setColorAt ( qreal position, const QColor & color )

It means to set the color of the position position. Among them, position is a 0-1-interval number. That is, the position is relative to the starting point and the ending point interval that we do when we create the gradient object. For example, this linear gradient, that is, from (60, 50) to (200, 200) of the line, in 0.2, one-fifth is set to white, in 0.6 is three-fifths is set to green, at 1.0 is the end of the set to black.

When we create Qbrush, we pass this gradient object in, which is our result:

So, how do we make the segments also gradient? You know, the line is drawn with a paintbrush! Here, if you look through the API documentation carefully, you will find that Qpen accepts Qbrush as an argument. That is, you can use a qbrush to create a qpen so that qbrush all the fill effects can be used on the brush!

void PaintedWidget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
QLinearGradient linearGradient(60, 50, 200, 200);
linearGradient.setColorAt(0.2, Qt::white);
linearGradient.setColorAt(0.6, Qt::green);
linearGradient.setColorAt(1.0, Qt::black);
painter.setPen(QPen(QBrush(linearGradient), 5));
painter.drawLine(50, 50, 200, 200);
}

Take a look at our gradient line!

Source: http://devbean.blog.51cto.com/448512/238168

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.