Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-64366

QPainter drawLine connects the startpoint and endpoint itself

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • P3: Somewhat important
    • None
    • 5.9.2
    • GUI: Painting

    Description

      I am learning Qt.I want to display a big number of  data,like monitoring data with 3500000 points,I want to display it with timeline,I tried to make a QImage width very a huge width,put it in QScrollArea,it can not work,then i decide to write a widget to mimic that,I use a QSlider ,take QSlider value as the index of an array which holding the data,start from that index ,I take QWidget.width() of elements from the int array,draw them on a QPixmap,then put it on the QWidget.it can work well for the first time show,but since i start to drag the QSlider,the startpoint and endpoint are always connected automatically ,I can not find anything wrong with my code logic,I tried some ways to see if the QPainter executed as many times as the code logics defines,it seems QPainter executes one more time itself.

      //I alreadly use qrand()%300 to generate data,i put the data in an int array,
      below is a part of the code for drawing waves as shown in the picture,it is the last part in the  complete code.

      painter.setPen(pen);
      for(int i=0;i<1000;++i)   //1000 is the QWidget width
      { 
           if(i+slideV>110000) break;  //if it reaches the end of the array,break;
           QLine line(x1,y1,i,sample[i+slideV]);  //draw a line between lastpoint and currentpoint
           painter.drawLine(line); 
           x1=i; y1=sample[i+slideV];  //llastpoint=currentpoint
      }
      

       

      The complete code here.thanks.

      //////////////////////////////////////////////////////////this is the head file////////////////////////////////////////////
      #ifndef WIDGET_H
      #define WIDGET_H

      #include <QWidget>
      #include <QScrollBar>
      #include <QSlider>
      #include <QDebug>
      #include <QPainter>
      #include <QPixmap>
      #include <QPen>
      #include <QPaintEvent>
      #include <QPoint>
      #include <QColor>
      class Widget : public QWidget
      {
      Q_OBJECT

      public:
      Widget(QWidget *parent = 0);
      ~Widget();
      void paintEvent(QPaintEvent*e);
      void paint();

      QScrollBar *scrollbar;

      QPainter *painter1;
      QPixmap *pixmap1;
      QPixmap *pixmap;
      QSlider *slider;
      int *sample =new int[110000];
      int *sample1 =new int[110000];
      QPoint p;
      int slideV;
      int x1=-1,y1=-1,x2=3,y2=3,x3=0,y3=0;
      QPen pen;
      QPen pen1,pen2,pen3,pen4;
      int paperspeed=2;

      QPoint points[6] = {
      QPoint(400, 560),
      QPoint(500, 500),
      QPoint(590, 573),
      QPoint(595, 540),
      QPoint(609, 510),
      QPoint(650, 400),

      };

      public slots:
      void currentValue1();
      void currentPosition1();

      };

      #endif // WIDGET_H

      ///////////////////////////////////////////////////////////////below is the cpp file//////////////////

      #include "widget.h"

      int MOD(int x,int y)

      { return x-(int (x/y))*y; }

      Widget::Widget(QWidget *parent)
      : QWidget(parent)
      {
      pen1.setColor(QColor(255,170,255));
      pen2.setColor(QColor("grey"));
      pen3.setColor(QColor(85,255,127));
      pen4.setColor(QColor(255,151,47));

      pixmap1=new QPixmap(1002,570);

      pen.setColor(QColor("red"));
      painter1=new QPainter(this);
      pixmap=new QPixmap(1002,570);
      //pixmap->fill(Qt::white);

      for (int i=0;i<110000;i++)//generate the sample data

      { sample[i]=qrand()%200; }

      for (int i=0;i<110000;++i)
      {
      int cal1=qrand()%570;
      if(cal1<300)

      {cal1=570-cal1; sample1[i]=cal1; }

      else
      sample1[i]=cal1;
      }

      //qDebug("sample value is:%d",sample[5]);
      resize(1000,600);
      //scrollbar=new QScrollBar(this);

      slider=new QSlider(Qt::Horizontal,this);
      slider->setMaximumSize(width(),20);
      slider->setGeometry(QRect(0,height()-20,width(),height()-20));
      slider->setMaximum(110000);
      // qDebug("current value is:%d",slider->maximum());
      //qDebug("current value is:%d",slider->minimum());
      connect(slider,&QSlider::valueChanged,this,&Widget::currentValue1);
      connect(slider,&QSlider::sliderMoved,this,&Widget::currentPosition1);
      //connect(slider,&QSlider::valueChanged,this,&Widget:;

      }

      Widget::~Widget()

      { delete sample; delete sample1; }

      void Widget::currentValue1()

      { // qDebug("current value is:%d",slider->value()); slideV=slider->value(); update(); }

      void Widget::currentPosition1()

      { // qDebug("current position is:%d",slider->sliderPosition()); }

      void Widget::paintEvent(QPaintEvent *e)

      { paint(); QPainter painter(this); painter.drawPixmap(QPoint(0,0), *pixmap); //qDebug("paintEvent is running"); }

      void Widget::paint()
      {
      //QPixmap *pixmap1=new QPixmap(1000,570);
      pixmap1->fill(Qt::white);
      QPainter painter(pixmap1);

      int i,j;
      int CTGtop=20;
      int offsetY=1;
      int linelarge=(pixmap1->height()-2*CTGtop)/280.0;

      painter.setPen(pen3);
      for(i=0;i<pixmap1->width();i++)

      { painter.drawLine(i,(210-160)*linelarge+CTGtop,i,(210-120)*linelarge+CTGtop); }

      for(i=0;i<16;i++){//draw the horizontal lines 画所有横线

      if(MOD(i,3)==0) painter.setPen(pen1);
      else painter.setPen(pen2);

      painter.drawLine(0,linelarge*i*10+CTGtop,pixmap->width(),linelarge*i*10+CTGtop);

      if(i<11)

      { if(MOD(i,2)==0) painter.setPen(pen1); else painter.setPen(pen2); painter.drawLine(0,pixmap1->height()-CTGtop-linelarge*i*10-offsetY,pixmap->width(),pixmap1->height()-CTGtop-linelarge*i*10-offsetY); }

      }

      int currentLine=slideV/25;
      int remainder1=slideV%25;
      int startPoint=25-remainder1;
      //int numberLines=1000/25;
      int howManyLines=(1000-remainder1)/25;
      //qDebug("here1");

      //画竖线 draw the vertical lines
      painter.drawLine(startPoint,CTGtop,startPoint,150*linelarge+CTGtop);
      painter.drawLine(startPoint,pixmap1->height()CTGtop-100*linelarge-offsetY,startPoint,pixmap1>height()-CTGtop-offsetY);

      int XY1=startPoint+25;
      for(int i=0;i<1000;++i)

      { painter.drawLine(XY1,CTGtop,XY1,150*linelarge+CTGtop); painter.drawLine(XY1,pixmap1->height()-CTGtop-100*linelarge-offsetY,XY1,pixmap1->height()-CTGtop-offsetY); XY1+=25; }

      int YX1=startPoint;//画时间刻度 draw the timeline
      int temp=howManyLines+currentLine;
      for(int i=currentLine+1;i<temp;++i){
      if(MOD(i,3)==0)

      { painter.setPen(pen4); painter.drawText(YX1,8,""+QString::number(i,10)+"'"); }

      YX1+=25;
      qDebug("YX1 is:%d",YX1);
      }

      int YY1=startPoint;
      for(int i=currentLine+1;i<temp;++i){
      if(MOD(i,30)==0){
      painter.setPen(QColor("black"));//pen().setColor(QColor("white"));
      for(j=0;j<16;j++){
      if(MOD(j,3)==0)

      { painter.drawText(YY1-5,linelarge*j*10+CTGtop,QString::number(210-(j/3)*30,10)); }

      if(j<11)

      { if(MOD(j,2)==0) painter.drawText(YY1-5,pixmap1->height()-CTGtop-linelarge*j*10-offsetY,QString::number((j/2)*20,10)); }

      }
      }
      YY1+=25;
      }

      int counterXX=0;
      qDebug("counterXXX run:%d",++counterXX);

      //开始画波形 draw the waves
      //int counter1=0;
      painter.setPen(pen);
      for(int i=0;i<1000;++i)
      { if(i+slideV>110000)
      break;
      //qDebug("counter1 run:%d",++counter1);
      // if(counter1==990)
      //

      { // painter.setPen(pen3); //break; // }

      QLine line(x1,y1,i,sample[i+slideV]);
      //painter.begin(pixmap1);
      painter.drawLine(line);
      //painter.end();



      x1=i;
      y1=sample[i+slideV];
      }







      for(int i=100;i<120;++i)//draw lines on the bottom grid,
      { //if(i+slideV>110000)
      // break;

      int value01=sample1[i+slideV];
      //QLine line(x2,y2,i,sample1[i+slideV]);
      //qDebug("counter run:%d",++counter);
      //if(counter==900)
      //{ // painter.setPen(pen3); //break; // }

      //painter.drawPolyline(points,6);
      // painter.begin(pixmap1);
      painter.drawLine(x2,y2,i,value01);

      x2=i;
      y2=sample1[i+slideV];
      //qDebug("x2=:%d",x2);
      //qDebug("y2=:%d",y2);
      }

      pixmap=pixmap1;
      //qDebug("PAINT Running");
      }

      Attachments

        Activity

          People

            sletta Gunnar Sletta
            shu shu yang
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: