Results 1 to 4 of 4

Thread: QTimer advice

  1. #1
    Join Date
    May 2010
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default QTimer advice

    Hi,

    I'm a relative newbie to Qt but am so far enjoying it. I have a question about QTimer. I have it working in an application but am just wondering if I am going about it the correct way.

    Qt Code:
    1. #include "timerExample.h"
    2. #include <QtGui>
    3.  
    4. timerExample::timerExample(QWidget *parent, Qt::WFlags flags)
    5. : QWidget(parent, flags)
    6. {
    7. ui.setupUi(this);
    8. time = 0;
    9. timeInterval = 1; // this sets the time interval at 1 millisecond
    10. timer = new QTimer;
    11. connect(timer, SIGNAL(timeout()), this, SLOT(update()));
    12. }
    13.  
    14.  
    15. void timerExample::update()
    16. {
    17. time = time + timeInterval;
    18. }
    19.  
    20.  
    21. void timerExample::on_threadStartButton_clicked()
    22. {
    23. timer->start(timeInterval);
    24. }
    25.  
    26. void timerExample::on_threadStopButton_clicked()
    27. {
    28. timer->stop();
    29.  
    30. // writes out time taken to a LineEdit
    31. QString str = QString::number(time);
    32. ui.timeLine->setText(str);
    33. }
    To copy to clipboard, switch view to plain text mode 


    Is this the right way to use QTimer or am I missing something?

  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: QTimer advice

    Theoretical it's ok, but in your case nonsens, and 1 millisecond is normally too short (a function call every millisecond...). In your case better have a look at QTime and QTime::elapsed().

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

    qt_beginner (10th May 2010)

  4. #3
    Join Date
    May 2010
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTimer advice

    Thanks for your input. I see that that function is exactly what I need but I'm using Qt 4.4. I can't see a similar method to that described in this version.

  5. #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: QTimer advice

    Well, it must be there: http://doc.trolltech.com/4.3/qtime.html#elapsed. But anyway, then do it yourself. On first button take the value of QTime::currentTime() and on the second button also and then calculate the difference between both.

Similar Threads

  1. Advice for an Application
    By salmanmanekia in forum Newbie
    Replies: 1
    Last Post: 19th April 2010, 12:06
  2. QtConcurrent, i need advice
    By SABROG in forum Qt Programming
    Replies: 10
    Last Post: 29th December 2009, 19:53
  3. Basic Advice
    By Ferric in forum Newbie
    Replies: 1
    Last Post: 18th December 2009, 08:52
  4. Need Advice: Best IDE for Mac OSX
    By JimDaniel in forum Qt Programming
    Replies: 6
    Last Post: 18th October 2008, 23:14
  5. Newbie needs advice
    By Seth in forum Newbie
    Replies: 3
    Last Post: 12th April 2007, 22:50

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.