Qt实现正弦曲线图(类似示波器功能)

写在前面:因为Qt的GUI界面坐标和数学坐标系不同,所在显示时要进行坐标转换,GUI界面是以左上角为原点坐标的

头文件:

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
private slots:
    int OnSettingsChanged();
};

#endif // MAINWINDOW_H
sinwidget.h(继承于QWidget)
#ifndef SINWIDGET_H
#define SINWIDGET_H

#include 
#include 
#include 
#include 
#include 

class SinWidget : public QWidget
{
    Q_OBJECT
public:
    explicit SinWidget(QWidget *parent = 0);
public:
    void Adjust(int period, int grain, int radius);
    QPoint origin();            // 坐标原点位置
    QPoint toCoord(QPoint p);   // 将GUI窗口的点(x,y)转换成坐标系统里的点
    QPoint fromCoord(QPoint p); // 将坐标系内的(x,y)转成GUI的坐标

signals:

public slots:

private:
    virtual void paintEvent(QPaintEvent *event );
    int m_period, m_grain, m_radius;
};

#endif // SINWIDGET_H

源文件:

main.cpp

#include "mainwindow.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}
mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    ui->m_ctlPeriod->setValue(50);
    ui->m_ctlGrain->setValue(1);
    ui->m_ctlRadius->setValue(50);

    connect(ui->m_ctlRadius, SIGNAL(valueChanged(int)),
            this, SLOT(OnSettingsChanged()));

    connect(ui->m_ctlGrain, SIGNAL(valueChanged(int)),
            this, SLOT(OnSettingsChanged()));

    connect(ui->m_ctlPeriod, SIGNAL(valueChanged(int)),
            this, SLOT(OnSettingsChanged()));

    OnSettingsChanged();
}

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

int MainWindow::OnSettingsChanged()
{
    ui->frame->Adjust(ui->m_ctlPeriod->value(),
                     ui->m_ctlGrain->value(),
                     ui->m_ctlRadius->value());

    return 0;
}
sinwidget.cpp

#include "sinwidget.h"

SinWidget::SinWidget(QWidget *parent) :
    QWidget(parent)
{
    m_period = 20;  //周期
    m_grain = 1;    //粒度
    m_radius = 10; // 振幅
}

void SinWidget::Adjust(int period, int grain, int radius)
{
    m_period = period;
    m_grain = grain;
    m_radius = radius;
    update(); // 更新重绘
}

QPoint SinWidget::origin()
{
    QRect r = this->rect();
    return r.center();
}
// 将GUI窗口的点(x,y)转换成数学坐标系统里的点
QPoint SinWidget::toCoord(QPoint p)
{
    // 圆心
    QPoint o = this->origin();
    QPoint result =  p - o;
    result.setY(0 - result.y()); // y坐标反转
    return result;
}
// 将数学坐标系内的(x,y)转成GUI的坐标
QPoint SinWidget::fromCoord(QPoint p)
{
    // 圆心
    QPoint o = this->origin();
    p.setY(0 - p.y()); // y坐标反转
    return p + o;

}

void SinWidget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);

    int width = this->width();
    int height = this->height();
    QRect rect(0,0, width, height);

    // 设置背景为黑色
    painter.setBrush(QBrush(QColor(0x00,0x00, 0x00)));
    painter.drawRect(rect);

    painter.setPen(QPen(QColor(0, 255, 0))); // 设置线条为绿色

    QPoint o = origin();
    painter.drawLine(QPoint(0, o.y()), QPoint(width, o.y())); // x轴
    painter.drawLine(QPoint(o.x(), 0), QPoint(o.x(), height));// y轴


    // 正弦曲线: 从坐标原点,向左、向右伸展
    QPoint p1(0,0);
    for(int x=0; x
设计界面:(用到了Spin Box 、Label、Widget、部件)
Qt实现正弦曲线图(类似示波器功能)_第1张图片

注:Widget部件为提升的自定义部件,即如下图;

Qt实现正弦曲线图(类似示波器功能)_第2张图片

效果图:

Qt实现正弦曲线图(类似示波器功能)_第3张图片

你可能感兴趣的:(学习记录之Qt篇)