用QT写CAN分析软件6--仿CANoe曲线的实现

1、新建个放坐标的类

#ifndef AXISMODEL_H
#define AXISMODEL_H

#include <QPoint>
#include <QPainter>
#include <QMap>

class AxisModel
{
public:
    AxisModel();

    QPointF startPoint;
    QPointF endPoint;

    double max;
    double min;

    double interval ;

    double xLocation;
    double yLocation;


    QMap<double,QString> Lables;
};

#endif // AXISMODEL_H

2、新建个每个信号曲线的类

#ifndef LINEBASE_H
#define LINEBASE_H

#include <QPoint>
#include <QPainter>
#include "axismodel.h"
#include <QPolygon>
#include <QList>



class LineBase
{
public:
    LineBase();

    int lineI;
    int lineJ;

    AxisModel yAxis;

    QString name;

     QList<QPointF> Points;

     QPolygonF polygon;

     void add(double x,double y);
     void addReal(double x,double y);
};

#endif // LINEBASE_H
#include "linebase.h"

LineBase::LineBase()
{
    lineI = 50000000;
    lineJ = 50000000;

    yAxis.xLocation = 56;
    yAxis.yLocation = 10;

   // polygon = new QPolygon();



   // Points<<QPointF(0,0)<<QPointF(10,10)<<QPointF(20,20);

    // polygon<< QPointF(0,0)<<QPointF(10,10)<<QPointF(20,20);
}

void LineBase::add(double x, double y)
{
    polygon.append(QPointF(x,y));
}

void LineBase::addReal(double x, double y)
{
    Points.append(QPointF(x,y));
}


3、新建个画曲线的类基于QWidget,然后把界面上的widget提升为此类

#ifndef DRAWLINE_H
#define DRAWLINE_H

#include <QWidget>
#include <QPainter>
#include <QPen>
#include "axismodel.h"
#include <QPoint>
#include <QList>
#include "linebase.h"

class DrawLine : public QWidget
{
    Q_OBJECT
public:
    explicit DrawLine(QWidget *parent = 0);
        ~DrawLine();

    QList<LineBase> Lines;

    void getRealValue(int i,int j,double x, double y);
    void getxAxisPoint();
    void updataPoint();

signals:

public slots:

private:

    AxisModel xAxis;



protected:
    void paintEvent(QPaintEvent *event);
};

#endif // DRAWLINE_H

利用void DrawLine::paintEvent(QPaintEvent *event)来对界面进行做画

利用updata方法来更新界面。

#include "drawline.h"
#include <QDebug>
#include <QPainterPath>

DrawLine::DrawLine(QWidget *parent) : QWidget(parent)
{
    this->xAxis.max = 30;
    this->xAxis.min = 0;
    this->xAxis.interval = 10;
    this->xAxis.yLocation = 30;
    this->xAxis.xLocation = 60;

}

DrawLine::~DrawLine()
{
    //  delete xAxis;
}

void DrawLine::getRealValue(int i,int j,double x, double y)
{
    for(int k = 0;k<Lines.size();k++)
    {
        if(Lines[k].lineI == i && Lines[k].lineJ == j)
        {
            if(x > xAxis.max)
            {
                double dd = x-xAxis.max;
                xAxis.min += dd;
                xAxis.max = x;

                updataPoint();
            }

            AxisModel yAxis = Lines[k].yAxis;
            double xx = xAxis.startPoint.x() + (x - xAxis.min) * (xAxis.endPoint.x() - this->xAxis.startPoint.x()) / (xAxis.max - xAxis.min) ;
            double yy = yAxis.startPoint.y() + (yAxis.max - y) * (yAxis.endPoint.y() - yAxis.startPoint.y()) / (yAxis.max - yAxis.min) ;

            Lines[k].addReal(x,y);
            Lines[k].add(xx,yy);

           // qDebug()<<"实际值:"<<x<<","<<y;
           // qDebug()<<"计算值:"<<xx<<","<<yy;

            update();
            break;
        }
    }
}

void DrawLine::getxAxisPoint()
{
    this->xAxis.startPoint = QPointF(this->xAxis.xLocation , this->height() - this->xAxis.yLocation);
    this->xAxis.endPoint = QPointF(this->width()-6 , this->height() - this->xAxis.yLocation);
}

void DrawLine::updataPoint()
{
    for(int k = 0;k<Lines.size();k++)
    {
        Lines[k].polygon.clear();

        for(int p=0;p< Lines[k].Points.size();p++)
        {
            if(Lines[k].Points[p].x()>=xAxis.min && Lines[k].Points[p].x()<= xAxis.max )
            {
                AxisModel yAxis = Lines[k].yAxis;
                double x1 = xAxis.startPoint.x() + (Lines[k].Points[p].x() - xAxis.min) * (xAxis.endPoint.x() - this->xAxis.startPoint.x()) / (xAxis.max - xAxis.min) ;
                double y1 = yAxis.startPoint.y() + (yAxis.max - Lines[k].Points[p].y()) * (yAxis.endPoint.y() - yAxis.startPoint.y()) / (yAxis.max - yAxis.min) ;
                Lines[k].add(x1,y1);
            }
        }
    }
}



void DrawLine::paintEvent(QPaintEvent *event)
{
    getxAxisPoint();
    QPainter p(this) ;
    p.drawLine(xAxis.startPoint,xAxis.endPoint);

    for(int i = 0;i<=xAxis.interval;i++)
    {
        double d =xAxis.startPoint.x() +  i * (xAxis.endPoint.x() - xAxis.startPoint.x()) / xAxis.interval;
        QString lb = QString("%1").arg(xAxis.min + i * (xAxis.max - xAxis.min) / xAxis.interval,0,'f',1);
        p.drawLine(QPointF(d,xAxis.startPoint.y()),QPointF(d,xAxis.startPoint.y() + 10) );
        p.drawText(QPointF(d-6,xAxis.startPoint.y() + 20),lb);
        //qDebug()<<"标签:"<<xAxis.max<<","<<lb;
    }

    for(int i = 0;i<this->Lines.size();i++)
    {
        double num = this->Lines.size();
        double d = (this->height() - this->xAxis.yLocation - 10) / num;

        this->Lines[i].yAxis.startPoint=QPointF(this->xAxis.xLocation - 6 , 10 + i * d);
        this->Lines[i].yAxis.endPoint=QPointF(this->width() - 6  , 10 + i * d + d - 10);

        p.drawLine(this->Lines[i].yAxis.startPoint,QPointF(this->Lines[i].yAxis.startPoint.x(),this->Lines[i].yAxis.endPoint.y()));

        p.drawLine(this->Lines[i].yAxis.startPoint,QPointF(this->Lines[i].yAxis.startPoint.x()-10 ,this->Lines[i].yAxis.startPoint.y()));
        p.drawLine(QPointF(this->Lines[i].yAxis.startPoint.x() ,this->Lines[i].yAxis.endPoint.y()),QPointF(this->Lines[i].yAxis.startPoint.x()-10 ,this->Lines[i].yAxis.endPoint.y()));

        QString max = QString("%1").arg(this->Lines[i].yAxis.max,0,'f',1);
        p.drawText(QPointF(this->Lines[i].yAxis.startPoint.x() - 54,this->Lines[i].yAxis.startPoint.y() + 5),max);
        QString min = QString("%1").arg(this->Lines[i].yAxis.min,0,'f',1);
        p.drawText(QPointF(this->Lines[i].yAxis.startPoint.x() - 54,this->Lines[i].yAxis.endPoint.y() + 5),min);

        QString name = this->Lines[i].name;
        double y =( this->Lines[i].yAxis.startPoint.y() + this->Lines[i].yAxis.endPoint.y()) / 2.0;
        p.drawText(QPointF(this->Lines[i].yAxis.startPoint.x() - 54,y),name);

     //   qDebug()<<this->Lines[i].polygon.size();
        if(this->Lines[i].polygon.size()>2)
        {
            p.drawPolyline(this->Lines[i].polygon);
        }
    }

    p.end();
}

 

4、在主界面的接收CAN数据,把接收到的数据传给画曲线

void MainWindow::UpdataReceive(CanDataBase *cdb)
{
    QString  str;
    str += cdb->IsSend?"发送 ":"接收 ";
    // str += QString::number(cdb->sTime,'f',6);
    str += cdb->Time.toString("HH:mm:ss.zzz");
    str += "  ID:0x";
    str += QString::number(cdb->Id,16);
    str += cdb->IsExtendFrame?" 扩展帧 ":" 标准帧 ";
    str += " 数据:";
    str += cdb->StrData;

    QListWidgetItem *item = new QListWidgetItem(str);

    if(cdb->IsSend)
        item->setForeground(Qt::red);

    ui->lwDataDisplay->addItem(item);
    ui->lwDataDisplay->scrollToBottom();

    if(  ui->lwDataDisplay->count() >= 50)
        ui->lwDataDisplay->removeItemWidget(  ui->lwDataDisplay->takeItem(0));


    for(int i=0;i<this->Dbc->MsgList.size();i++)
    {
        if(this->Dbc->MsgList[i].Can.Id == cdb->Id)
        {
            this->Dbc->MsgList[i].getValue(cdb->Data,cdb->Len);

            model->item(i,2)->setText(cdb->StrData);

            for(int j=0;j<this->Dbc->MsgList[i].SingalList.size();j++)
            {
                model->item(i)->child(j,2)->setText(this->Dbc->MsgList[i].SingalList[j].GetStringValue());

                ui->widget->getRealValue(i,j,cdb->sTime,this->Dbc->MsgList[i].SingalList[j].GetValue());
            }
        }
    }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值