直接看代码吧。反正贼简单。
#include "widget.h"
#include <QHBoxLayout>
#include <QMessageBox>
#include <windows.h>
#include <qdebug.h>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
QFont textFont;
textFont.setPointSize(12);
textFont.setFamily("Microsoft YaHei");
QWidget *topWgt=new QWidget(this);
m_pOkBtn=new QPushButton(tr("OK"),this);
m_pToolsEdit=new QTextEdit(this);
m_pTimeEdit=new QDateTimeEdit(this);
QDateTime dateTime=QDateTime::currentDateTime();
m_pTimeEdit->setDateTime(dateTime);
m_pOkBtn->setFont(textFont);
m_pToolsEdit->setFont(textFont);
m_pTimeEdit->setFont(textFont);
QHBoxLayout *topLayout=new QHBoxLayout(this);
topLayout->addWidget(m_pTimeEdit);
topLayout->addWidget(m_pOkBtn);
topLayout->setMargin(0);
topLayout->setSpacing(10);
topWgt->setLayout(topLayout);
QVBoxLayout *mainLayout=new QVBoxLayout(this);
mainLayout->addWidget(topWgt);
mainLayout->addWidget(m_pToolsEdit);
mainLayout->setMargin(10);
mainLayout->setSpacing(5);
this->setLayout(mainLayout);
this->resize(200,300);
connect(m_pOkBtn,&QPushButton::clicked,this,&Widget::onOkClicked);
}
Widget::~Widget()
{
}
void Widget::onOkClicked()
{
this->showMinimized();
QString strText=m_pToolsEdit->toPlainText();
if(strText!="")
{
QDateTime setTime=m_pTimeEdit->dateTime();
QDateTime currentTime=QDateTime::currentDateTime();
int setTime_t=setTime.toTime_t();
int currentTime_t=currentTime.toTime_t();
if(setTime_t-currentTime_t>0)
{
while (true) {
QDateTime thisTime=QDateTime::currentDateTime();
int thisTime_t=thisTime.toTime_t();
if(thisTime_t==setTime_t)
{
QMessageBox::warning(this, tr("Memorandum"),
"Don't forget\n"+strText,
QMessageBox::Ok);
break;
}
Sleep(1000);
}
}
else
{
QMessageBox::warning(this, tr("Memorandum"),
"Setting Time Cannot Lessthan CurrentTime!!",
QMessageBox::Ok);
}
}
else {
QMessageBox::warning(this, tr("Memorandum"),
"Infomation is NULL!!",
QMessageBox::Ok);
}
}
就是简单的定时功能。