
Originally Posted by
Santosh Reddy
Instead can you tell use what actually you have to do, and how you are doing it?
Im plotting some data in a few graphics I've made myself.
Each graph represents an object. And I have 2 kinds of graphs (therefore, 2 classes):
- line graphs (Class GraphLine);
- histograms (Class GraphHistogram).
And they both inherit from a base class Graph.
So I thought I could use a Timer, so each time it timed out, I could update all graphs with new data.
I had it working, but I was creating one timer per graph. That was not a problem until I realized I wanted to stop and re-start all timers at once each time I wanted (while the program was running).
That's when I thought of a static Timer.
//graph.h
#ifndef GRAPH_H
#define GRAPH_H
#include <QTimer>
class Graph{
public:
Graph(/*stuff*/);
protected:
/*stuff*/
};
#endif // GRAPH_H
//graph.h
#ifndef GRAPH_H
#define GRAPH_H
#include <QTimer>
class Graph{
public:
Graph(/*stuff*/);
static QTimer *timer;
protected:
/*stuff*/
};
#endif // GRAPH_H
To copy to clipboard, switch view to plain text mode
//graph.cpp
#include "graph.h"
Graph::Graph(/*stuff*/){
/*stuff*/
}
//graph.cpp
#include "graph.h"
QTimer *Graph::timer = new QTimer(0);
Graph::Graph(/*stuff*/){
/*stuff*/
}
To copy to clipboard, switch view to plain text mode
//graphline.h (same goes for graphhistogram.h)
#ifndef GRAPHLINE_H
#define GRAPHLINE_H
#include "graph.h"
class GraphLine : public Graph{
public:
GraphLine(/*stuff*/);
void updateGraph(int rand = 0);
private:
int graphArray[100];
public slots:
void MySlot();
};
#endif // GRAPHLINE_H
//graphline.h (same goes for graphhistogram.h)
#ifndef GRAPHLINE_H
#define GRAPHLINE_H
#include "graph.h"
class GraphLine : public Graph{
public:
GraphLine(/*stuff*/);
void updateGraph(int rand = 0);
private:
int graphArray[100];
public slots:
void MySlot();
};
#endif // GRAPHLINE_H
To copy to clipboard, switch view to plain text mode
//graphline.cpp
#include "graphline.h"
GraphLine::GraphLine(/*stuff*/) : Graph(/*stuff*/){
updateGraph(0);
QObject::connect(timer,
SIGNAL(timeout
()),
this,
SLOT(MySlot
()));
timer->start(40);
}
void GraphLine::updateGraphic(int rand){
/*graphic draw process*/
}
void GraphLine::MySlot(){
updateGraph(rand() % 100); // now i'm just giving rand values instead of true data
}
//graphline.cpp
#include "graphline.h"
GraphLine::GraphLine(/*stuff*/) : Graph(/*stuff*/){
updateGraph(0);
QObject::connect(timer, SIGNAL(timeout()), this, SLOT(MySlot()));
timer->start(40);
}
void GraphLine::updateGraphic(int rand){
/*graphic draw process*/
}
void GraphLine::MySlot(){
updateGraph(rand() % 100); // now i'm just giving rand values instead of true data
}
To copy to clipboard, switch view to plain text mode
Bookmarks