Results 1 to 7 of 7

Thread: timer problem(timer does not work)

  1. #1
    Join Date
    Jan 2011
    Posts
    39
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default timer problem(timer does not work)

    hi

    form1.h
    Qt Code:
    1. #ifndef FORM1_H
    2. #define FORM1_H
    3.  
    4. #include <QString>
    5.  
    6. #include <QWidget>
    7.  
    8. #include <QTimer>
    9. #include <QTime>
    10.  
    11.  
    12. namespace Ui {
    13. class Form1;
    14. }
    15.  
    16. class Form1 : public QWidget
    17. {
    18. Q_OBJECT
    19.  
    20. public:
    21. explicit Form1(QWidget *parent = 0);
    22. ~Form1();
    23. int i;
    24.  
    25. private:
    26. Ui::Form1 *ui;
    27.  
    28.  
    29. private:
    30. QTime time;
    31. QTimer timer;
    32.  
    33. public slots:
    34. void startTime();
    35. void showTime();
    36. void stopTime();
    37.  
    38. };
    39. #endif // FORM1_H
    To copy to clipboard, switch view to plain text mode 

    form1.cpp
    Qt Code:
    1. #include "form1.h"
    2. #include "ui_form1.h"
    3.  
    4.  
    5. #include <QTimer>
    6. #include <QTime>
    7.  
    8.  
    9. Form1::Form1(QWidget *parent) :
    10. QWidget(parent),
    11. ui(new Ui::Form1)
    12. {
    13. ui->setupUi(this);
    14.  
    15. ui->lcdNumber->setNumDigits(8);
    16. QTime time = QTime();
    17. time.setHMS(0,0,0,0);
    18. QTimer *timer=new QTimer(this);
    19. connect(timer, SIGNAL(timeout()), this, SLOT(showTime()));
    20. i=0;
    21. QString text = time.toString("hh:mm:ss");
    22. ui->lcdNumber->display(text);
    23.  
    24.  
    25.  
    26. }
    27.  
    28. Form1::~Form1()
    29. {
    30. delete ui;
    31. }
    32.  
    33. void Form1::startTime()
    34. {
    35. timer.start();
    36.  
    37. }
    38.  
    39. void Form1::showTime()
    40. {
    41. QTime newtime = QTime();
    42. newtime.setHMS(0,0,0,0);
    43. i=i+1;
    44. newtime=time.addSecs(i);
    45.  
    46. QString text = newtime.toString("hh:mm:ss");
    47. ui->lcdNumber->display(text);
    48.  
    49.  
    50. }
    51.  
    52. void Form1::stopTime()
    53. {
    54. timer.stop();
    55. }
    To copy to clipboard, switch view to plain text mode 


    timer is not working.timer is not starting

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: timer problem(timer does not work)

    Well, you are initiate a timer in the c-tor which is not the one declared in the header file. Basic C++ failure. Read about how to initialize members correctly.

  3. #3
    Join Date
    Jan 2011
    Posts
    39
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: timer problem(timer does not work)

    i can't understand where is the problem.please help me.

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: timer problem(timer does not work)

    Qt Code:
    1. class Form1 : public QWidget
    2. {
    3. QTimer timer;
    4. }
    5.  
    6.  
    7. Form1::Form1(QWidget *parent) :
    8. QWidget(parent),
    9. ui(new Ui::Form1)
    10. {
    11. QTimer *timer=new QTimer(this);
    12. }
    To copy to clipboard, switch view to plain text mode 
    They are not the same! You have to initialize your member correctly. Read any book about C++.

  5. #5
    Join Date
    Jan 2011
    Posts
    39
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: timer problem(timer does not work)

    Now timer is working but cannot stop.
    Qt Code:
    1. #include "form1.h"
    2. #include "ui_form1.h"
    3.  
    4.  
    5. #include <QTimer>
    6. #include <QTime>
    7.  
    8.  
    9. Form1::Form1(QWidget *parent) :
    10. QWidget(parent),
    11. ui(new Ui::Form1)
    12. {
    13. ui->setupUi(this);
    14.  
    15.  
    16. time.setHMS(0,0,0,0);
    17. i=0;
    18. ui->lcdNumber->setNumDigits(8);
    19. QString text = time.toString("hh:mm:ss");
    20. ui->lcdNumber->display(text);
    21. }
    22.  
    23. Form1::~Form1()
    24. {
    25. delete ui;
    26. }
    27.  
    28. void Form1::startTime()
    29. {
    30. QTimer *timer=new QTimer(this);
    31. connect(timer, SIGNAL(timeout()), this, SLOT(showTime()));
    32. timer->start(1000);
    33. }
    34.  
    35. void Form1::showTime()
    36. {
    37. QTime newtime;
    38. i=i+1;
    39. newtime=time.addSecs(i);
    40. QString text = newtime.toString("hh:mm:ss");
    41. ui->lcdNumber->display(text);
    42.  
    43. }
    44.  
    45. void Form1::stopTime()
    46. {
    47. QTimer *timer=new QTimer(this);
    48. timer->stop();
    49. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: timer problem(timer does not work)

    Ehm, read any book about C++! You obviously have no idea what you are doing. With that poor knowledge of C++ you won't be able to do anything with Qt since it is C++ after all. So please first learn C++.

  7. The following user says thank you to Lykurg for this useful post:

    masuk (13th February 2011)

  8. #7
    Join Date
    Jan 2011
    Posts
    39
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: timer problem(timer does not work)

    now working...
    form1.cpp
    Qt Code:
    1. #include "form1.h"
    2. #include "ui_form1.h"
    3.  
    4. #include <QTimer>
    5. #include <QTime>
    6.  
    7.  
    8. Form1::Form1(QWidget *parent) :
    9. QWidget(parent),
    10. ui(new Ui::Form1)
    11. {
    12. ui->setupUi(this);
    13.  
    14.  
    15. time.setHMS(0,0,0,0);
    16. i=0;
    17. ui->lcdNumber->setNumDigits(8);
    18. QString text = time.toString("hh:mm:ss");
    19. ui->lcdNumber->display(text);
    20. }
    21.  
    22. Form1::~Form1()
    23. {
    24. delete ui;
    25. }
    26.  
    27. void Form1::startTime()
    28. {
    29. timer=new QTimer(this);
    30. connect(timer, SIGNAL(timeout()), this, SLOT(showTime()));
    31. timer->start(1000);
    32. }
    33.  
    34. void Form1::showTime()
    35. {
    36. QTime newtime;
    37. i=i+1;
    38. newtime=time.addSecs(i);
    39. QString text = newtime.toString("hh:mm:ss");
    40. ui->lcdNumber->display(text);
    41.  
    42. }
    43.  
    44. void Form1::stopTime()
    45. {
    46. timer->stop();
    47. }
    To copy to clipboard, switch view to plain text mode 

    form1.h
    Qt Code:
    1. #ifndef FORM1_H
    2. #define FORM1_H
    3.  
    4. #include <QString>
    5.  
    6. #include <QWidget>
    7.  
    8. #include <QTimer>
    9. #include <QTime>
    10.  
    11.  
    12. namespace Ui {
    13. class Form1;
    14. }
    15.  
    16. class Form1 : public QWidget
    17. {
    18. Q_OBJECT
    19.  
    20. public:
    21. explicit Form1(QWidget *parent = 0);
    22. ~Form1();
    23. int i;
    24.  
    25. private:
    26. Ui::Form1 *ui;
    27.  
    28.  
    29. private:
    30. QTime time;
    31.  
    32. public:
    33. QTimer *timer;
    34.  
    35. public slots:
    36. void startTime();
    37. void showTime();
    38. void stopTime();
    39.  
    40. };
    41. #endif // FORM1_H
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. How to add timer delay
    By qtUse in forum Newbie
    Replies: 1
    Last Post: 12th October 2010, 19:09
  2. implemet a timer
    By adamatic in forum Qt Programming
    Replies: 12
    Last Post: 17th February 2009, 08:31
  3. Thread, Timer and Socket. Comuication problem
    By ^NyAw^ in forum Qt Programming
    Replies: 6
    Last Post: 17th January 2008, 17:48
  4. Problem about timer in multithread programs
    By vql in forum General Programming
    Replies: 4
    Last Post: 17th October 2007, 16:00
  5. Timer not timing out?
    By Mariane in forum Newbie
    Replies: 5
    Last Post: 2nd March 2006, 22:30

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.