awpitt13
8th February 2012, 23:23
Hello all,
I'm running a data logger and I'm trying to implement a plotter that pops up when a button is pressed on my gui. I am aware of Qwt, but I don't need anything fancy and from what I've read it seems installing it is sort of a pain. Instead, I've been using a plotter class I found in a book I just bought, C++ GUI Programming with Qt 4. Right now the code is setup for plotting two horizontal lines, I'm just trying to get the code functional, I'll worry about getting it functional with my data logger later.
I am getting no errors, nothing seems to happen when I click the button
Here is some of the code;
plotter.h
#ifndef PLOTTER_H
#define PLOTTER_H
#include <QMap>
#include <QPixmap>
#include <QVector>
#include <QWidget>
class QToolButton;
class PlotSettings;
class Plotter : public QWidget
{
Q_OBJECT
public:
Plotter (QWidget *parent = 0);
void setPlotSettings(const PlotSettings &settings);
void setCurveData(int id, const QVector<QPointF> &data);
void clearCurve(int id);
QSize minimumSizeHint() const;
QSize sizeHint() const;
public slots:
void zoomIn();
void zoomOut();
protected:
void paintEvent(QPaintEvent *event);
void resizeEvent(QResizeEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void keyPressEvent(QKeyEvent *event);
void wheelEvent(QWheelEvent *event);
private:
void updateRubberBandRegion();
void refreshPixmap();
void drawGrid(QPainter *painter);
void drawCurves(QPainter *painter);
enum { Margin = 50 };
QToolButton *zoomInButton;
QToolButton *zoomOutButton;
QMap<int, QVector<QPointF> > curveMap;
QVector<PlotSettings> zoomStack;
int curZoom;
bool rubberBandIsShown;
QRect rubberBandRect;
QPixmap pixmap;
};
class PlotSettings
{
public:
PlotSettings();
void scroll(int dx, int dy);
void adjust();
double spanX() const { return maxX - minX; }
double spanY() const { return maxY - minY; }
double minX;
double maxX;
int numXTicks;
double minY;
double maxY;
int numYTicks;
private:
static void adjustAxis(double &min, double &max, int &numTicks);
};
#endif
#include "plotter.h"
plotSettings.cpp
PlotSettings::PlotSettings()
{
minX = 0.0;
maxX = 10.0;
numTicks = 5;
minY = 0.0;
maxY = 10.0;
numYTicks = 5;
}
//more code...
here is the implementation file, Plotter.cpp
#include "plotter.h"
#include <QtGui>
Plotter::Plotter(QWidget *parent)
: QWidget(parent)
{
setBackgroundRole(QPalette::Dark);
setAutoFillBackground(true);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
setFocusPolicy(Qt::StrongFocus);
rubberBandIsShown = false;
zoomInButton = new QToolButton(this);
zoomInButton->setIcon(QIcon(":/images/zoomin.png"));
zoomInButton->adjustSize();
connect(zoomInButton, SIGNAL(clicked()), this, SLOT(zoomIn()));
zoomOutButton = new QToolButton(this);
zoomOutButton->setIcon(QIcon(":/images/zoomout.png"));
zoomOutButton->adjustSize();
connect(zoomOutButton, SIGNAL(clicked()), this, SLOT(zoomOut()));
setPlotSettings(PlotSettings());
}
//all the functions follow...
Now in my main window I call it
//rest of program code
void mainWindow::on_pushButton_clicked()
{
PlotSettings settings;
settings.minX(0.0);
settings.maxX(100.0);
settings.minY(0.0);
settings.maxY(100.0);
int numPoints = 100;
ArrayList<QPointF> points0 = new ArrayList<QPointF>();
ArrayList<QPointF> points1 = new ArrayList<QPointF>();
for (int x = 0; x < numPoints; ++x) {
points0.add(new QPointF(x, Math.random() * 100));
points1.add(new QPointF(x, Math.random() * 100));
}
Plotter plotter;
plotter.setWindowTitle(plotter.tr("Jambi Plotter"));
plotter.setPlotSettings(settings);
plotter.setCurveData(0, points0);
plotter.setCurveData(1, points1);
plotter.show();
//rest of program code
}
Thank you in advance for any help you can offer
I'm running a data logger and I'm trying to implement a plotter that pops up when a button is pressed on my gui. I am aware of Qwt, but I don't need anything fancy and from what I've read it seems installing it is sort of a pain. Instead, I've been using a plotter class I found in a book I just bought, C++ GUI Programming with Qt 4. Right now the code is setup for plotting two horizontal lines, I'm just trying to get the code functional, I'll worry about getting it functional with my data logger later.
I am getting no errors, nothing seems to happen when I click the button
Here is some of the code;
plotter.h
#ifndef PLOTTER_H
#define PLOTTER_H
#include <QMap>
#include <QPixmap>
#include <QVector>
#include <QWidget>
class QToolButton;
class PlotSettings;
class Plotter : public QWidget
{
Q_OBJECT
public:
Plotter (QWidget *parent = 0);
void setPlotSettings(const PlotSettings &settings);
void setCurveData(int id, const QVector<QPointF> &data);
void clearCurve(int id);
QSize minimumSizeHint() const;
QSize sizeHint() const;
public slots:
void zoomIn();
void zoomOut();
protected:
void paintEvent(QPaintEvent *event);
void resizeEvent(QResizeEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void keyPressEvent(QKeyEvent *event);
void wheelEvent(QWheelEvent *event);
private:
void updateRubberBandRegion();
void refreshPixmap();
void drawGrid(QPainter *painter);
void drawCurves(QPainter *painter);
enum { Margin = 50 };
QToolButton *zoomInButton;
QToolButton *zoomOutButton;
QMap<int, QVector<QPointF> > curveMap;
QVector<PlotSettings> zoomStack;
int curZoom;
bool rubberBandIsShown;
QRect rubberBandRect;
QPixmap pixmap;
};
class PlotSettings
{
public:
PlotSettings();
void scroll(int dx, int dy);
void adjust();
double spanX() const { return maxX - minX; }
double spanY() const { return maxY - minY; }
double minX;
double maxX;
int numXTicks;
double minY;
double maxY;
int numYTicks;
private:
static void adjustAxis(double &min, double &max, int &numTicks);
};
#endif
#include "plotter.h"
plotSettings.cpp
PlotSettings::PlotSettings()
{
minX = 0.0;
maxX = 10.0;
numTicks = 5;
minY = 0.0;
maxY = 10.0;
numYTicks = 5;
}
//more code...
here is the implementation file, Plotter.cpp
#include "plotter.h"
#include <QtGui>
Plotter::Plotter(QWidget *parent)
: QWidget(parent)
{
setBackgroundRole(QPalette::Dark);
setAutoFillBackground(true);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
setFocusPolicy(Qt::StrongFocus);
rubberBandIsShown = false;
zoomInButton = new QToolButton(this);
zoomInButton->setIcon(QIcon(":/images/zoomin.png"));
zoomInButton->adjustSize();
connect(zoomInButton, SIGNAL(clicked()), this, SLOT(zoomIn()));
zoomOutButton = new QToolButton(this);
zoomOutButton->setIcon(QIcon(":/images/zoomout.png"));
zoomOutButton->adjustSize();
connect(zoomOutButton, SIGNAL(clicked()), this, SLOT(zoomOut()));
setPlotSettings(PlotSettings());
}
//all the functions follow...
Now in my main window I call it
//rest of program code
void mainWindow::on_pushButton_clicked()
{
PlotSettings settings;
settings.minX(0.0);
settings.maxX(100.0);
settings.minY(0.0);
settings.maxY(100.0);
int numPoints = 100;
ArrayList<QPointF> points0 = new ArrayList<QPointF>();
ArrayList<QPointF> points1 = new ArrayList<QPointF>();
for (int x = 0; x < numPoints; ++x) {
points0.add(new QPointF(x, Math.random() * 100));
points1.add(new QPointF(x, Math.random() * 100));
}
Plotter plotter;
plotter.setWindowTitle(plotter.tr("Jambi Plotter"));
plotter.setPlotSettings(settings);
plotter.setCurveData(0, points0);
plotter.setCurveData(1, points1);
plotter.show();
//rest of program code
}
Thank you in advance for any help you can offer