我想創建一個應用程序,它包含一個任務列表,並且每次都有一個截止日期,現在我想要在滿足最後期限時執行一個函數(顯示一個彈出窗口)。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