weixin_43900245
Ya丶你也想起舞吗
采纳率0%
2021-03-01 15:14

qt5 在有数据读取绘图的时候,使用鼠标绘图会有延迟

GUIWidget::GUIWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::GUIWidget)
{

 
   image = QImage(592,550,QImage::Format_RGB32);
   pix = QPixmap(592,550);
   QColor backColor=qRgb(255,255,255);
   image.fill(backColor);
   pix.fill(Qt::transparent);
   Paint();//绘制坐标
    timer = new QTimer(this);

 

 
    connect(timer,&QTimer::timeout,this,[=]{

 
        if(wid_ptr!=NULL)
       {
            buff=wid_ptr->buffer;

 
        arrayCut(wid_ptr->buffer,newbuf);
        }

 
        paint(newbuf);            //绘图
        update();         //刷新

 
    });
    timer->start(0); //300毫秒后发送timeout信号

 
/**************/

 

 
    ui->setupUi(this);
/*******************/
}

 

 

 

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

 

 

 

 
/***************绘图函数*******************/
void GUIWidget::paint(float newbuf[][6])
{
    QPainter p(&image);//指定当前绘画对象p

 

 
/************设置画笔参数***************/
    QColor color(255,0,0);
    QPen pen(color);
    pen.setWidth(3);
    p.setRenderHint(QPainter::Antialiasing);
/******************************************/

 
    p.setPen(pen);

 
    int xx=0;   //记录多少个需要绘制的线段
    int i=0,j=0;
    for(i=0;i<64;i++)
    {
        if(0 != newbuf[i][0])
        {
            for(j=0;j<64;j++)
            {
                if( 0 !=oldbuf[j][0])
                {

 
                    if(newbuf[i][0]==oldbuf[j][0])
                        if(newbuf[i][2]!=oldbuf[j][1]||newbuf[i][3]!=oldbuf[j][2])
                        {

 
                          {
                            linef[i]= QLine(296+(oldbuf[j][2]*20),550-(oldbuf[j][1]*1.5),296+(newbuf[i][3]*20),550-(newbuf[i][2]*1.5));
                            qDebug()<<oldbuf[j][0]<<"oldbuf"<<j<<"[1],oldbuf[j][2]"<<oldbuf[j][1]<<","<<oldbuf[j][2]<<endl;
                            qDebug()<<newbuf[i][0]<<"newbuf"<<i<<"[2],newbuf[i][3]"<<newbuf[i][2]<<","<<newbuf[i][3]<<endl;
                            qDebug()<<"*********************"<<endl;
                            xx++;
                            break;
                          }
                        }

 
                 }
                else

 
                    break;
            }
        }
        else
            break;
    }
    p.drawLines(linef,xx);

 
    for(i=0;i<64;i++)
    {
        oldbuf[i][0]=newbuf[i][0];
        oldbuf[i][1]=newbuf[i][2];
        oldbuf[i][2]=newbuf[i][3];
    }

 

 
}

 

 

 

 
void GUIWidget::paintEvent(QPaintEvent *)
{

 
    QPainter painter(this);
    painter.drawImage(0,0,image);

 

 

 
    QColor color(0,255,0);
    QPen pen(color);
    pen.setWidth(6);
    painter.setPen(pen);
    for(int i=0;i<64;i++)
    {
        if(0!=newbuf[i][0])
          painter.drawPoint(296+newbuf[i][3]*20,550-newbuf[i][2]*1.5);
    }

 
    QPainter p(&image);
    p.drawPixmap(0,0,pix);

 

 
    QPainter pp(&pix);
    for(int i=0; i<_lines.size(); i++)
    {
        //第i条线
        const QVector<QPoint> &line = _lines.at(i);

 
        //每一条线中所有的点,两两连起来,就是一条线
        for(int j=0; j<line.size()-1; j++)
        {

 
            pp.drawLine(line.at(j), line.at(j+1));

 
        }
    }

 

 

 

 
}

 

 

 

 
void GUIWidget::Paint()//绘制坐标系
{
    QPainter painter(&image);
        painter.setRenderHint(QPainter::Antialiasing, true);//设置反锯齿模式,好看一点
        int pointx=296,pointy=550;
        int height=550;
        painter.drawLine(0,pointy,592,pointy);//坐标轴x宽度为width
        painter.drawLine(pointx,pointy-height,pointx,pointy);//坐标轴y高度为height
        QPen penDegree;
           penDegree.setColor(Qt::black);
           penDegree.setWidth(2);
           painter.setPen(penDegree);

 

 
          //  double n=3.7;
           //画上x轴刻度线
           for(int i=0;i<8;i++)//分成8份
           {
               //选取合适的坐标,绘制一段长度为4的直线,用于表示刻度
               painter.drawLine(i*3.7*20,pointy,i*3.7*20,pointy-4);
               painter.drawText((i+0.65)*3.7*20,pointy-10,QString::number((int)(i+1)));
           }

 
           //y轴刻度线
           double _maStep=30;//y轴刻度间隔需根据最大值来表示
           for(int i=0;i<10;i++)
           {
               //主要就是确定一个位置,然后画一条短短的直线表示刻度。
               painter.drawLine(pointx,pointy-(i+1)*height/10,
                                pointx-4,pointy-(i+1)*height/10);
               painter.drawText(pointx-20,pointy-(i+0.85)*height/10,
                                QString::number((int)(_maStep*(i+1))));
           }

 
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

为你推荐