未启动:
启动:
.cpp
#include "widget.h"
#include "ui_widget.h"
void Widget::btn1_slots()
{
//点击启动开始定时
event_timer = this->startTimer(1000);
btn1->setEnabled(false);
btn2->setEnabled(true);
edit2->setEnabled(false);
txtEit->setEnabled(false);
}
void Widget::btn2_slots()
{
this->killTimer(event_timer);
btn1->setEnabled(true);
btn2->setEnabled(false);
edit2->setEnabled(true);
txtEit->setEnabled(true);
}
void Widget::btn3_slots()
{
this->close();
}
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
lab1 = new QLabel("背景",this);
movie = new QMovie(":/Logo/nan.gif");
lab1->setMovie(movie);
movie->start();
lab1->adjustSize();
//当前系统时间框位置
edit1 = new QLineEdit("当前时间",this);
edit1->resize(230,45);
edit1->move(420,165);
edit1->setFont(QFont("微软雅黑",12));
edit1->setStyleSheet("border:none;border-bottom:1px solid black;background:transparent");
edit1->setEnabled(false);
//用户手动输入时间
edit2 = new QLineEdit("定闹钟啦",this);
edit2->resize(160,35);
edit2->move(420,300);
edit2->setFont(QFont("微软雅黑",10));
edit2->setStyleSheet("border:none;border-bottom:1px solid black;background:transparent");
//文本编辑框
txtEit = new QTextEdit("输入闹钟文本",this);
txtEit->resize(230,280);
txtEit->move(90,250);
txtEit->setFont(QFont("微软雅黑",16));
txtEit->setStyleSheet("border:none;border-top:1px solid black;border-bottom:1px solid black;background:transparent");
//启动按钮
btn1 = new QPushButton(QIcon(""),"启动",this);
btn1->resize(120,60);
btn1->move(380,350);
btn1->setFont(QFont("微软雅黑",15));
btn1->setStyleSheet("border-radius:10");
connect(btn1,&QPushButton::clicked,this,&Widget::btn1_slots);
//停止按钮
btn2 = new QPushButton(QIcon(""),"停止",this);
btn2->resize(120,60);
btn2->move(490,350);
btn2->setFont(QFont("微软雅黑",15));
btn2->setStyleSheet("border-radius:10");
//设置初始不可以使用
btn2->setEnabled(false);
connect(btn2,&QPushButton::clicked,this,&Widget::btn2_slots);
//关闭按钮
btn3 = new QPushButton(QIcon(":/Logo/quxiao.png"),"关闭",this);
btn3->resize(70,50);
btn3->move(500,60);
btn3->setFont(QFont("微软雅黑",10));
btn3->setStyleSheet("border-radius:10");
connect(btn3,&QPushButton::clicked,this,&Widget::btn3_slots);
//speecher.say(this->txtEit->toPlainText());
}
Widget::~Widget()
{
delete ui;
}
void Widget::mousePressEvent(QMouseEvent *event)
{
//获取当前的vector位置
vector = event->globalPos() - this->frameGeometry().topLeft();
//光标的全局坐标 //当前界面的几何图形的左上角的坐标
}
void Widget::mouseMoveEvent(QMouseEvent *event)
{
//将lab标签,跟随鼠标一起移动
this->move(event->globalPos()-vector);
}
void Widget::timerEvent(QTimerEvent *e)
{
if(e->timerId() == event_timer)
{
this->edit1->setAlignment(Qt::AlignHCenter | Qt::AlignHCenter);
//获取系统时间
QDateTime sys_dt = QDateTime::currentDateTime();
//把时间转换为字符串并写在edit2上
this->edit1->setText(sys_dt.toString("HH:mm:ss"));
}
if(edit2->text() == edit1->text())
{
speecher.say(this->txtEit->toPlainText());
QMessageBox::information(this,"提示","闹钟开始");
}
}
.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QWidget>
#include <QDebug>
#include <QIcon>//图标头文件
#include <QPushButton>
#include <QLabel> //标签类头文件
#include <QLineEdit>
#include <cstring>
#include <QTextToSpeech>
#include <QMovie>
#include <QMessageBox>
#include <QTextEdit>
#include <QMouseEvent>
#include <QDateTime>//定义日期时间类
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
private slots:
void btn1_slots();
void btn2_slots();
void btn3_slots();
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
void mousePressEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
//重写定时器事件处理函数
void timerEvent(QTimerEvent *e);
private:
Ui::Widget *ui;
//定义一个定时器id,int类型
int event_timer;
QLabel *lab1;
QLineEdit *edit1; //当前时间
QLineEdit *edit2; //用户所定时间
QMovie *movie;
QPoint vector;//鼠标组件向量
QTextToSpeech speecher;//播报
QPushButton *btn1;
QPushButton *btn2;
QPushButton *btn3;
QTextEdit *txtEit;
};
#endif // WIDGET_H