QT progress bar speed - c++

Does QT provide any functions to control a progress bar's speed? For example, if I want it to increase by 1% every 1 second, is there any QT way to do it instead of using a loop and sleeping for 1 second between each value change?

You can use QTimeLine for this. The detailed description in the documentation gives an example of exactly what you want.

Use a QTimer.
Connect the signal timeout() to a slot that increases the value in the QProgressBar.
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(1000);
In this cas, update() will be call each second.

If you're using Qt 4.6 you can also use QPropertyAnimation

Related

QTimer dont stop when windows is closed

I am currently starting on QTCreator. I have been asked to use QTimers in a particular context which is this:
We have an open window,
One or more QTimers are triggered and make things appear on the screen every x msec.
When we press "Escape" the window should close and everything should be reset to 0.
But here is the problem, the timers are defined in a static way:
QTimer::singleShot(500, this, SLOT(foo());
When I call this->close() (which closes my window), the timers do not stop and continue. I tried several solutions: browse all the QTimers contained in my object, obviously there are none since they are defined in static. Instead of declaring them in static I've tried to create each time a new QTimer object like that:
QTimer *timer= new QTimer(this);
timer->setSingleShot(true);
timer->setInterval(2000);
timer->setParent(this);
timer->start();
And then call timer->stop() later, but I think it's very brutal when you have multiple Timers in the same code.
Is there a way to stop the timers when this->close is called, knowing that the timers are defined as a static one ?
Assuming you are using,
QWindow *qw = new QWindow();
QTimer *timer= new QTimer();
To solve the issue you need to connect destroyed() signal of QWindow to timer's slot stop()
So as soon as window is destroyed all registered timers will be stopped without explicit stop call. make sure you connect all timer instances. Code snippet as following,
QObject::connect(&qw, SIGNAL(destroyed()), timer, SLOT(stop()))
QObject::connect(&qw, SIGNAL(destroyed()), timer2, SLOT(stop()))
QObject::connect(&qw, SIGNAL(destroyed()), timer3, SLOT(stop()))
PS:
QTimer *timer= new QTimer(this); // here you are setting parent as 'this' already
timer->setSingleShot(true);
timer->setInterval(2000);
timer->setParent(this); // remove this, no need to set parent again.
timer->start();

Call a function n times with an interval of n seconds

I want to simply call a function 4 times within an interval of 10 seconds.
This calls it only once
QTimer *timer = new QTimer(this);
timer->setSingleShot(true);
connect(timer, SIGNAL(timeout()), this, SLOT(myFunction()));
timer->start(10000);
And if I get rid of the setSingleShot method, it will be called forever. Is there a built in way to call it only n times. I couldn't find it in the Qt documentation.
The brute-force, non-scalable way: start four timers.
A more practical way is to have the callback keep track of how many times it has been called, then stop the timer on the fourth call.

How to call a function periodically in Qt?

Is it possible to call a function periodically in C++ with Qt function ?
And how to stop the timed function after it is set to be called periodically ?
If you are using qt, you can you QTimer which by default creates a repetitive timer.
There is an example in the documentation (shown below) and an example (Analog Clock).
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(1000);
One possibility would be to use a QTimer timeout signal and a QObject slot. Connect the two and start() the timer.
http://qt-project.org/doc/qt-4.8/qtimer.html#timeout
To stop the timer, call stop().
As people have said in answers before me, you can use the timeout() signal to trigger a function to run.
If you want to stop the timer at some point, you can connect to the stop() slot, or call it directly yourself.
You can use the QTimer class.
Just declare a QTimer with the desired time interval, wrap your function in a QObject as a slot, and connect the QTimer's timeout() signal to the slot you just declared.
Then, when the condition for stopping calling the function is met, just call QTimer::stop().
Make a function that uses timer functionallity or a while loop that just waits for 100 ms and when your function meets the requirement just break. You could quite easy found a solution on this one if you just made a search among all the other questions that has been posted here.

Qt run at certain time?

Is there a easy way to call a event at a given time in Qt? Or do I need to implement some loop to check the time?
Any advice on how to do this would be greatly appreciated, even if it's not using Qt
QTimer would help.
Something like this:
QTImer *timer = new QTimer(this);
timer->setInterval(1000);
connect(timer, SIGNAL(timeout()), this, SLOT(timerTimeout()));

pause functionality in Qt using QTimer on button press

I am trying to implement pause button for a game I am developing in Qt + OpenGL.
I want to implement it using QTimer.
Basically I am updating screen per 100ms. So in order to pause game, I will stop the timer on button press. and when button is again pressed i will start the timer again
Here is my pauseOrPlay SLOT:
void Window::pauseOrPlay()
{
GLWidget::modifyTimer = TRUE;
GLWidget::isPaused = !GLWidget::isPaused;
GLWidget timerUpdater;
timerUpdater.timerFunc();
}
and Here is my timerFunc()
GLvoid GLWidget::timerFunc()
{
static QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
if( GLWidget::isPaused)
timer->start(100);
else
timer->stop();
}
But I am not getting the functionality. I get paused screen on this particular code and upon trying few tweaks here and there, sometimes I get screen updating very fastly which pointed me to this but I was unable to find cure to my problem somehow
Any help or Pointers?
PS: In this question there is nothing about OpenGL, but I think those are the people who might have dealt with similar think, adding OpenGL tag
That connection is in a bad place. You have it set up to connect each time you pause or resume the game. But if you connect multiple times, your slot will be called multiple tiles for each signal emission.
Make sure you only create the timer and connect to it once. I suggest moving the timer construction and signal connection into your GLWidget's constructor. Store a pointer to the timer as a member variable of your class so you can start and stop it in the class's member functions.