Qt之QtPainter:漸變風格[Qt5開發實戰]

一、新建QT GUI, 基類選擇QWidget,取消「建立界面」this

二、在widget.h中添加#include <QPainter>和重寫繪畫事件定義:
 * protected:
    void paintEvent(QPaintEvent *event);
三、在widget.cpp中添加劇繪事件void Widget::paintEvent(QPaintEvent *event)3d

四、線性漸變QLinearGradientcode

void Widget::paintEvent(QPaintEvent *event)
{
    QPainter painter;

    painter.begin(this);
    QLinearGradient ling(QPointF(70, 70), QPoint( 140, 140 ) );  //從起點到終點的漸變
    ling.setColorAt(0, Qt::blue);  //起點到中心要顯示的顏色
    ling.setColorAt(1, Qt::green);
    ling.setSpread( QGradient::PadSpread );   //默認顯示模式

    QBrush brush(ling);
    painter.setBrush(brush);
    painter.drawRect(0, 0, 200, 200);

    painter.end();
}

void Widget::paintEvent(QPaintEvent *event)
{
    QPainter painter;

    painter.begin(this);
    QLinearGradient ling(QPointF(70, 70), QPoint( 140, 140 ) );  //從起點到終點的漸變
    ling.setColorAt(0, Qt::blue);  //起點到中心要顯示的顏色
    ling.setColorAt(1, Qt::green);
    ling.setSpread( QGradient::RepeatSpread );   //默認顯示模式

    QBrush brush(ling);
    painter.setBrush(brush);
    painter.drawRect(0, 0, 200, 200);

    painter.end();
}

void Widget::paintEvent(QPaintEvent *event)
{
    QPainter painter;

    painter.begin(this);
    QLinearGradient ling(QPointF(70, 70), QPoint( 140, 140 ) );  //從起點到終點的漸變
    ling.setColorAt(0, Qt::blue);  //起點到中心要顯示的顏色
    ling.setColorAt(1, Qt::green);
    ling.setSpread( QGradient::ReflectSpread );   //默認顯示模式

    QBrush brush(ling);
    painter.setBrush(brush);
    painter.drawRect(0, 0, 200, 200);

    painter.end();
}

五、圓心漸變QRadialGradientblog

void Widget::paintEvent(QPaintEvent *event)
{
    QPainter painter;
    painter.begin(this);

    QRadialGradient radg(100, 100, 50, 120, 120);  //在圓心(100, 100),半徑50的圓上漸變。漸變起點是(120, 120)
    radg.setColorAt( 0, Qt::black );
    radg.setColorAt( 1, Qt::white );
    //radg.setSpread(QGradient::PadSpread);   //默認
    QBrush brush(radg);
    painter.setBrush(brush);
    painter.drawRect(0, 0, 200, 200);
    painter.end();
}

 

void Widget::paintEvent(QPaintEvent *event)
{
    QPainter painter;
    painter.begin(this);

    QRadialGradient radg(100, 100, 50, 120, 120);  //在圓心(100, 100),半徑50的圓上漸變。漸變起點是(120, 120)
    radg.setColorAt( 0, Qt::black );
    radg.setColorAt( 1, Qt::white );
    radg.setSpread(QGradient::RepeatSpread);
    QBrush brush(radg);
    painter.setBrush(brush);
    painter.drawRect(0, 0, 200, 200);
    painter.end();
}

void Widget::paintEvent(QPaintEvent *event)
{
    QPainter painter;
    painter.begin(this);

    QRadialGradient radg(100, 100, 50, 120, 120);  //在圓心(100, 100),半徑50的圓上漸變。漸變起點是(120, 120)
    radg.setColorAt( 0, Qt::black );
    radg.setColorAt( 1, Qt::white );
    radg.setSpread(QGradient::ReflectSpread);
    QBrush brush(radg);
    painter.setBrush(brush);
    painter.drawRect(0, 0, 200, 200);
    painter.end();
}

六、座標角度漸變QConicalGradient事件

void Widget::paintEvent(QPaintEvent *event)
{
    QPainter painter;
    painter.begin(this);

    QConicalGradient cong( 100, 100, 45.0 );  //在(100, 100)座標上以45°爲中心漸變
    cong.setColorAt( 0, Qt::black );
    cong.setColorAt( 1, Qt::white );

    QBrush brush(cong);
    painter.setBrush(brush);
    painter.drawRect(0, 0, 200, 200);


    painter.end();
}

相關文章
相關標籤/搜索