Qt 绘制渐变线

先看效果:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QVector>
struct GradientLine
{
    double pointAX ;
    double pointAY ;
    double pointBX ;
    double pointBY ;

    double startLen;
    double endLen;
};

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void dradGradientLine(int startX,int startY,int endX,int endY,int startLen,int endLen);

private:
    Ui::MainWindow *ui;
    QVector<GradientLine> mVector;


protected:
    void paintEvent(QPaintEvent *);

};

#endif // MAINWINDOW_H


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPainter>
#include "math.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    dradGradientLine(90,120,300,400,1,2);
    dradGradientLine(90,150,90,400,1,2);
    dradGradientLine(46,99,10,234,1,2);
    dradGradientLine(46,88,10,555,1,8);



}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::dradGradientLine(int startX, int startY, int endX, int endY, int startLen, int endLen)
{
    GradientLine tempLine ;
    tempLine.pointAX = startX;
    tempLine.pointAY = startY;
    tempLine.pointBX = endX;
    tempLine.pointBY = endY;
    tempLine.startLen = startLen;
    tempLine.endLen = endLen;

    mVector.append(tempLine);
}
void MainWindow::paintEvent(QPaintEvent *e)
{

    for(int i = 0; i < mVector.size();i++)
    {
        GradientLine tempLine = mVector.at(i);
        int pointA1X ;
        int pointA2X ;
        int pointB1X ;
        int pointB2X ;
        int pointA1Y ;
        int pointA2Y ;
        int pointB1Y ;
        int pointB2Y ;

        if(tempLine.pointAX != tempLine.pointBX && tempLine.pointAY != tempLine.pointBY)
        {

            //
            double gradient = double(tempLine.pointBY - tempLine.pointAY)/double(tempLine.pointBX - tempLine.pointAX);
//            qDebug()<<"gradient:"<<gradient;
            double sinVal = sin(atan(gradient));
            double cosVal = cos(atan(gradient));

            double dAx = ((double)tempLine.startLen/2) * sinVal;
            double dAy = ((double)tempLine.startLen/2) * cosVal;
            double dBx = ((double)tempLine.endLen/2) * sinVal;
            double dBy = ((double)tempLine.endLen/2) * cosVal;

            pointA1X = qRound(tempLine.pointAX + dAx);
            pointA2X = qRound(tempLine.pointAX - dAx);
            pointB1X = qRound(tempLine.pointBX + dBx);
            pointB2X = qRound(tempLine.pointBX - dBx);
            pointA1Y = qRound(tempLine.pointAY - dAy);
            pointA2Y = qRound(tempLine.pointAY + dAy);
            pointB1Y = qRound(tempLine.pointBY - dBy);
            pointB2Y = qRound(tempLine.pointBY + dBy);

    //        qDebug()<<pointA1X<<pointA1Y;
    //        qDebug()<<pointA2X<<pointA2Y;
    //        qDebug()<<pointB1X<<pointB1Y;
    //        qDebug()<<pointB2X<<pointB2Y;
        }
        else if(tempLine.pointAX == tempLine.pointBX)//垂直
        {
            double dAx = ((double)tempLine.startLen/2) * 1;
            double dAy = 0;
            double dBx = ((double)tempLine.endLen/2) * 1;
            double dBy = 0;

            pointA1X = qRound(tempLine.pointAX + dAx);
            pointA2X = qRound(tempLine.pointAX - dAx);
            pointB1X = qRound(tempLine.pointBX + dBx);
            pointB2X = qRound(tempLine.pointBX - dBx);
            pointA1Y = qRound(tempLine.pointAY - dAy);
            pointA2Y = qRound(tempLine.pointAY + dAy);
            pointB1Y = qRound(tempLine.pointBY - dBy);
            pointB2Y = qRound(tempLine.pointBY + dBy);
        }
        else if(tempLine.pointAY == tempLine.pointBY)//水平
        {
            double dAx = ((double)tempLine.startLen/2) * 0;
            double dAy = ((double)tempLine.startLen/2) * 1;
            double dBx = ((double)tempLine.endLen/2) * 0;
            double dBy = ((double)tempLine.endLen/2) * 1;

            pointA1X = qRound(tempLine.pointAX + dAx);
            pointA2X = qRound(tempLine.pointAX - dAx);
            pointB1X = qRound(tempLine.pointBX + dBx);
            pointB2X = qRound(tempLine.pointBX - dBx);
            pointA1Y = qRound(tempLine.pointAY - dAy);
            pointA2Y = qRound(tempLine.pointAY + dAy);
            pointB1Y = qRound(tempLine.pointBY - dBy);
            pointB2Y = qRound(tempLine.pointBY + dBy);
        }

            // 绘制文本
    //        painter.drawLine(90,120,300,400);

            QPainter painter(this);

                // 反走样
                painter.setRenderHint(QPainter::Antialiasing, true);

                // 设置画笔颜色
                painter.setPen(QColor(255, 160, 90));

                // 各个点的坐标
                QPointF points[4] = {QPointF(pointA1X, pointA1Y), QPointF(pointA2X, pointA2Y), QPointF(pointB2X, pointB2Y), QPointF(pointB1X, pointB1Y)};
                // 绘制多边形
                painter.setBrush(QColor(255, 160, 90));
                painter.drawPolygon(points, 4);
    }



}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt是一款功能强大的跨平台C++图形用户界面开发框架,可以用于绘制彩色图和等值线。以下是绘制彩色图和等值线的实现逻辑。 要实现彩色图,首先需要获取要绘制的数据,通常以二维数组形式表示。然后,可以使用Qt的QImage类创建一个与数据大小相匹配的图像对象。接下来,可以使用QPainter类的函数来为图像设置颜色和渐变效果。按照数据的值和范围,可以为每个像素点设置对应的颜色,绘制出彩色图。 对于等值线绘制,首先也需要获取要绘制的数据。然后,可以使用Qt的QPainter类的函数绘制线条和曲线的路径对象。可以将数据的每个点依次连接起来,形成一个等值线的路径。然后,可以在QImage对象上绘制这条路径,通过改变路径的颜色和线宽可以调整等值线的样式。最后,将绘制好的图像显示在Qt的窗口上即可。 绘制彩色图和等值线主要依赖于Qt的绘图功能和像素级操作能力。通过合理利用QImage、QPainter等类和函数,可以方便地绘制彩色图和等值线,实现各种样式和效果的显示。同时,Qt也提供对绘图对象的变换、旋转、缩放等操作,使得绘制的彩色图和等值线更加灵活多样。 绘制彩色图和等值线可以用于数据可视化、科学计算、地图绘制等众多领域。Qt作为一种主流的图形用户界面开发工具,其绘图功能强大,使用方便,可以满足各种绘制彩色图和等值线的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值