Qt Qwt打点画图

源程序:FunCurve.h

#ifndef FUNCURVE_H
#define FUNCURVE_H

#include <QMainWindow>
#include <qwt_plot_curve.h>
#include <QVector>
#include <qwt_symbol.h>

namespace Ui {
class FunCurve;
}

class FunCurve : public QMainWindow
{
    Q_OBJECT
    
public:
    explicit FunCurve(QWidget *parent = 0);
    ~FunCurve();
    
private:
    Ui::FunCurve *ui;
    QwtPlotCurve *  curve;
    QVector<double> xData;
    QVector<double> yData;
};

#endif // FUNCURVE_H


 

FunCurve.cpp

#include "funcurve.h"
#include "ui_funcurve.h"
FunCurve::FunCurve(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::FunCurve)
{
    ui->setupUi(this);
    ui->plot->setCanvasBackground(Qt::green);
    curve = new QwtPlotCurve();
    curve->attach(this->ui->plot);
    ui->plot->setAxisScale(QwtPlot::xBottom, 0.0, 10.0);
    ui->plot->setAxisScale(QwtPlot::yLeft, 0.0, 10.0);
    ui->plot->setAxisTitle(QwtPlot::xBottom,"x   -->");
    ui->plot->setAxisTitle(QwtPlot::yLeft,"y   -->");
    for(int x=0;x<100;x++)
    {
       xData.append((double)x);
       yData.append((double)x);
    }
    curve->setSymbol(new QwtSymbol(QwtSymbol::Cross, Qt::NoBrush,
        QPen(Qt::black), QSize(5, 5) ) );
    curve->setPen(QColor(Qt::darkGreen));
    curve->setStyle(QwtPlotCurve::Lines);
    curve->setCurveAttribute(QwtPlotCurve::Fitted);
    /*
    d_curves[i].setSymbol(new QwtSymbol(QwtSymbol::Ellipse, Qt::yellow,
        QPen(Qt::blue), QSize(5, 5) ) );
    d_curves[i].setPen(QColor(Qt::red));
    d_curves[i].setStyle(QwtPlotCurve::Sticks);
    i++;
    d_curves[i].setPen(QColor(Qt::darkBlue));
    d_curves[i].setStyle(QwtPlotCurve::Lines);   //*qwtexample中的实例,画图样式
    i++;
    d_curves[i].setPen(QColor(Qt::darkBlue));
    d_curves[i].setStyle(QwtPlotCurve::Lines);
    d_curves[i].setRenderHint(QwtPlotItem::RenderAntialiased);
    i++;
    d_curves[i].setPen(QColor(Qt::darkCyan));
    d_curves[i].setStyle(QwtPlotCurve::Steps);
    i++;
    d_curves[i].setSymbol(new QwtSymbol(QwtSymbol::XCross, Qt::NoBrush,
        QPen(Qt::darkMagenta), QSize(5, 5) ) );
    d_curves[i].setStyle(QwtPlotCurve::NoCurve);
    */
    curve->setRawSamples(&xData[0], &yData[0], xData.size());
    ui->plot->replot();
}
FunCurve::~FunCurve()
{
    delete ui;
}

效果如图:




  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
1. 创建一个Qt项目 2. 下载并安装Qwt库 3. 在Qt项目中添加Qwt库和头文件 4. 创建一个QWidget类,将其作为主窗口 5. 在QWidget类中添加QwtPlotQwtPlotCurve对象 6. 在构造函数中设置QwtPlotQwtPlotCurve的属性,如标题、坐标轴范围等 7. 在QWidget类中添加一个槽函数,用于实时更新数据 8. 在槽函数中添加数据到QwtPlotCurve对象中,并调用QwtPlot::replot()函数刷新界面 9. 在主函数中创建QWidget类的对象,显示界面 下面是一个简单的示例代码: ``` #include <QApplication> #include <QWidget> #include <qwt_plot.h> #include <qwt_plot_curve.h> #include <qwt_legend.h> #include <qwt_legend_item.h> #include <qwt_plot_grid.h> #include <QTimer> class RealtimePlot : public QWidget { Q_OBJECT public: RealtimePlot(QWidget *parent = nullptr); ~RealtimePlot(); private slots: void updatePlot(); private: QwtPlot *m_plot; QwtPlotCurve *m_curve; QTimer *m_timer; double m_x; double m_y; }; RealtimePlot::RealtimePlot(QWidget *parent) : QWidget(parent) { m_plot = new QwtPlot(this); m_plot->setTitle(&quot;Realtime Plot&quot;); m_plot->setCanvasBackground(Qt::white); m_plot->setAxisTitle(QwtPlot::xBottom, &quot;X Axis&quot;); m_plot->setAxisTitle(QwtPlot::yLeft, &quot;Y Axis&quot;); m_plot->setAxisScale(QwtPlot::xBottom, 0.0, 10.0); m_plot->setAxisScale(QwtPlot::yLeft, -1.0, 1.0); m_curve = new QwtPlotCurve(&quot;Realtime Curve&quot;); m_curve->setPen(Qt::blue, 2); m_curve->setRenderHint(QwtPlotItem::RenderAntialiased, true); m_curve->setLegendAttribute(QwtPlotCurve::LegendShowLine, true); QwtLegend *legend = new QwtLegend(); legend->setItemMode(QwtLegend::CheckableItem); m_plot->insertLegend(legend, QwtPlot::RightLegend); m_curve->attach(m_plot); m_plot->replot(); m_timer = new QTimer(this); connect(m_timer, SIGNAL(timeout()), this, SLOT(updatePlot())); m_timer->start(100); // update every 100 ms } RealtimePlot::~RealtimePlot() { } void RealtimePlot::updatePlot() { m_x += 0.1; m_y = sin(m_x); m_curve->setData(&m_x, &m_y, 1); m_plot->replot(); } int main(int argc, char *argv[]) { QApplication a(argc, argv); RealtimePlot plot; plot.show(); return a.exec(); } ```
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值