Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) QwtExample2

 

QwtQtQt CreatorLubuntuUbuntuWindows

 

Qwt example 2: QwtPlot as QGraphicsItem is a Qwt example that shows how to add a QwtPlot to a QGraphicsScene.

 

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./CppQwtExample2/CppQwtExample2.pro

 

#Qwt does not go together with Qwt
include(../../DesktopApplicationNoWeffcpp.pri)
include(../../Libraries/Qwt.pri)

SOURCES += main.cpp \
    qtwidget.cpp \
    qwtplotitem.cpp \
    qwtplotwidget.cpp \
    qttextitem.cpp

HEADERS += \
    qtwidget.h \
    qwtplotitem.h \
    qwtplotwidget.h \
    qttextitem.h

 

 

 

 

 

./CppQwtExample2/main.cpp

 

#include <QApplication>
#include "qtwidget.h"
#include "qwtplotwidget.h"

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  QtWidget widget;
  widget.setGeometry(100,100,800,600);
  widget.show();
  return a.exec();
}

 

 

 

 

 

./CppQwtExample2/qttextitem.h

 

#ifndef QTTEXTITEM_H
#define QTTEXTITEM_H

#include <boost/signals2.hpp>
#include <QGraphicsSimpleTextItem>

struct QtTextItem : public QGraphicsSimpleTextItem
{
  QtTextItem(const QString& text, QGraphicsItem* parent = 0);

  boost::signals2::signal<void(double)> m_signal_value;

  void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
};

#endif // QTTEXTITEM_H

 

 

 

 

 

./CppQwtExample2/qttextitem.cpp

 

#include "qttextitem.h"

QtTextItem::QtTextItem(const QString& text, QGraphicsItem* parent)
  : QGraphicsSimpleTextItem(parent)
{
  this->setText(text);
  this->setFlags(
      QGraphicsItem::ItemIsFocusable
    | QGraphicsItem::ItemIsMovable
    | QGraphicsItem::ItemIsSelectable);
}

void QtTextItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
  QGraphicsSimpleTextItem::mouseMoveEvent(event);
  m_signal_value(this->pos().y());
}

 

 

 

 

 

./CppQwtExample2/qtwidget.h

 

#ifndef QTWIDGET_H
#define QTWIDGET_H

#include <QGraphicsView>

struct QwtPlotItem;

struct QtWidget : public QGraphicsView
{
  QtWidget(QGraphicsScene* scene = 0, QWidget* parent = 0);

  private:
  QwtPlotItem * m_item;

  void ReceiveValue(const double value);
};

#endif // QTWIDGET_H

 

 

 

 

 

./CppQwtExample2/qtwidget.cpp

 

#include "qtwidget.h"

#include <cassert>
#include <iostream>

#include <boost/lambda/lambda.hpp>

#include <QGraphicsProxyWidget>
#include <QGraphicsScene>

#include "qwtplotitem.h"
#include "qttextitem.h"
#include "qwtplotwidget.h"

QtWidget::QtWidget(QGraphicsScene* scene, QWidget* parent)
  : QGraphicsView(scene,parent), m_item(0)
{
  this->setScene(new QGraphicsScene);
  {
    QwtPlotWidget * const widget = new QwtPlotWidget(100);
    QGraphicsProxyWidget * const proxy = this->scene()->addWidget(widget,Qt::Dialog);
    m_item = new QwtPlotItem(widget,proxy);
  }
  m_item->m_proxy->setGeometry(QRectF(-200.0,-200.0,400.0,400.0));
  m_item->m_widget->AddY(1.0);
  {
    QtTextItem * const item = new QtTextItem("MOVE ME");
    item->m_signal_value.connect(boost::bind(&QtWidget::ReceiveValue,this,boost::lambda::_1));
    this->scene()->addItem(item);
    item->setPos(300.0,0.0);
  }
}

void QtWidget::ReceiveValue(const double value)
{
  this->m_item->m_widget->AddY(value);
}

 

 

 

 

 

./CppQwtExample2/qwtplotitem.h

 

#ifndef QWTPLOTITEM_H
#define QWTPLOTITEM_H


#include <QGraphicsProxyWidget>

struct QwtPlotWidget;

///Has access to item and original widget
struct QwtPlotItem
{
  QwtPlotItem(
    QwtPlotWidget * const widget,
    QGraphicsProxyWidget * const proxy);

  QGraphicsProxyWidget * const m_proxy;
  QwtPlotWidget * const m_widget;
};


#endif // QWTPLOTITEM_H

 

 

 

 

 

./CppQwtExample2/qwtplotitem.cpp

 

#include "qwtplotitem.h"

QwtPlotItem::QwtPlotItem(
  QwtPlotWidget * const widget,
  QGraphicsProxyWidget * const proxy)
  : m_proxy(proxy), m_widget(widget)
{

}

 

 

 

 

 

./CppQwtExample2/qwtplotwidget.h

 

#ifndef QWTPLOTWIDGET_H
#define QWTPLOTWIDGET_H

struct QwtPlot;
struct QwtPlotCurve;

#include <vector>
#include <QWidget>

struct QwtPlotWidget : public QWidget
{
  QwtPlotWidget(const int sz);

  void AddY(const double y);
private:
  QwtPlotCurve * const m_curve;
  QwtPlot * const m_plot;
  const std::vector<double> m_xs;
  std::vector<double> m_ys;

  static const std::vector<double> CreateXs(const int n);
};

#endif // QWTPLOTWIDGET_H

 

 

 

 

 

./CppQwtExample2/qwtplotwidget.cpp

 

#include "qwtplotwidget.h"

#include <cassert>
#include <deque>
#include <iostream>

#include <QVBoxLayout>

#include <qwt_plot.h>
#include <qwt_plot_curve.h>
#include <qwt_plot_grid.h>
#include <qwt_plot_zoomer.h>

#if QWT_VERSION >= 0x060100 || !WIN32
#include "qwt_point_data.h"
#endif

QwtPlotWidget::QwtPlotWidget(const int sz)
  : m_curve(new QwtPlotCurve),
    m_plot(new QwtPlot),
    m_xs(CreateXs(sz))
{

  m_plot->setTitle("QwtPlotWidget");
  m_plot->setAxisTitle(QwtPlot::yLeft,"Y coordinat");
  m_plot->setAxisTitle(QwtPlot::xBottom,"Time");
  m_curve->attach(m_plot);
  //Add grid
  { QwtPlotGrid * const grid = new QwtPlotGrid; grid->setPen(QPen(QColor(196,196,196))); grid->attach(m_plot); }
  //Add zoomer
  //{ new QwtPlotZoomer(m_plot->canvas()); }
  this->setGeometry(-100.0,-100.0,200,200);
  //Add some data
  {
    for (int i=0; i!=sz; ++i)
    {
      const double y_val = 0.0;
      m_ys.push_back(y_val);
    }
    #if QWT_VERSION >= 0x060100 || !WIN32
    m_curve->setData(new QwtPointArrayData(&m_xs[0],&m_ys[0],m_ys.size()));
    #else
    m_curve->setData(&m_xs[0],&m_ys[0],m_ys.size());
    #endif
    //m_curve->setData(new QwtPointArrayData(&m_xs[0],&m_ys[0],m_xs.size()));
  }

  {
    QVBoxLayout * const layout = new QVBoxLayout;
    layout->addWidget(m_plot);
    assert(!this->layout());
    this->setLayout(layout);
  }
}

void QwtPlotWidget::AddY(const double y)
{
  //Pop first, append y
  std::deque<double> d(m_ys.begin(),m_ys.end());
  d.pop_front();
  d.push_back(y);
  m_ys = std::vector<double>(d.begin(),d.end());
  assert(m_xs.size() == m_ys.size());

  m_curve->setData(new QwtPointArrayData(&m_xs[0],&m_ys[0],m_xs.size()));
  m_plot->replot(); //No replot, no glory
}

const std::vector<double> QwtPlotWidget::CreateXs(const int n)
{
  std::vector<double> v(n);
  for (int i=0; i!=n; ++i) { v[i] = static_cast<double>(i); }
  return v;
}

 

 

 

 

 

./CppQwtExample2/crosscompiletowindows.sh

 

#!/bin/sh
#From http://richelbilderbeek.nl/CppQtCrosscompileToWindowsExample15.htm

echo "Cross compiling to Windows"

echo "1/2: Creating Windows makefile"
i686-pc-mingw32-qmake CppQwtExample2.pro

echo "2/2: making makefile"

make

echo "Done"

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml