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
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    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.

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

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

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