我想创建一个应用程序,它包含一个任务列表,并且每次都有一个截止日期,现在我想要在满足最后期限时执行一个函数(显示一个弹出窗口)。QT C++等待具体时间来执行功能
我有这样的:
#ifndef TIMER_H
#define TIMER_H
#include <QWidget>
#include <QTimer>
#include <QtGui>
#include <QObject>
class Timer : public QWidget
{
Q_OBJECT
public:
Timer(QWidget * parent = 0);
void setTimer(QString title, QString description, QDate date, QTime reminderTime);
public slots:
void showWarning() {QString show = tit;
QPushButton * thanks = new QPushButton(QObject::tr("Thank you for reminding me!"));
show.append("\n");
show.append(des);
QMessageBox popup;
popup.setText(show);
popup.setWindowTitle("Calendar : Reminder");
popup.setDefaultButton(thanks);
popup.exec();
}
private:
QString tit;
QString des;
QDateTime now;
QDateTime timeoftheaction;
QTimer *timer;
};
CPP文件:
#endif // TIMER_H
#include "timer.h"
#include <iostream>
using namespace std;
Timer::Timer(QWidget * parent)
: QWidget(parent)
{
}
void Timer::setTimer(QString title, QString description, QDate date, QTime reminderTime)
{
now.currentDateTime();
timer = new QTimer;
tit = title;
des = description;
timeoftheaction.setDate(date);
timeoftheaction.setTime(reminderTime);
connect(timer, SIGNAL(timeout()),this,SLOT(showWarning()));
timer->start(now.secsTo(timeoftheaction)*1000);
}
但功能showWarning从不被称为...... 没有编译错误,功能showWarning完美的作品(测试)
我认为错误是在连接,但我不知道...
你是否检查过'now.secsTo(timeoftheaction)* 1000'实际上给你你认为它是什么? – enderland 2012-08-16 23:22:18
如果将计时器设置为:'timer-> start(1)',它会立即启动吗?如果是这样,那么@恩德兰的建议是正确的 – jdi 2012-08-17 00:14:18
这也可能是一个解决方案: http://stackoverflow.com/questions/11996706/qt-c-wait-till-specific-time-to-executefunction/14144614#14144614 – formiaczek 2013-01-03 18:14:53