做一个闹钟
头文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QMouseEvent>
#include <QTimer>
#include <QTime>
#include <QTimerEvent>
#include<QTextToSpeech> //文本转语音类
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
void mouseMoveEvent(QMouseEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
signals:
void my_signal(); //信号函数的定义
private:
Ui::Widget *ui;
QTextToSpeech *speecher; //定义播报员指针
QTimer t1;
QPoint temp;
};
#endif // WIDGET_H
源文件
#include "widget.h"
#include "ui_widget.h"
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include<QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//给播报员实例化空间
this->speecher = new QTextToSpeech(this);
this->setWindowFlag(Qt::FramelessWindowHint);
QLabel *lab = new QLabel(this);
lab->move(50,75);
lab->resize(300,150);
lab->setScaledContents(true);
lab->setStyleSheet("background-color:skyblue;");
QLineEdit *edit = new QLineEdit(this);
edit->move(400,75);
edit->resize(350,40);
QPushButton *btn1 = new QPushButton("启动",this);
btn1->resize(70,50);
btn1->move(edit->x()+100,edit->y()+100);
QPushButton *btn2 = new QPushButton("取消",this);
btn2->resize(70,50);
btn2->move(btn1->x()+btn1->width()+50,btn1->y());
btn2->setEnabled(false);
QLineEdit *edit1 = new QLineEdit(this);
edit1->move(50,250);
edit1->resize(700,300);
connect(btn1, &QPushButton::clicked, [=](){
t1.start(1000);
btn1->setEnabled(false);
btn2->setEnabled(true);
edit->setEnabled(false);
edit1->setEnabled(false);
});
connect(btn2, &QPushButton::clicked, [=](){
t1.stop();
btn1->setEnabled(true);
btn2->setEnabled(false);
edit->setEnabled(true);
edit1->setEnabled(true);
});
connect(&t1,&QTimer::timeout,[=](){
QTime systime = QTime::currentTime();
QString tm = systime.toString("hh:mm:ss");
lab->setText(tm);
lab->setAlignment(Qt::AlignCenter);
if(edit->text()==lab->text())
{
edit->clear();
btn1->setEnabled(true);
btn2->setEnabled(false);
edit->setEnabled(true);
edit1->setEnabled(true);
t1.stop();
QString dt = edit1->text();
//让播报员进行播报
speecher->say(dt);
emit my_signal();
}
});
connect(this, &Widget::my_signal, [&](){
t1.stop();
});
}
Widget::~Widget()
{
delete ui;
}
void Widget::mouseMoveEvent(QMouseEvent *event)
{
this->move(event->globalPos() - temp);
}
void Widget::mousePressEvent(QMouseEvent *event)
{
temp = event->globalPos() - this->pos();
if(event->buttons()==Qt::RightButton)
{
this->close();
}
}
思维导图