QT中的波形图(逻辑中的功能问题)
Wave diagram in QT (functionality issues in logic)
QT 中的波形图。逻辑上存在一些功能性问题。我使用 QPainter 逻辑地尝试了波形图。波浪图画得很完美,但是画了一条线。任何人都可以帮助我解决这个问题。
//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();
不是 肯定 (a) 如果这是导致你的问题的原因,因为你有未定义的行为(意思是 任何东西 可能会发生)但是,第一次通过循环时,您在 xx
和 yy
被赋予值之前使用它们:
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 );
这可能是在使用前将它们初始化为零的简单情况,因为这可能是初始点的最佳偏移量。
(a)肯定可行,因为初始值xx
和[=12] =] 可能会将初始点设置为该直线的最右端。
这意味着初始画线不是构成余弦波一部分的小线,而是从该点到该波最左边的点。您可以通过简单地将 break
作为 for
循环中的最终语句并看到直线是唯一出现的东西来检查这一点。
您可以也通过实际调试代码来检查它,或者在 x
和 y
的计算处放置一个断点,然后查看 xx
和 yy
值是,或者在每次使用它们之前将它们打印出来。调试技能是您武器库中的宝贵工具。
如果是这种情况,在循环开始之前将 xx
和 yy
设置为零应该 肯定 可以解决您的问题。
QT 中的波形图。逻辑上存在一些功能性问题。我使用 QPainter 逻辑地尝试了波形图。波浪图画得很完美,但是画了一条线。任何人都可以帮助我解决这个问题。
//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();
不是 肯定 (a) 如果这是导致你的问题的原因,因为你有未定义的行为(意思是 任何东西 可能会发生)但是,第一次通过循环时,您在 xx
和 yy
被赋予值之前使用它们:
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 );
这可能是在使用前将它们初始化为零的简单情况,因为这可能是初始点的最佳偏移量。
(a)肯定可行,因为初始值xx
和[=12] =] 可能会将初始点设置为该直线的最右端。
这意味着初始画线不是构成余弦波一部分的小线,而是从该点到该波最左边的点。您可以通过简单地将 break
作为 for
循环中的最终语句并看到直线是唯一出现的东西来检查这一点。
您可以也通过实际调试代码来检查它,或者在 x
和 y
的计算处放置一个断点,然后查看 xx
和 yy
值是,或者在每次使用它们之前将它们打印出来。调试技能是您武器库中的宝贵工具。
如果是这种情况,在循环开始之前将 xx
和 yy
设置为零应该 肯定 可以解决您的问题。