Results 1 to 18 of 18

Thread: Time measurement

  1. #1
    Join Date
    Jul 2012
    Posts
    9
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    2

    Default Time measurement

    How can i measure time that is between button pressed and released states?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Time measurement

    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.


  3. #3
    Join Date
    Jul 2012
    Posts
    9
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    2

    Default Re: Time measurement

    Qt Code:
    1. void Button::mousePressEvent(QMouseEvent *e)
    2. {
    3. QElapsedTimer t;
    4. t.start();
    5.  
    6. QAbstractButton::mousePressEvent(e);
    7.  
    8. }
    9. void Button::mouseReleaseEvent(QMouseEvent *e)
    10. {
    11.  
    12. QAbstractButton::mouseReleaseEvent(e);
    13. int remainingTime =t.elapsed();
    14. if(remainingTime>5000)
    15. QAbstractButton::mousePressEvent(e);
    16. else
    17. e->ignore();
    18.  
    19. }
    To copy to clipboard, switch view to plain text mode 
    why is 'remainingTime' equal zero all the time?
    Last edited by antinkuntin; 6th July 2012 at 11:18.

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

    Default Re: Time measurement

    Because your local variable goes out of scope. By the way, what are you trying to achieve? Your code does not make much sense.
    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.


  5. #5
    Join Date
    Jul 2012
    Posts
    9
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    2

    Default Re: Time measurement

    i know im trying my problem is theris a push button and if the button is pressed during5 sec.,it will change its state. is it clear?
    Last edited by antinkuntin; 6th July 2012 at 11:17.

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: Time measurement

    Set the timeout to 5 secs on a timer.
    Then connect the clicked() signal of the button to the start() slot of the timer.
    Connect the timeout() signal from the timer to a slot (in the button, or anywhere that makes sense), and there change the button state.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

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

    antinkuntin (6th July 2012)

  8. #7
    Join Date
    Jul 2012
    Posts
    9
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    2

    Default Re: Time measurement

    you can help me?

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Time measurement

    Quote Originally Posted by antinkuntin View Post
    my problem is theris a push button and if the button is pressed during5 sec.,it will change its state. is it clear?
    If your code worked, it would do something else -- it would try to press the mouse button on the push button if you released the mouse after having pressed it for 5 seconds.
    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.


  10. #9
    Join Date
    Jul 2012
    Posts
    9
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    2

    Default Re: Time measurement

    it didn't work. Can you write an example code? maybe i want lots of thing but i need to help.

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Time measurement

    What didn't work?
    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.


  12. #11
    Join Date
    Aug 2011
    Location
    California, USA
    Posts
    24
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Default Re: Time measurement

    Quote Originally Posted by antinkuntin View Post
    it didn't work. Can you write an example code? maybe i want lots of thing but i need to help.
    If I'm understanding you correctly, you want to make a button change it's state if it's pressed for 5 seconds? If so, you can create a pointer to a QTime
    Qt Code:
    1. QTime * buttonTimer;
    To copy to clipboard, switch view to plain text mode 
    in your header file.

    Then, to detect if the button was pressed for 5 seconds or more:
    Qt Code:
    1. void MainWindow::on_pushButton_pressed()
    2. {
    3. buttonTimer = new QTime(); //Creates object
    4. buttonTimer->start(); //Starts the QTime
    5. }
    6.  
    7. void MainWindow::on_pushButton_released()
    8. {
    9. if(buttonTimer->elapsed() >= 5000) //QTime::elapsed() returns the number of milliseconds since QTime::start() was called
    10. {
    11. //Set button state to whatever you want it to be (down, checked, up, unchecked, etc)
    12. }
    13. else
    14. {
    15. //Button wasn't pressed for 5 seconds
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    You want to create a pointer to a QTime in your header file so that your QTime does not fall out of scope as your code executes.

    I hope this helps!

    Chris

  13. The following user says thank you to chriskon149 for this useful post:

    antinkuntin (9th July 2012)

  14. #12
    Join Date
    Jul 2012
    Posts
    9
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    2

    Default Re: Time measurement

    thank you so much. Only you exactly understood me.

  15. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Time measurement

    You don't need any pointers that leak memory but rather a member variable of type QTime. However what you really need is what was given in post #6.
    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.


  16. #14
    Join Date
    Aug 2011
    Location
    California, USA
    Posts
    24
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Default Re: Time measurement

    Quote Originally Posted by wysota View Post
    You don't need any pointers that leak memory but rather a member variable of type QTime. However what you really need is what was given in post #6.
    Sorry about that. I should have put the
    Qt Code:
    1. buttonTimer = new QTime();
    To copy to clipboard, switch view to plain text mode 
    in the constructor of the main window and just called QTime::start(); each time the button is clicked. That way there isn't a memory leak.

    My bad!

    Chris

  17. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Time measurement

    This does not change two facts:
    1. the idea behind the code is wrong (e.g. when using NTP that can change your system time)
    2. allocating the QTime object on the heap is wrong.

    The proper way is to use a 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.


  18. #16
    Join Date
    Feb 2013
    Location
    India
    Posts
    4
    Qt products
    Qt3
    Platforms
    MacOS X

    Default Re: Time measurement

    I think you might want to add the mouseout(), to show the distinction soon enough, in situation the user pulls the mouse outside the factor before releasing. So, you should be make sure this thing, this will be definitely helpful for you.

  19. #17
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: Time measurement

    Quote Originally Posted by wysota View Post
    1. the idea behind the code is wrong (e.g. when using NTP that can change your system time)
    I think that can be fixed by using QElapsedTimer

    Cheers,
    _

  20. #18
    Join Date
    Sep 2011
    Posts
    1,241
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3
    Thanked 127 Times in 126 Posts

    Default Re: Time measurement

    there're also plenty of timing related classes in <chrono> header
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. Replies: 5
    Last Post: 19th November 2010, 03:25
  2. Replies: 6
    Last Post: 18th August 2010, 13:52
  3. Replies: 1
    Last Post: 25th June 2010, 19:31
  4. check the time of last time keyboard is pressed
    By Aki Tomar in forum General Programming
    Replies: 2
    Last Post: 5th February 2008, 10:10
  5. Time Measurement
    By Krish_ng in forum Qt Programming
    Replies: 3
    Last Post: 20th July 2007, 13:34

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.