ページ

ラベル Qwt の投稿を表示しています。 すべての投稿を表示
ラベル Qwt の投稿を表示しています。 すべての投稿を表示

2014年4月8日火曜日

レイアウトの中で表示

ヴァーティカル レイアウトの中にグラフを表示する。


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qwt_plot.h>

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

   QwtPlot* qwtPlot = new QwtPlot();
   ui->verticalLayout->addWidget(qwtPlot);
}

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


2014年1月8日水曜日

目盛の調整

標準の場合



QwtScaleDiv を使って目盛を調整
majorTicks,mediumTicks,minorTicks の設定



QwtScaleDraw を使って目盛を調整
目盛の文字の色を変更

QwtScaleDraw を継承したクラスを作成して呼び出す。
setAxisScaleDraw( QwtPlot::xBottom, new MyQwtScaleDraw() );

MediumTick の表示



目盛りの非表示
plot->enableAxis(QwtPlot::xBottom, false);

目盛りの最大値の取得
plot->setAutoReplot(true);
plot->axisScaleDiv(QwtPlot::xBottom).upperBound();
plot->axisScaleDiv(QwtPlot::xBottom).interval().maxValue();


簡単なサインカーブ[0-360]



2013年11月29日金曜日

qwtテンプレート

Qt GUI アプリケーションでプロジェクトを開く

qwtTest002.pro
#-------------------------------------------------
#
# Project created by QtCreator 2013-11-29T11:23:42
#
#-------------------------------------------------

QT       += core gui
CONFIG   += qwt

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = qwtTest002
TEMPLATE = app


SOURCES += main.cpp\
        plot.cpp

HEADERS  += plot.h

FORMS    += plot.ui


plot.h
#ifndef PLOT_H
#define PLOT_H

#include <QMainWindow>
#include <qwt_plot.h>

namespace Ui {
class Plot;
}

class Plot : public QwtPlot
{
    Q_OBJECT
    
public:
    explicit Plot(QWidget *parent = 0);
    ~Plot();
    
private:
    Ui::Plot *ui;
};

#endif // PLOT_H

main.cpp
#include "plot.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Plot w;
    w.show();
    
    return a.exec();
}


plot.cpp
#include "plot.h"
#include "ui_plot.h"

Plot::Plot(QWidget *parent) :
    QwtPlot(parent)
{
    setTitle( "Plot Demo" );//グラフのタイトル

}

Plot::~Plot()
{
    //ウインドウ終了
}


examples フォルダにある。simpleplot を参考に書いてみる。
plot.cpp
#include "plot.h"
#include "ui_plot.h"
#include <qwt_plot.h>
#include <qwt_plot_curve.h>
#include <qwt_plot_grid.h>
#include <qwt_symbol.h>
#include <qwt_legend.h>

Plot::Plot(QWidget *parent) :
    QwtPlot(parent)
{
    setTitle( "Plot Demo" );//グラフのタイトル
    setCanvasBackground( Qt::white );
    setAxisScale( QwtPlot::yLeft, 0.0, 10.0 );
    insertLegend( new QwtLegend() );
    
    QwtPlotGrid *grid = new QwtPlotGrid();
    grid->attach( this );
    
    QwtPlotCurve *curve = new QwtPlotCurve();
    curve->setTitle( "Some Points" );
    curve->setPen( Qt::blue, 4 ),
            curve->setRenderHint( QwtPlotItem::RenderAntialiased, true );
    
    QwtSymbol *symbol = new QwtSymbol( QwtSymbol::Ellipse,
    QBrush( Qt::yellow ), QPen( Qt::red, 2 ), QSize( 8, 8 ) );
    curve->setSymbol( symbol );
    
    QPolygonF points;
    points << QPointF( 0.0, 4.4 ) << QPointF( 1.0, 3.0 )
           << QPointF( 2.0, 4.5 ) << QPointF( 3.0, 6.8 )
           << QPointF( 4.0, 7.9 ) << QPointF( 5.0, 7.1 );
    curve->setSamples( points );
    
    curve->attach( this );
    
    resize( 600, 400 );
    
}

Plot::~Plot()
{
    //ウインドウ終了
}


2013年11月28日木曜日

データの追記

タイマーを使って擬似的にリアルタイムなグラフの作成に挑戦

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>

QVector<double> xval;//空のベクタ宣言
QVector<double> yval;//空のベクタ宣言
double xyData = 1;

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

    xval.append(0);
    xval.append(1);

    yval.append(0);
    yval.append(1);

    setTitle( "Plot Demo" );//グラフのタイトル
    setTitle("first_plot");
    setAxisTitle(QwtPlot::xBottom, " time [Second]");
    setAxisScale(QwtPlot::xBottom, 0,100 );
    setAxisTitle(QwtPlot::yLeft, "Value");
    setAxisScale(QwtPlot::yLeft, 0,100 );
    setAutoReplot(true);  // データの追記で必要
    
    curve = new QwtPlotCurve();
    curve->setPen(QPen(Qt::blue));
    curve->setSamples(xval.data(),yval.data(),xval.count());
    curve->attach(this);

    resize( 600, 400 );

    // タイマー
    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(update()));
    timer->start(1000);
}

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

void MainWindow::update()
{
    qDebug() << "hello" << xyData;

    xyData += 1;

    xval.append(xyData);
    yval.append(xyData);

    curve->setSamples(xval.data(), yval.data(), xyData+1 );
}



サインカーブでやってみる。
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>

QVector<double> xval;//空のベクタ宣言
QVector<double> yval;//空のベクタ宣言

int kArraySize = 1000;
//double x[kArraySize] = {}; // x
//double y[kArraySize] = {}; // y
double xData = 0;

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

    setTitle( "Plot Demo" );//グラフのタイトル
    setTitle("first_plot");
    setAxisTitle(QwtPlot::xBottom, " time [Second]");
    setAxisScale(QwtPlot::xBottom, 0,1 );
    setAxisTitle(QwtPlot::yLeft, "Value");
    setAxisScale(QwtPlot::yLeft, -1,1 );
    setAutoReplot(true);  // データの追記で必要

    curve = new QwtPlotCurve();
    curve->setPen(QPen(Qt::blue));
    //curve->setSamples(xval.data(),yval.data(),xval.count());
    curve->attach(this);

    resize( 600, 400 );

    // タイマー
    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(update()));
    timer->start(5);
}

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

void MainWindow::update()
{
    qDebug() << "hello" << xData;

    if( xData < 1000 ){
        double x = xData/(1000-1.0);
        xval.append( x );
        yval.append( sin(2.0*M_PI*x) );

        curve->setSamples(xval.data(), yval.data(), xData);
        xData += 1;
    }else{
        timer->stop();
    }

}


x軸の移動を考えてみる
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>
#include <qwt_plot_magnifier.h>

QVector<double> xval;//空のベクタ宣言
QVector<double> yval;//空のベクタ宣言

double xData = 0;

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

    setTitle( "Plot Demo" );//グラフのタイトル
    setTitle("first_plot");
    setAxisTitle(QwtPlot::xBottom, " time [Second]");
    setAxisScale(QwtPlot::xBottom, 0,1 );   // x軸
    setAxisTitle(QwtPlot::yLeft, "Value");
    setAxisScale(QwtPlot::yLeft, -1,1 );
    setAutoReplot(true);  // データの追記で必要

    curve = new QwtPlotCurve();
    curve->setPen(QPen(Qt::blue));
    curve->attach(this);

    QwtPlotMagnifier* magnifier = new QwtPlotMagnifier( canvas());// 拡大縮小
    magnifier->setMouseButton(Qt::LeftButton);

    resize( 600, 400 );

    // タイマー
    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(update()));
    timer->start(30);
}

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

void MainWindow::update()
{
        double x = xData/(100-1.0);
        xval.append( x );
        yval.append( sin(2.0*M_PI*x) );

        curve->setSamples(xval.data(), yval.data(), xval.count());

        // x軸を移動
        if( xval.last() > 1){
            setAxisScale(QwtPlot::xBottom,  xval.last()-1 ,xval.last() );
        }

        xData += 1;
}


もっと便利な方法があるかもしれません。


配列のデータでグラフを描く


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

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

    double xval[5]={10,20,30,40,50};
    double yval[5]={1,2.2,3,4,5};

    setTitle( "Plot Demo" );//グラフのタイトル
    setTitle("first_plot");
    setAxisTitle(QwtPlot::xBottom, " System time [h:m:s]");
    setAxisScale(QwtPlot::xBottom, 0,60 );
    setAxisTitle(QwtPlot::yLeft, "Degree");
    setAxisScale(QwtPlot::yLeft, -60,60 );

    curve = new QwtPlotCurve();
    curve->setPen(QPen(Qt::blue));
    curve->setSamples(xval,yval,5);
    curve->attach(this);

    resize( 600, 400 );
}

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



グラフの拡大・縮小

#include <qapplication.h>
#include <qwt_plot.h>
#include <qwt_plot_curve.h>
#include <qwt_plot_grid.h>
#include <qwt_symbol.h>
#include <qwt_legend.h>
#include <qwt_plot_magnifier.h>

int main(int argc, char *argv[])
{
    QApplication a( argc, argv );

       QwtPlot plot;
       plot.setTitle( "Plot Demo" );
       plot.setCanvasBackground( Qt::white );
       plot.setAxisScale( QwtPlot::yLeft, 0.0, 10.0 );
       plot.insertLegend( new QwtLegend() );

       QwtPlotGrid *grid = new QwtPlotGrid();
       grid->attach( &plot );

       QwtPlotCurve *curve = new QwtPlotCurve();
       curve->setTitle( "Some Points" );
       curve->setPen( Qt::blue, 4 ),
       curve->setRenderHint( QwtPlotItem::RenderAntialiased, true );

       QwtSymbol *symbol = new QwtSymbol( QwtSymbol::Ellipse,
           QBrush( Qt::yellow ), QPen( Qt::red, 2 ), QSize( 8, 8 ) );
       curve->setSymbol( symbol );

       QPolygonF points;
       points << QPointF( 0.0, 4.4 ) << QPointF( 1.0, 3.0 )
           << QPointF( 2.0, 4.5 ) << QPointF( 3.0, 6.8 )
           << QPointF( 4.0, 7.9 ) << QPointF( 5.0, 7.1 );
       curve->setSamples( points );
       curve->attach( &plot );

       QwtPlotMagnifier* magnifier = new QwtPlotMagnifier( plot.canvas());
       magnifier->setMouseButton(Qt::LeftButton);

       plot.resize( 600, 400 );
       plot.show();

       return a.exec();
}

サンプル画像一覧








2013年11月23日土曜日

Qwtのインストール・メモ

Qt のグラフライブラリ インストールに苦労したのでメモ

使用したもの
windows 8 64bit
Qt 5.1.1 for Windows 32-bit (MinGW 4.8, OpenGL, 666 MB)
qwt-6.1.0.zip

1.Qt をインストールしてシステム環境変数を登録
C:\Qt\Qt5.1.1\5.1.1\mingw48_32\bin
C:\Qt\Qt5.1.1\Tools\mingw48_32\bin

2.qwt-6.1.0.zipを解凍する
コマンドプロンプトを開く。
qwt-6.1.0.zipを解凍したフォルダ(C:\temp\qwt-6.1.0)で次のコマンドを打つ。
qmake qwt.pro
mingw32-make
mingw32-make install

成功すると
C:\Qwt-6.1.0 が出来上がる。

3.システム環境変数を登録
C:\Qwt-6.1.0\lib

ユーザー環境変数を登録する。
変数:QT_PLUGIN_PATH  値:C:\Qwt-6.1.0\plugins;
変数:QMAKEFEATURES  値:C:\Qwt-6.1.0\features;


環境変数を反映させるために再起動させる。
これで完了。

参考にしたサイト:HowTo: Installation of Qt 5.0.1 and Qwt 6.1.0 rc3 (Win7 64bit)


以下苦労した点 
mingw32-make の時エラーが出た。

collect2.exe: error: ld returned 1 exit status
Makefile.Release:320: recipe for target '../lib/qwt.dll' fa
mingw32-make[2]: *** [../lib/qwt.dll] Error 1
mingw32-make[2]: Leaving directory 'D:/qwt-6.1.0/src'
Makefile:38: recipe for target 'release-all' failed
mingw32-make[1]: *** [release-all] Error 2
mingw32-make[1]: Leaving directory 'D:/qwt-6.1.0/src'
makefile:41: recipe for target 'sub-src-make_first-ordered'
mingw32-make: *** [sub-src-make_first-ordered] Error 2

たぶん以前に利用していたMinGW を使っていたからだと思う

備考:
>qmake -set QMAKEFEATURES C:\Qwt-6.0.1\features と解説している所もあった。

> qmake -unset QMAKEFEATURES
> set QMAKEFEATURES=C:\Qwt-6.1.0\features
> qmake  
>



>mingw32-make install の時に
mingw32-make: *** No rule to make target 'inatall'.  Stop. のエラーが出た時は再起動

qwt widget は使えない。プラグインが読み込めていないみたいです。

QCustomPlot というライブラリもあるようです。