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

简介:      在《QWT在QtCreator中的安装与使用》一文中,我们完成了QWT的安装,这篇文章我们讲讲基础曲线的绘制功能。     首先,我们新建一个Qt应用程序,然后一路默认即可。
     在 《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、右上角的图例可以点击,并显示或隐藏对应曲线


目录
相关文章
|
1月前
|
存储 网络协议 C语言
【C/C++ 串口编程 】深入探讨C/C++与Qt串口编程中的粘包现象及其解决策略
【C/C++ 串口编程 】深入探讨C/C++与Qt串口编程中的粘包现象及其解决策略
79 0
|
1月前
|
Linux 数据处理 C++
Linux系统编程 C/C++ 以及Qt 中的零拷贝技术: 从底层原理到高级应用(一)
Linux系统编程 C/C++ 以及Qt 中的零拷贝技术: 从底层原理到高级应用
77 0
|
1月前
|
Linux API C语言
Qt串口编程探究:理论与实践
Qt串口编程探究:理论与实践
64 1
|
1月前
|
存储
QT图形视图框架绘制曲线图和Smith图
QT图形视图框架绘制曲线图和Smith图
18 0
|
1月前
|
存储 Linux API
Linux系统编程 C/C++ 以及Qt 中的零拷贝技术: 从底层原理到高级应用(三)
Linux系统编程 C/C++ 以及Qt 中的零拷贝技术: 从底层原理到高级应用
31 1
|
1月前
|
消息中间件 Linux 数据处理
Linux系统编程 C/C++ 以及Qt 中的零拷贝技术: 从底层原理到高级应用(二)
Linux系统编程 C/C++ 以及Qt 中的零拷贝技术: 从底层原理到高级应用
32 1
|
1月前
|
存储 并行计算 安全
【Qt 线程】探索Qt线程编程的奥秘:多角度深入剖析(二)
【Qt 线程】探索Qt线程编程的奥秘:多角度深入剖析
61 0
|
5月前
关于Qt的pri模块化编程详解
今天在移植一份代码的时候遇到了了Qt的`pri`文件,在CSDN上看了一下怎么用,都告诉我新建文件夹,直接Ctrl+S的,试了半天不行,看了一下需要移植的代码,茅塞顿开,分享给大家详细过程。
|
1月前
|
负载均衡 并行计算 安全
【Qt 线程】探索Qt线程编程的奥秘:多角度深入剖析(三)
【Qt 线程】探索Qt线程编程的奥秘:多角度深入剖析
45 0
|
1月前
|
安全 数据处理 API
【Qt 线程】探索Qt线程编程的奥秘:多角度深入剖析(一)
【Qt 线程】探索Qt线程编程的奥秘:多角度深入剖析
83 0

热门文章

最新文章

推荐镜像

更多