Search code examples
qtc++11qt4

Wave diagram in QT (functionality issues in logic)


Wave diagram in QT.. There was some functionality issues in logic. I tried a wave diagram logically using QPainter. Wave diagram is drawn perfectly but there was one line has drawn.. Any one help me to solve this problem.

//dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"
#include <QtMath>
#include <QDebug>
#include <QDialog>
#include <QTGui>
#include <QtCore>


Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
}

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

void Dialog::paintEvent(QPaintEvent *e)
{
    e->accept();

    float scale = 40;
    //boolean negativeX = true;
    float width = 500;
    float height = 108;

    QPainter painter(this);

    QPen linepen(Qt::red);
    linepen.setWidth(2);
    QPoint p1;
    QPoint p2;

    painter.setPen(linepen);

    float xx, yy, dx=4, x0=width / 2, y0=height / 2;

    //float iMax = (width - x0) / dx;
    float iMax = 63;

    //float iMin = negativeX ? -x0 / dx : 0;
    float iMin = -63;

    for (int i=iMin;i<=iMax;i++) {
           float x = x0+xx;
           float y = y0-yy;
         p1.setX(x);
         p1.setY(y);

         xx = dx*i;
         float xscl = xx/scale;
         yy = scale * qCos( 3* xscl );

           x = x0+xx;
           y = y0-yy;

         p2.setX(x);
         p2.setY(y);

         painter.drawLine(p1, p2);

     }

   }

// dialog.h

protected:
    void paintEvent(QPaintEvent *e);

// main.cpp

QApplication a(argc, argv);
Dialog w;
w.show();

enter image description here


Solution

  • Not sure (a) if this is what's causing your problem since you have undefined behaviour (meaning anything could happen) but, the first time through the loop, you use xx and yy before they've been given a value:

    float xx, yy, ...                 // will be arbitrary value.
    float iMax = 63;
    float iMin = -63;
    for (int i=iMin;i<=iMax;i++) {
       float x = x0+xx;               // shouldn't be using them here.
       float y = y0-yy;
    
       p1.setX(x);
       p1.setY(y);
    
       xx = dx*i;                     // not set until here.
       float xscl = xx/scale;
       yy = scale * qCos( 3* xscl );
    

    This may be a simple case of initialising them to zero before use, since that's likely to be the best offset for the initial point.


    (a) It's certainly feasible that this is the case since the initial value of xx and yy may be such that it sets the initial point to the rightmost end of that straight line.

    That means the initial line draw, far from being a small line forming part of the cosine wave, will be from that point to the leftmost point of that wave. You can check this by simply putting a break as the final statement within the for loop and seeing that the straight line is the only thing that appears.

    You could also check it by actually debugging the code, either putting a breakpoint at the calculation of x and y and seeing what the xx and yy values are, or printing them out before each time you use them. Debugging skills are an invaluable tool in your arsenal.

    If that is the case, setting xx and yy to zero before the loop starts should definitely fix your problem.