How can i measure time that is between button pressed and released states?
How can i measure time that is between button pressed and released states?
why is 'remainingTime' equal zero all the time?Qt Code:
{ QElapsedTimer t; t.start(); } { int remainingTime =t.elapsed(); if(remainingTime>5000) else e->ignore(); }To copy to clipboard, switch view to plain text mode
Last edited by antinkuntin; 6th July 2012 at 11:18.
Because your local variable goes out of scope. By the way, what are you trying to achieve? Your code does not make much sense.
i know im tryingmy 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.
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.
antinkuntin (6th July 2012)
you can help me?
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 in your header file.
Then, to detect if the button was pressed for 5 seconds or more:
Qt Code:
void MainWindow::on_pushButton_pressed() { buttonTimer->start(); //Starts the QTime } void MainWindow::on_pushButton_released() { if(buttonTimer->elapsed() >= 5000) //QTime::elapsed() returns the number of milliseconds since QTime::start() was called { //Set button state to whatever you want it to be (down, checked, up, unchecked, etc) } else { //Button wasn't pressed for 5 seconds } }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
antinkuntin (9th July 2012)
thank you so much. Only you exactly understood me.![]()
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 thein 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.Qt Code:
To copy to clipboard, switch view to plain text mode
My bad!
Chris
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.
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.
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.
Bookmarks