Results 1 to 10 of 10

Thread: static QTimer

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

    Default static QTimer

    I want to get a single timer to run for several objects.
    If I put this in the header file of my class:
    static QTimer* timer = new QTimer(this);

    It says: « 'this' cannot appear in a constant-expression ». I understand i cannot use 'this', since I want a class member, not an object member (I guess that makes sense), but I don't know how to solve it. How do I put it then?

    Thanks in advance.

    PF91

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: static QTimer

    Here is a static class member example
    Qt Code:
    1. //a.h
    2. Class A
    3. {
    4. private:
    5. static QTimer timer1;
    6. static QTimer *timer2;
    7. }
    8.  
    9. //a.cpp
    10. #include "a.h"
    11. QTimer A::timer1;
    12. QTimer * A::timer2 = new QTimer(0); //EDIT: Added *
    To copy to clipboard, switch view to plain text mode 
    Last edited by Santosh Reddy; 6th June 2013 at 21:56.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    May 2013
    Posts
    10
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: static QTimer

    Lets say I have class B that inherits from class A:
    Qt Code:
    1. //a.h
    2. Class A
    3. {
    4. public:
    5. static QTimer timer1;
    6. static QTimer *timer2;
    7. }
    8.  
    9. //b.h
    10. Class B : public A
    11. {
    12. }
    13.  
    14. //b.cpp
    15. #include "b.h"
    16. B::B(){
    17. QTimer A::timer1;
    18. QTimer A::timer2 = new QTimer(0);
    19. }
    To copy to clipboard, switch view to plain text mode 

    And I'm getting this error:
    invalid use of qualified-name 'A::timer1'
    invalid use of qualified-name 'A::timer2'

    Where is this wrong?

    PS: I don't think I'm getting this straight. If I'm gonna have a single (static) timer, I guess I should only start it once. In this code, that will be done each time I instantiate an object from class B, I guess that's not correct.
    All I wanna do is a QObject::connect function from my timer1's timeout() SLOT to a SIGNAL, and that signal is different from object to object (since I have others classes that inherit from class A, besides class B) that's why I'm running it in B's (or other subclass) constructor. I guess that's doable.
    Last edited by PauloF91; 6th June 2013 at 18:25.

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: static QTimer

    Oh yes you got it wrong.

    If I understand your problem, you have a base class say A, and there are different derived classes from it each wanting to connect to a common signal from base object but do derived class specific slot.

    Will this example work?
    Qt Code:
    1. //a.h
    2. Class A
    3. {
    4. public:
    5. static QTimer *timer;
    6. }
    7.  
    8. //a.cpp
    9. #include "a.h"
    10. QTimer * A::timer = new QTimer(0);
    11.  
    12. //b.h
    13. Class B : public A
    14. {
    15. }
    16.  
    17. //b.cpp
    18. #include "b.h"
    19. B::B() : A() {
    20. QObject::connect(timer, SIGNAL(timeout()), object, SLOT(object_slot()));
    21. }
    To copy to clipboard, switch view to plain text mode 

    Anyway this is just a basic C++ problem, nothing to do with Qt, you better read more about C++ static class members.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. #5
    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
    If I understand your problem, you have a base class say A, and there are different derived classes from it each wanting to connect to a common signal from base object but do derived class specific slot.
    Yes that's it.
    Ye.. this is my very first project in C++ or anything related to OOP, I should read some stuff about this, but I gotta get this done asap.

    Just a couple things I still don't get.
    What do you mean by "object" in:
    Qt Code:
    1. QObject::connect(timer, SIGNAL(timeout()), object, SLOT(object_slot()));
    To copy to clipboard, switch view to plain text mode 
    Should I put '0' or 'this'? Cause neither or them will work. I got this (obvious) error:

    QObject::connect: Cannot connect QTimer::timeout() to (null)::MySlot()

    Sorry if this is too obvious, I wouldn't ask if I didn't really had to.
    Thanks in advance.
    Last edited by PauloF91; 7th June 2013 at 01:11.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: static QTimer

    I would recommend against using a static timer.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: static QTimer

    I am sure even if showed you how to use QTimer or SIGNAL/SLOTS in general that will still not solve you problem. Instead can you tell use what actually you have to do, and how you are doing it?
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  8. #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.

  9. #9
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: static QTimer

    See if this example helps
    Qt Code:
    1. #include <QtGui>
    2. #include <QtWidgets>
    3. #include <QApplication>
    4.  
    5. class Graph : public QWidget
    6. {
    7. Q_OBJECT
    8. public:
    9. explicit Graph(QWidget * parent = 0)
    10. : QWidget(parent)
    11. , mGridLayout(new QGridLayout(this))
    12. , mLabel(new QLabel("Graph"))
    13. , mCount(0)
    14. {
    15.  
    16. if(mTimer.interval() != mInterval)
    17. mTimer.setInterval(mInterval);
    18.  
    19. QPushButton * startButton = new QPushButton("Start Update", this);
    20. QPushButton * stopButton = new QPushButton("Stop Update", this);
    21.  
    22. connect(startButton, SIGNAL(clicked()), this, SLOT(startUpdateTimer()));
    23. connect(stopButton, SIGNAL(clicked()), this, SLOT(stopUpdateTimer()));
    24. connect(&mTimer, SIGNAL(timeout()), this, SLOT(updateGraph()));
    25.  
    26. mGridLayout->addWidget(mLabel, 0, 0, 1, 2);
    27. mGridLayout->addWidget(startButton, 1, 0, 1, 1);
    28. mGridLayout->addWidget(stopButton, 1, 1, 1, 1);
    29. }
    30.  
    31. public slots:
    32. void startUpdateTimer(int interval = mInterval)
    33. {
    34. if(mTimer.interval() != interval)
    35. mTimer.setInterval(interval);
    36. mCount = 0;
    37. mTimer.start();
    38. }
    39.  
    40. void stopUpdateTimer(void)
    41. {
    42. mTimer.stop();
    43. }
    44.  
    45. virtual void updateGraph(void) = 0;
    46.  
    47. protected:
    48. QGridLayout * mGridLayout;
    49. QLabel * mLabel;
    50. int mCount;
    51.  
    52. private:
    53. static QTimer mTimer;
    54. static const int mInterval = 1000;
    55. };
    56.  
    57. QTimer Graph::mTimer;
    58.  
    59. class GraphLine : public Graph
    60. {
    61. Q_OBJECT
    62. public:
    63. explicit GraphLine(QWidget * parent = 0)
    64. : Graph(parent)
    65. {
    66. setWindowTitle("GraphLine");
    67. }
    68.  
    69. public slots:
    70. virtual void updateGraph(void)
    71. {
    72. QString str = QString("GraphLine::updateGraph(): %1").arg(mCount++);
    73. qDebug() << str;
    74. mLabel->setText(str);
    75. }
    76. };
    77.  
    78. class GraphHistogram : public Graph
    79. {
    80. Q_OBJECT
    81. public:
    82. explicit GraphHistogram(QWidget * parent = 0)
    83. : Graph(parent)
    84. {
    85. setWindowTitle("GraphHistogram");
    86. }
    87.  
    88. public slots:
    89. virtual void updateGraph(void)
    90. {
    91. QString str = QString("GraphHistogram::updateGraph(): %1").arg(mCount++);
    92. qDebug() << str;
    93. mLabel->setText(str);
    94. }
    95. };
    96.  
    97. int main(int argc, char *argv[])
    98. {
    99. QApplication app(argc, argv);
    100.  
    101. GraphLine g1;
    102. g1.show();
    103.  
    104. GraphHistogram g2;
    105. g2.show();
    106.  
    107. return app.exec();
    108. }
    109.  
    110. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  10. The following user says thank you to Santosh Reddy for this useful post:

    PauloF91 (8th June 2013)

  11. #10
    Join Date
    May 2013
    Posts
    10
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: static QTimer

    Looking at your example did help me! I got it working... a single timer to run all my graphs.
    I had to redo my base class to inherits from QWidget, and now my QObject::connect function is working just fine.

    Thanks!

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.