Results 1 to 13 of 13

Thread: A Stopwatch in qt GUI ?

Hybrid View

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

    Default Re: A Stopwatch in qt GUI ?

    I am able to show the stopwatch thanks to your help. Anyway , I realised that the void Stop(), really stop the whole timing and will be reset after i click again . is there anyway to like just resume .
    Yes, you need to maintain a session time variable and keep adding the time to it so that the sum is retainted over the start-pause sessions

    Is the anyway to beautify the label of the stop watch
    Yes, you could use style sheets or just simply set the desired font using setFont()

    Now when it is running , it is like 0.0.5.123, can i make it 00.00.05,123?
    Yes, for that you will need to learn VUDOO . Change QString("%1:%2:%3,%4")...
    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.

  2. #2
    Join Date
    May 2013
    Posts
    47
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: A Stopwatch in qt GUI ?

    [QUOTE=Santosh Reddy;247633]Yes, you need to maintain a session time variable and keep adding the time to it so that the sum is retainted over the start-pause sessions


    Can You teach me a method how ? What i can think of is jus makiing a
    QString a=ui->label->text();
    But i dont it will work as I will have to Resume it to start counting . Anyway to do it ?

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

    Default Re: A Stopwatch in qt GUI ?

    Qt Code:
    1. class StopWatch : public QWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit StopWatch(QWidget * parent = 0)
    6. : QWidget(parent)
    7. , mRunning(false)
    8. , mStartTime()
    9. , mLabel(new QLabel("00:00:00,000"))
    10. , mTotalTime(0)
    11. , mStart(new QPushButton("Start"))
    12. , mPause(new QPushButton("Pause"))
    13. , mStop(new QPushButton("Stop"))
    14. {
    15. QGridLayout * gridLayout = new QGridLayout(this);
    16.  
    17. gridLayout->addWidget(mLabel, 0, 0, 1, 3);
    18. gridLayout->addWidget(mStart, 1, 0, 1, 1);
    19. gridLayout->addWidget(mPause, 1, 1, 1, 1);
    20. gridLayout->addWidget(mStop, 1, 2, 1, 1);
    21.  
    22. connect(mStart, SIGNAL(clicked()), SLOT(start()));
    23. connect(mPause, SIGNAL(clicked()), SLOT(pause()));
    24. connect(mStop, SIGNAL(clicked()), SLOT(stop()));
    25.  
    26. QFont font("Arial", 24, QFont::Bold);
    27. QPalette palette = mLabel->palette();
    28. palette.setColor(QPalette::WindowText, Qt::blue);
    29. mLabel->setPalette(palette);
    30. mLabel->setFont(font);
    31. gridLayout->setAlignment(mLabel, Qt::AlignCenter);
    32.  
    33. mStart->setEnabled(true);
    34. mPause->setEnabled(false);
    35. mStop->setEnabled(false);
    36.  
    37. startTimer(0);
    38. }
    39.  
    40. public slots:
    41. void start(void)
    42. {
    43. mStartTime = QDateTime::currentDateTime();
    44. mRunning = true;
    45. mStart->setEnabled(false);
    46. mPause->setEnabled(true);
    47. mStop->setEnabled(true);
    48. }
    49.  
    50. void pause(void)
    51. {
    52. mStart->setEnabled(true);
    53. mPause->setEnabled(false);
    54. mStop->setEnabled(true);
    55. timerEvent(new QTimerEvent(0));
    56. mTotalTime += mSessionTime;
    57. mRunning = false;
    58. }
    59.  
    60. void stop(void)
    61. {
    62. mStart->setEnabled(true);
    63. mPause->setEnabled(false);
    64. mStop->setEnabled(false);
    65. mTotalTime = 0;
    66. mRunning = false;
    67. }
    68.  
    69. protected:
    70. void timerEvent(QTimerEvent *)
    71. {
    72. if(mRunning)
    73. {
    74. mSessionTime = mStartTime.msecsTo(QDateTime::currentDateTime());
    75. qint64 time = mTotalTime + mSessionTime;
    76. unsigned int h = time / 1000 / 60 / 60;
    77. unsigned int m = (time / 1000 / 60) - (h * 60);
    78. unsigned int s = (time / 1000) - (m * 60);
    79. unsigned int ms = time - (s + ((m + (h * 60))* 60)) * 1000;
    80. const QString diff = QString("%1:%2:%3,%4")
    81. .arg(h, 2, 10, QChar('0'))
    82. .arg(m, 2, 10, QChar('0'))
    83. .arg(s, 2, 10, QChar('0'))
    84. .arg(ms, 3, 10, QChar('0'));
    85. mLabel->setText(diff);
    86. }
    87. }
    88.  
    89. private:
    90. bool mRunning;
    91. QDateTime mStartTime;
    92. QLabel * mLabel;
    93. qint64 mTotalTime;
    94. qint64 mSessionTime;
    95.  
    96. QPushButton * mStart;
    97. QPushButton * mPause;
    98. QPushButton * mStop;
    99. };
    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.

  4. #4
    Join Date
    May 2013
    Posts
    47
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: A Stopwatch in qt GUI ?

    Thanks again . And thanks for helping me with the "VUDOO" which u mention just now . From your codes
    .arg(ms,3,10,QChar('0'));
    I know the 3 is for the number of integer in the text . But i am unsure of the 10.Can you explain why we need to include that ?

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

    Default Re: A Stopwatch in qt GUI ?

    Qt Code:
    1. QString QString::arg(uint a, int fieldWidth = 0, int base = 10, QChar fillChar = QLatin1Char( ' ' )) const
    To copy to clipboard, switch view to plain text mode 
    The a argument is expressed in base base, which is 10 by default and must be between 2 and 36. For bases other than 10, a is treated as an unsigned integer.

    fieldWidth specifies the minimum amount of space that a is padded to and filled with the character fillChar. A positive value produces right-aligned text; a negative value produces left-aligned text.

    If fillChar is '0' (the number 0, ASCII 48), the locale's zero is used. For negative numbers, zero padding might appear before the minus sign.

    also there is small problem in the earlier code, here is the correction
    Qt Code:
    1. void timerEvent(QTimerEvent *)
    2. {
    3. if(mRunning)
    4. {
    5. mSessionTime = mStartTime.msecsTo(QDateTime::currentDateTime());
    6. qint64 time = mTotalTime + mSessionTime;
    7. time *= 111;
    8. unsigned int h = time / 1000 / 60 / 60;
    9. unsigned int m = (time / 1000 / 60) - (h * 60);
    10. unsigned int s = (time / 1000) - ((m + (h * 60))* 60); //<<<<<<<<<<<<<<<<<<<<<<
    11. unsigned int ms = time - (s + ((m + (h * 60))* 60)) * 1000;
    12. const QString diff = QString("%1:%2:%3,%4")
    13. .arg(h, 2, 10, QChar('0'))
    14. .arg(m, 2, 10, QChar('0'))
    15. .arg(s, 2, 10, QChar('0'))
    16. .arg(ms, 3, 10, QChar('0'));
    17. mLabel->setText(diff);
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Santosh Reddy; 15th July 2013 at 15:40.
    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.

  6. #6
    Join Date
    May 2013
    Posts
    47
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: A Stopwatch in qt GUI ?

    Hey Sorry to make this thread alive again , I recently encountered a problem with the stopwatch . I let it run for a while just to make sure no errors occurs . The stopwatch is working perfectly fine but there seem to be an error after an Hour . The StopWatch became like this 1:39:1234. The sec hands shld be a 2 digit number but instead it becomes a 4 digit number . It is kinda wierd though as the Stopwatch is working perfectly fine until that happens

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

    Default Re: A Stopwatch in qt GUI ?

    Did you take the corrected code from my previous post.

    Anyway here it is again
    Qt Code:
    1. class StopWatch : public QWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit StopWatch(QWidget * parent = 0)
    6. : QWidget(parent)
    7. , mRunning(false)
    8. , mStartTime()
    9. , mLabel(new QLabel("00:00:00,000"))
    10. , mTotalTime(0)
    11. , mStart(new QPushButton("Start"))
    12. , mPause(new QPushButton("Pause"))
    13. , mStop(new QPushButton("Stop"))
    14. {
    15. QGridLayout * gridLayout = new QGridLayout(this);
    16.  
    17. gridLayout->addWidget(mLabel, 0, 0, 1, 3);
    18. gridLayout->addWidget(mStart, 1, 0, 1, 1);
    19. gridLayout->addWidget(mPause, 1, 1, 1, 1);
    20. gridLayout->addWidget(mStop, 1, 2, 1, 1);
    21.  
    22. connect(mStart, SIGNAL(clicked()), SLOT(start()));
    23. connect(mPause, SIGNAL(clicked()), SLOT(pause()));
    24. connect(mStop, SIGNAL(clicked()), SLOT(stop()));
    25.  
    26. QFont font("Arial", 24, QFont::Bold);
    27. QPalette palette = mLabel->palette();
    28. palette.setColor(QPalette::WindowText, Qt::blue);
    29. mLabel->setPalette(palette);
    30. mLabel->setFont(font);
    31. gridLayout->setAlignment(mLabel, Qt::AlignCenter);
    32.  
    33. mStart->setEnabled(true);
    34. mPause->setEnabled(false);
    35. mStop->setEnabled(false);
    36.  
    37. startTimer(0);
    38. }
    39.  
    40. public slots:
    41. void start(void)
    42. {
    43. mStartTime = QDateTime::currentDateTime();
    44. mRunning = true;
    45. mStart->setEnabled(false);
    46. mPause->setEnabled(true);
    47. mStop->setEnabled(true);
    48. }
    49.  
    50. void pause(void)
    51. {
    52. mStart->setEnabled(true);
    53. mPause->setEnabled(false);
    54. mStop->setEnabled(true);
    55. timerEvent(new QTimerEvent(0));
    56. mTotalTime += mSessionTime;
    57. mRunning = false;
    58. }
    59.  
    60. void stop(void)
    61. {
    62. mStart->setEnabled(true);
    63. mPause->setEnabled(false);
    64. mStop->setEnabled(false);
    65. mTotalTime = 0;
    66. mRunning = false;
    67. }
    68.  
    69. protected:
    70. void timerEvent(QTimerEvent *)
    71. {
    72. if(mRunning)
    73. {
    74. mSessionTime = mStartTime.msecsTo(QDateTime::currentDateTime());
    75. qint64 time = mTotalTime + mSessionTime;
    76. time *= 111;
    77. unsigned int h = time / 1000 / 60 / 60;
    78. unsigned int m = (time / 1000 / 60) - (h * 60);
    79. unsigned int s = (time / 1000) - ((m + (h * 60))* 60);
    80. unsigned int ms = time - (s + ((m + (h * 60))* 60)) * 1000;
    81. const QString diff = QString("%1:%2:%3,%4")
    82. .arg(h, 2, 10, QChar('0'))
    83. .arg(m, 2, 10, QChar('0'))
    84. .arg(s, 2, 10, QChar('0'))
    85. .arg(ms, 3, 10, QChar('0'));
    86. mLabel->setText(diff);
    87. }
    88. }
    89.  
    90. private:
    91. bool mRunning;
    92. QDateTime mStartTime;
    93. QLabel * mLabel;
    94. qint64 mTotalTime;
    95. qint64 mSessionTime;
    96.  
    97. QPushButton * mStart;
    98. QPushButton * mPause;
    99. QPushButton * mStop;
    100. };
    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.

  8. #8
    Join Date
    May 2013
    Posts
    47
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: A Stopwatch in qt GUI ?

    Yea i followed the way you did it , just took away the adding of widget and the ms part . As i am using qt designer to add the buttons . The stopwatch is working perfectly fine just after the 1 hr 30min mark , something went wrong . I will counter check the code . Will update this post if there are still errors

  9. #9
    Join Date
    May 2013
    Posts
    47
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: A Stopwatch in qt GUI ?

    Ok , the problem is soved already , It seems that i did not copy the unsigned h part correctly . Thank you so much for the help Santoshb Reddy

  10. #10
    Join Date
    Jan 2015
    Posts
    1
    Qt products
    Qt5

    Default Re: A Stopwatch in qt GUI ?

    Hello, Santosh,

    sorry to ask this, but how exactly can I get this code to run? I get lots of errors.

    I know my question exposes huge deficit in most basic knowledge of the QT environment, however I need a cutom stopwatch next week So I was just googling for some code and found this.

    I downloaded QT Creator and made a new project with the wizard, which generated three files: main.cpp, mainwindow.cpp and mainwindow.h

    Where do I have to put your code to be able to run it?

    Sorry for my ignorance, I will certainly dig deeper in near future, but I need a quick start right now. Yes, I understand what QT is, but I do not have experience in C++ - I speak some C, PHP, Python and web related things, so this is mainly about not understanding the formalities of the environment, I do indeed understand what your code does, but I have no clue how to accomodate the QT Creator to make it executable.

    Thanks for your attention!
    John

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.