Hi everyone,
I have a question in "c++ gui programming with qt 4" chapter 5-4 example(Plotter):
[plotter.h]
#ifndef PLOTTER_H
#define PLOTTER_H
//QMap store (key, value) pairs and provides fast lookup of the value associated with a key.
#include <QMap>
#include <QPixmap>
#include <QVector>
#include <QWidget>
class PlotSettings;
Q_OBJECT
public:
void setPlotSettings(const PlotSettings &settings);
void setCurveData(int id, const QVector<QPointF> &data);
void clearCurve(int id);
QSize minimumSizeHint
() const;
protected:
void KeyPressEvent(QkeyEvent *event);
private:
void updateRubberBandRegion();
void refreshPixmap();
enum { Margin = 50 };
QMap<int, QVector<QPointF> > curveMap;
QVector<PlotSettings> zoomStack;
int curZoom;
bool rubberBandIsShown;
};
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 maxX;
double minX;
int numXTicks;
double maxY;
double minY;
int numYTicks;
private:
static void adjustAxis(double &min, double &max, int &numTicks);
};
#endif
#ifndef PLOTTER_H
#define PLOTTER_H
//QMap store (key, value) pairs and provides fast lookup of the value associated with a key.
#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;
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 maxX;
double minX;
int numXTicks;
double maxY;
double minY;
int numYTicks;
private:
static void adjustAxis(double &min, double &max, int &numTicks);
};
#endif
To copy to clipboard, switch view to plain text mode
[plotter.cpp]
#include <QtGui>
#include <cmath>
#include "plotter.h"
setAutoFillBackground(true);
setFocusPolicy(Qt::StrongFocus);
rubberBandIsShown = false;
zoomInButton
->setIcon
(QIcon(":/images/zoomIn.png"));
zoomInButton->adjustSize();
connect(zoomInButton, SIGNAL(clicked()), this, SLOT(zoomIn());
zoomOutButton
->setIcon
(QIcon(":/images/zoomOut.png"));
zoomOutButton->adjustSize();
connect(zoomOutButton, SIGNAL(clicked), this, SLOT(zoomOut));
setPlotSettings(PlotSettings());
}
......
#include <QtGui>
#include <cmath>
#include "plotter.h"
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());
}
......
To copy to clipboard, switch view to plain text mode
and i have question in plotter.cpp,
i never include QToolButton class,
why i can use its constructor and function.
Can someone tell me how to solve the problem?
thanks !
Bookmarks