PDA

View Full Version : Time measurement



antinkuntin
6th July 2012, 09:48
How can i measure time that is between button pressed and released states?

wysota
6th July 2012, 10:02
QTime::elapsed() or QElapsedTimer.

antinkuntin
6th July 2012, 10:40
void Button::mousePressEvent(QMouseEvent *e)
{
QElapsedTimer t;
t.start();

QAbstractButton::mousePressEvent(e);

}
void Button::mouseReleaseEvent(QMouseEvent *e)
{

QAbstractButton::mouseReleaseEvent(e);
int remainingTime =t.elapsed();
if(remainingTime>5000)
QAbstractButton::mousePressEvent(e);
else
e->ignore();

}
why is 'remainingTime' equal zero all the time?

wysota
6th July 2012, 10:50
Because your local variable goes out of scope. By the way, what are you trying to achieve? Your code does not make much sense.

antinkuntin
6th July 2012, 11:06
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? :D

high_flyer
6th July 2012, 11:43
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.

antinkuntin
6th July 2012, 11:43
you can help me?

wysota
6th July 2012, 12:29
my problem is theris a push button and if the button is pressed during5 sec.,it will change its state. is it clear? :D

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.

antinkuntin
6th July 2012, 13:23
it didn't work. Can you write an example code? maybe i want lots of thing but i need to help.

wysota
7th July 2012, 18:11
What didn't work?

chriskon149
9th July 2012, 06:07
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
QTime * buttonTimer; in your header file.

Then, to detect if the button was pressed for 5 seconds or more:


void MainWindow::on_pushButton_pressed()
{
buttonTimer = new QTime(); //Creates object
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
}
}


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, 08:04
thank you so much. Only you exactly understood me. :)

wysota
10th July 2012, 00:19
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.

chriskon149
10th July 2012, 03:07
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
buttonTimer = new QTime(); 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

wysota
10th July 2012, 06:24
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.

mortonhalden
21st February 2013, 13:44
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.

anda_skoa
22nd February 2013, 14:38
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,
_

amleto
22nd February 2013, 23:17
there're also plenty of timing related classes in <chrono> header