【Qt编程】基于QWT的曲线绘制及图例显示操作

     在 《QWT在QtCreator中的安装与使用》一文中,我们完成了QWT的安装,这篇文章我们讲讲基础曲线的绘制功能。

     首先,我们新建一个Qt应用程序,然后一路默认即可。这时,你会发现总共有:mainwindow.h,mainwindow.cpp,main.cpp,mainwindow.ui四个文件。

     然后,选中项目,添加新文件,添加一个c++类,我们假设命名为PlotLines,基类选择QwtPlot,选择继承自QWidget。

     接着,在pro文件中添加

                                         INCLUDEPATH +=D:\Qt\Qt5.3.0\5.3\msvc2010_opengl\include\QWT
                                         LIBS+= -lqwtd
      注意我这里是将绘制曲线单独用一个类PlotLines表示的,而不是向参考实例一样是直接放在其他类的内部。所以这里我们需要在类的头文件中添加关键性语句:
    #define QWT_DLL

      最后,在主文件main.cpp中添加我们类的头文件,并在函数中生成该类的实例并显示,修改后的main.cpp文件如下所示:

#include "mainwindow.h"
#include <QApplication>
#include"plotlines.h"
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
//    MainWindow w;//这里的主窗口我们没有使用,当然也可以在主窗口中显示曲线
//    w.show();

    PlotLines line;
    line.show();
    return a.exec();
}

PlotLines.h文件如下:
#ifndef PLOTLINES_H
#define PLOTLINES_H
#define QWT_DLL
#include<qwt_plot.h>
#include <qwt_plot_layout.h>
#include <qwt_plot_canvas.h>
#include <qwt_plot_renderer.h>
#include <qwt_plot_grid.h>
#include <qwt_plot_histogram.h>
#include <qwt_plot_curve.h>
#include <qwt_plot_zoomer.h>
#include <qwt_plot_panner.h>
#include <qwt_plot_magnifier.h>

#include <qwt_legend.h>
#include <qwt_legend_label.h>
#include <qwt_column_symbol.h>
#include <qwt_series_data.h>
#include <qpen.h>
#include <qwt_symbol.h>
#include <qwt_picker_machine.h>
class PlotLines : public QwtPlot
{
    Q_OBJECT
public:
    explicit PlotLines(QWidget *parent = 0);




private Q_SLOTS:
    void showItem(const QVariant &itemInfo, bool on);//点击图例,显示相应的曲线
};

#endif // PLOTLINES_H

PlotLines.cpp文件如下:
#include "plotlines.h"

PlotLines::PlotLines(QWidget *parent) :
    QwtPlot(parent)
{
    setTitle("图的标题");
//---------设置画布---------//
    QwtPlotCanvas *canvas=new QwtPlotCanvas();
    canvas->setPalette(Qt::white);
    canvas->setBorderRadius(10);
    setCanvas( canvas );
    plotLayout()->setAlignCanvasToScales( true );

    //-----------设置x,y坐标和范围--------------//
    setAxisTitle( QwtPlot::yLeft, "ylabel" );
    setAxisTitle( QwtPlot::xBottom, "xlabel" );
    setAxisScale(QwtPlot::yLeft,0.0,10.0);
    setAxisScale(QwtPlot::xBottom,0.0,10.0);

    //----------------设置栅格线-------------------//
    QwtPlotGrid *grid = new QwtPlotGrid;
    grid->enableX( true );//设置网格线
    grid->enableY( true );
    grid->setMajorPen( Qt::black, 0, Qt::DotLine );
    grid->attach( this );

    //-----------------开始画图----------------------//
    QwtPlotCurve *curve=new QwtPlotCurve("curve");
   // curve->setTitle( "信道"+QString( "%1 " ).arg( i+1));
    curve->setPen(Qt::blue,2);//设置曲线颜色 粗细
    curve->setRenderHint(QwtPlotItem::RenderAntialiased,true);//线条光滑化

    QwtSymbol *symbol = new QwtSymbol( QwtSymbol::Ellipse,
    QBrush( Qt::yellow ), QPen( Qt::red, 2 ), QSize( 6, 6) );//设置样本点的颜色、大小
    curve->setSymbol( symbol );//添加样本点形状

    QPolygonF points1, points2;//输入节点数据QPointF(x,y)
    points1<<QPointF(1,1)<<QPointF(2,2)<<QPointF(3,3)<<QPointF(4,4)<<QPointF(5,5)<<QPointF(6,6)<<QPointF(7,7);
    points2<<QPointF(1,2)<<QPointF(2,3)<<QPointF(3,4)<<QPointF(4,5)<<QPointF(5,6)<<QPointF(6,7)<<QPointF(7,8);
    curve->setSamples(points1);
    curve->attach( this );
    curve->setLegendAttribute(curve->LegendShowLine);//显示图例的标志,这里显示线的颜色。

    //曲线2的形状采用默认,即不单独设置画笔的颜色、样本点的显示
    QwtPlotCurve *curve2=new QwtPlotCurve("curve2");
    curve2->setSamples(points2);
    curve2->attach( this );
    curve2->setLegendAttribute(curve->LegendShowLine);

//--------------设置图例可以被点击来确定是否显示曲线-----------------------//
    QwtLegend *legend = new QwtLegend;
    legend->setDefaultItemMode( QwtLegendData::Checkable );//图例可被点击
    insertLegend( legend, QwtPlot::RightLegend );
    connect( legend, SIGNAL( checked( const QVariant &, bool, int ) ),
        SLOT( showItem( const QVariant &, bool ) ) );//点击图例操作

    QwtPlotItemList items = itemList( QwtPlotItem::Rtti_PlotCurve );//获取画了多少条曲线,如果为获取其他形状,注意改变参数
   //  qDebug()<<items;
    for ( int i = 0; i < items.size(); i++ )
    {

        if ( i == 0 )
        {
            const QVariant itemInfo = itemToInfo( items[i] );

            QwtLegendLabel *legendLabel =
                qobject_cast<QwtLegendLabel *>( legend->legendWidget( itemInfo ) );
            if ( legendLabel )
                legendLabel->setChecked( true );//

            items[i]->setVisible( true );
        }
        else
        {
            items[i]->setVisible( false );
        }
    }


    this->resize(600,400);

    this->replot();

    setAutoReplot( true );//设置自动重画,相当于更新

}
//点击图例,显示相应的曲线
void PlotLines::showItem(const QVariant &itemInfo, bool on)
{
    QwtPlotItem *plotItem = infoToItem( itemInfo );
    if ( plotItem )
        plotItem->setVisible( on );
}
 
 
其他的文件没有作任何改变,在此就不列出来了。显示结果如下图:
1、初始界面如下:


2、点击右上角的图例后:


 
 
本文所创建的PlotLines类,完成的功能如下:
1、坐标轴的绘制
2、根据数据点绘制相应的曲线
3、右上角的图例可以点击,并显示或隐藏对应曲线


  • 9
    点赞
  • 87
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
要使用 Qwt 绘制曲线图,需要先在项目中添加 Qwt 库,并且在代码中包含 Qwt 头文件。 以下是一个简单的示例代码,演示如何使用 Qwt 绘制曲线图: ```cpp #include <QApplication> #include <QMainWindow> #include <QVBoxLayout> #include <qwt_plot.h> #include <qwt_plot_curve.h> int main(int argc, char *argv[]) { QApplication a(argc, argv); // 创建主窗口 QMainWindow mainWindow; QWidget* centralWidget = new QWidget(&mainWindow); mainWindow.setCentralWidget(centralWidget); // 创建 Qwt 绘图控件 QwtPlot* plot = new QwtPlot(centralWidget); QVBoxLayout* layout = new QVBoxLayout(centralWidget); layout->addWidget(plot); // 创建曲线 QwtPlotCurve* curve = new QwtPlotCurve(); curve->setTitle("My Curve"); // 设置曲线数据 double xData[5] = {0.0, 1.0, 2.0, 3.0, 4.0}; double yData[5] = {0.0, 2.0, 4.0, 6.0, 8.0}; curve->setSamples(xData, yData, 5); // 将曲线添加到绘图控件中 curve->attach(plot); // 显示窗口 mainWindow.show(); return a.exec(); } ``` 在这个示例代码中,我们首先创建了一个 QMainWindow 对象,并将其设置为主窗口。然后,我们创建了一个 QwtPlot 对象,该对象用于绘制曲线图。我们还创建了一个 QwtPlotCurve 对象,该对象表示要绘制曲线。设置曲线数据后,我们将曲线添加到 QwtPlot 对象中,并将其显示在主窗口中。 需要注意的是,要使用 Qwt 绘制曲线图,需要在项目中添加 Qwt 库。具体的添加方法可以参考 Qt 文档中的说明。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值