作业
实现闹钟功能
头文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QDateTime>
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
#include <QTextEdit>
#include <QTimerEvent>
#include <QtTextToSpeech>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
void timerEvent(QTimerEvent *event)override;
private:
Ui::Widget *ui;
QLabel *time_lab;
QPushButton *start_btn;
QPushButton *stop_btn;
QPushButton *cancel_btn;
QLineEdit *settime_line;
QTextEdit *text;
QTextToSpeech *speecher;
int timer_id1;
int timer_id2;
public slots:
void slot_cancel();
void slot_start();
void slot_stop();
};
#endif // WIDGET_H
源文件
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//播报员
speecher = new QTextToSpeech(this);
//主界面
this->setWindowFlag(Qt::FramelessWindowHint);
this->setFixedSize(630,520);
//时间
time_lab = new QLabel(this);
time_lab->resize(400,100);
time_lab->setStyleSheet("QLabel{background-color:rgb(0,255,255)}");
//按钮
start_btn = new QPushButton(this);
stop_btn = new QPushButton(this);
start_btn->setText("启动");
start_btn->resize(100,50);
start_btn->move(time_lab->x()+time_lab->width()+20,time_lab->y()+50);
stop_btn->resize(start_btn->size());
stop_btn->setText("停止");
stop_btn->move(start_btn->x()+start_btn->width()+10,start_btn->y());
stop_btn->setEnabled(false);
connect(start_btn,&QPushButton::clicked,this,&Widget::slot_start);
connect(stop_btn,&QPushButton::clicked,this,&Widget::slot_stop);
//定时框
settime_line = new QLineEdit(this);
settime_line->resize(210,50);
settime_line->clear();
settime_line->move(start_btn->x(),time_lab->y());
//提醒
text = new QTextEdit(this);
text->resize(630,400);
text->move(time_lab->x(),time_lab->y()+time_lab->height()+20);
text->setFont(QFont("微软雅黑",18));
cancel_btn =new QPushButton(this); cancel_btn->setText("取消");
cancel_btn->resize(start_btn->size());
connect(this->cancel_btn,&QPushButton::clicked,this,&Widget::slot_cancel);
cancel_btn->move(this->width()-100,this->height()-50);
//时钟
timer_id1 = this->startTimer(1000);
}
Widget::~Widget()
{
delete ui;
}
void Widget::slot_cancel()
{
this->close();
}
void Widget::slot_start()
{
this->stop_btn->setEnabled(true);
this->start_btn->setEnabled(false);
this->settime_line->setEnabled(false);
this->text->setEnabled(false);
timer_id2 = this->startTimer(1000);
}
void Widget::slot_stop()
{
this->stop_btn->setEnabled(false);
this->start_btn->setEnabled(true);
this->settime_line->setEnabled(true);
this->text->setEnabled(true);
this->text->clear();
this->settime_line->clear();
this->killTimer(timer_id2);
}
void Widget::timerEvent(QTimerEvent *event)
{
if(event->timerId() == timer_id1)
{
QDateTime dt = QDateTime::currentDateTime();
QString s1 = dt.toString("yyyy:MM:dd hh:mm:ss");
this->time_lab->setText(s1);
this->time_lab->setAlignment(Qt::AlignHCenter);
this->time_lab->setFont(QFont("微软雅黑",20));
}else if(event->timerId() == timer_id2)
{
QString buf = settime_line->text();
QDateTime dt1 = QDateTime::currentDateTime();
QString s2 = dt1.toString("hh:mm:ss");
if(buf == s2)
{
speecher->say(this->text->toPlainText());
}
}
}
思维导图