Results 1 to 10 of 10

Thread: static QTimer

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #8
    Join Date
    May 2013
    Posts
    10
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: static QTimer

    Quote Originally Posted by Santosh Reddy View Post
    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.

    Qt Code:
    1. //graph.h
    2. #ifndef GRAPH_H
    3. #define GRAPH_H
    4. #include <QTimer>
    5.  
    6. class Graph{
    7. public:
    8. Graph(/*stuff*/);
    9. static QTimer *timer;
    10.  
    11. protected:
    12. /*stuff*/
    13. };
    14.  
    15. #endif // GRAPH_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //graph.cpp
    2. #include "graph.h"
    3.  
    4. QTimer *Graph::timer = new QTimer(0);
    5.  
    6. Graph::Graph(/*stuff*/){
    7. /*stuff*/
    8. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //graphline.h (same goes for graphhistogram.h)
    2. #ifndef GRAPHLINE_H
    3. #define GRAPHLINE_H
    4. #include "graph.h"
    5. class GraphLine : public Graph{
    6. public:
    7. GraphLine(/*stuff*/);
    8. void updateGraph(int rand = 0);
    9.  
    10. private:
    11. int graphArray[100];
    12.  
    13. public slots:
    14. void MySlot();
    15. };
    16. #endif // GRAPHLINE_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //graphline.cpp
    2. #include "graphline.h"
    3.  
    4. GraphLine::GraphLine(/*stuff*/) : Graph(/*stuff*/){
    5. updateGraph(0);
    6. QObject::connect(timer, SIGNAL(timeout()), this, SLOT(MySlot()));
    7. timer->start(40);
    8. }
    9.  
    10. void GraphLine::updateGraphic(int rand){
    11. /*graphic draw process*/
    12. }
    13.  
    14. void GraphLine::MySlot(){
    15. updateGraph(rand() % 100); // now i'm just giving rand values instead of true data
    16. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by PauloF91; 7th June 2013 at 14:28.

Similar Threads

  1. Replies: 1
    Last Post: 25th October 2012, 19:47
  2. Replies: 15
    Last Post: 4th August 2012, 19:11
  3. Replies: 3
    Last Post: 1st June 2011, 15:32
  4. Replies: 3
    Last Post: 31st January 2010, 16:56
  5. Replies: 4
    Last Post: 14th February 2006, 21:35

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.