Reputation: 186
For school I have to create a calendar program for the PC with Qt, this calendar also has to give notifications a certain time before some scheduled appointment starts.
But i have trouble finding out how I exactly can create this notifications without using a while loop (which will stop my program), I would be really thankfully if someone can help me a bit with that certain part of my project.
Thank you, Dennis
Upvotes: 0
Views: 1290
Reputation: 5083
You can subclass QCalendarWidget to create your GUI i.e. the user can select the date when he wants the appointment. Use the QTimer to trigger the SIGNAL at your precise appointment time.
Now you can get the current date and time using QDateTime.
1. Check if the appointment is on the same date as current.
2. If yes jump to step no 4.
3. Set a timer using QTimer to emit a SIGNAL after 24 hours and connect this SIGNAL to your custom SLOT which will check if the current date is same as appointment date. Continue this step until your current date is same as appointment date.
4. Calculate the time difference between your appointment and current time and set this difference to a QTimer which will emit a SIGNAL at the required time.
5. This emitted SIGNAL would be connected a SLOT in which you are doing whatever is needed when the appointment is reached
I hope this helps you get in track. Giving code would be like solving your homework which I don't want to do.
Upvotes: 2
Reputation: 6057
You can create a QTimer
instance with interval set to 1 second. Then connect to QTimer::timeout()
signal and in the slot you can check whether there is any appointment approaching, something like that:
void YourClass::slotNameForTimeoutSignal()
{
static const int fifteenMinutes = 15 * 60;
foreach (const Appointment& app : allAppoitments)
{
if ((app.getStartUnixTime() - QDateTime::currentDateTime().toTime_t()) <= fifteenMinutes)
{
notifyAboutTheAppointment(app); // implement this method to display notification
}
}
}
This code assumes you have some kind of Appointment
class/structure which holds unixtime when the appoitment starts. The timer setup is easy. Somewhere in your application initialization create a timer (which should be a member field in your class), set it up and run:
YourClass::YourClass()
{
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(slotNameForTimeoutSignal()));
timer->setInterval(1000);
timer->setSingheShot(false);
timer->start();
}
If you didn't know it yet, the unixtime is a time format, which is a integer number - a number of seconds that passed since the begining of 1970. Here's more details: http://en.wikipedia.org/wiki/Unix_time
Also if you're not familar with signals/slots in Qt, you should really read first about those in Qt documentation.
Upvotes: 3