PDA

View Full Version : Some question in example



jhowliu
9th August 2013, 11:48
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 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

[plotter.cpp]

#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());
}

......


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 !

rockdemon
9th August 2013, 12:49
#include <QtGui> includes the things you're talking about in qt4.

anda_skoa
9th August 2013, 14:06
As rockdemon already said this is the working of the #include <QtGui>

To be a bit more precise, <QtGui> it a so-called module include, it is basically a header file that in turn includes all the headers for a certain Qt module.
In case of QtGui all headers of the the Qt GUI module, which in Qt4 means all widgets.

There is a similar module include for QtCore, QtNetwork and so on.

Those monster includes obviously come with a price, they expand to all class declarations of a module when the source file is run through the compiler, making it parse tons of classes that it then doesn't need.

So while the module includes are very convenient for small test programs or prototypes, it is usually considered better pratice in real world projects to include all used classes individually.

Cheers,
_

jhowliu
9th August 2013, 17:24
Thanks for your reply.

and thanks anda_skoa very much for your explanation.
I will do more practice, thanks.