Qt run at certain time? - c++

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()));

Related

How to use QTimer for non-ui task in QT?

In my QT project I want to do a non ui activity; which is sending files to server every 60 minutes.
I tried using QTimer for it and for that QCoreApplication instance is also needed.
The timer does start and call the upload function periodically but it keeps QCoreApplication hostage (unable to delete it as timer would be running infinitely) and ultimately application crashes.
I tried to move QTimer to another thread, but that also doesn't solve the issue as app is still running in main thread.
Below is code of function which is called from __stdcall function loaded during dll initialization.
{
int argc=0;
QCoreApplication app(argc, nullptr);
QThread* newThread = new QThread(this);
QTimer *timer = new QTimer(0);
timer->setInterval(60*60*1000);
timer->moveToThread(newThread);
QObject::connect(timer, SIGNAL(timeout()), this, SLOT(uploadFiles()), Qt::DirectConnection);
timer->connect(newThread, SIGNAL(started()), SLOT(start()));
newThread->start();
app.exec();
}
Maybe this is not best approach to do it . Need guidance on how to achieve it.
Update:
Figured out that Qtimer is definitely not ideal way to achieve it.
I was able to perform periodic execution using std::thread and atomic.

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();

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.

Infinite Looping in a Qt Widget Project

I am making something in a Qt Widget Project, coded in C++. Because of what I need to do, I need infinite looping, and after doing my research, I realized that infinite looping in an object's event doesn't work, and instead, I need to use a some threading..
I decided to use QTimer, but am thinking about using QThread. Which one should I use?
This is my QTimer code, which doesn't seem to work:
Clock_Application::Clock_Application(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Clock_Application)
{
ui->setupUi(this);
QTimer *timer_Stopwatch = new QTimer(this);
connect(timer_Stopwatch, SIGNAL(timeout()), this, SLOT(timer_Start()));
timer_Stopwatch->start(1000);
}
Edit: a simple fix to this solution is using the QCoreApplication::processEvents() function, after every iteration of the loop, as that allows the widget application to process necessary events. This is a quick fix however, and using the QTimer library is a better way to fix it.
As you suggest yourself, performing your processing in a separate thread would be an option and I would surely look into doing that. I don't think that the QTimer as you show it will help.
A quick fix however would be to manually call QCoreApplication::processEvents() at the end of each loop. This will make sure that all the pending events get processed by Qt, keeping your UI responsive.
Your timer is not working because it only exists in the constructor. You should declare it out the constructor in the header file as private for example.

QT progress bar speed

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