1> 实现闹钟功能
头文件
- #ifndef WIDGET_H
- #define WIDGET_H
-
- #include
- #include
- #include
- #include
- #include
- #include
//定时器事件类 - #include
- #include
- #include
-
-
- 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 slots:
- void fixed_time_clicked();
- void cancel_clicked();
-
- void mousePressEvent(QMouseEvent *event) override;
- void mouseMoveEvent(QMouseEvent *event) override;
-
- private:
- Ui::Widget *ui;
-
- QLabel *back; //背景
- QLabel *TIME; //实时显示时间
- QPushButton *fixed_time; //定时按钮
- QPushButton *cancel; //取消按钮
- QLineEdit *clock; //设置闹钟
- QTextEdit *text; //设置播报的闹钟内容
- int timer_id;
- int timer_clock;
- QTextToSpeech *speecher; //播报员
- QPoint drap;
- };
- #endif // WIDGET_H
源文件
- #include "widget.h"
- #include "ui_widget.h"
-
- Widget::Widget(QWidget *parent)
- : QWidget(parent)
- , ui(new Ui::Widget)
- {
- ui->setupUi(this);
-
- //窗口设置
- this->setFixedSize(600,300);
- this->setWindowTitle("Bloodborne alarm clock");
- this->setWindowIcon(QIcon(":/pricte/641193.jpg"));
- this->setWindowFlag(Qt::FramelessWindowHint); //纯净窗口
-
- //设置背景
- back = new QLabel(this);
- back->resize(this->size());
- back->setPixmap(QPixmap(":/pricte/R-C.jpg"));
- back->setScaledContents(true);
-
- //设置时间显示
- TIME = new QLabel(this);
- TIME->resize(350,40);
- QFont font("黑体",30,100);
- TIME->setStyleSheet("font-size:35px;"
- "color:white;");
- TIME->move(30,30);
- timer_id = this->startTimer(1000);
-
- //设置闹钟
- clock = new QLineEdit(this);
- clock->resize(120,30);
- clock->move(TIME->x()+430,TIME->y());
- clock->setPlaceholderText("设置闹钟");
- clock->setStyleSheet("background:rgb(0,0,0,0);"
- "color:white;");
-
-
- //定时按钮
- fixed_time = new QPushButton("定时",this);
- fixed_time->move(clock->x(),clock->y()+40);
- fixed_time->resize(clock->width()/2-5,25);
- fixed_time->setStyleSheet("background:rgb(102,255,255,80);"
- "color:white;");
- connect(fixed_time, &QPushButton::clicked, this, &Widget::fixed_time_clicked);
-
- //取消按钮
- cancel = new QPushButton("取消",this);
- cancel->move(clock->x()+fixed_time->width()+10,clock->y()+40);
- cancel->resize(clock->width()/2-5,25);
- cancel->setStyleSheet("background:rgb(102,255,255,80);"
- "color:white;");
- connect(cancel, &QPushButton::clicked, this, &Widget::cancel_clicked);
- cancel->setEnabled(false);
-
-
- //设置播报的闹钟内容
- text = new QTextEdit(this);
- text->resize(320,80);
- text->move(30,100);
- text->setStyleSheet("background:rgb(0,0,0,0);color:"
- "white;");
- text->setPlaceholderText("设置播报的闹钟内容");
- text->setFont(QFont("宋体",15));//设置字体和字体大小
-
- speecher = new QTextToSpeech(this);
- }
-
- Widget::~Widget()
- {
- delete ui;
- }
-
- //时间显示 时间比较
- void Widget::timerEvent(QTimerEvent *event)
- {
- QDateTime sys_dt = QDateTime::currentDateTime();
-
- if(event->timerId() == timer_id)
- {
- TIME->setText(sys_dt.toString("yyyy-MM-dd hh:mm:ss"));
- }
- if(event->timerId() == timer_clock)
- {
- QString a = clock->text();
-
- QString t = sys_dt.toString("hh:mm:ss");
-
- QString b = text->toPlainText();
- if(a == t)
- {
- speecher->say(b);
- }
- }
- }
-
- //定时按钮槽函数
- void Widget::fixed_time_clicked()
- {
-
- timer_clock = this->startTimer(1000);
-
- fixed_time->setEnabled(false);
- text->setEnabled(false);
- clock->setEnabled(false);
- cancel->setEnabled(true);
- }
-
- //取消按钮槽函数
- void Widget::cancel_clicked()
- {
- this->killTimer(timer_clock);
-
- fixed_time->setEnabled(true);
- text->setEnabled(true);
- clock->setEnabled(true);
- cancel->setEnabled(false);
-
- clock->clear();
- }
-
- //鼠标按下事件处理函数
- void Widget::mousePressEvent(QMouseEvent *event)
- {
- drap = event->globalPos()-this->pos();
-
- if(event->button() == Qt::RightButton)
- {
- this->close();
- }
- }
-
- //鼠标移动事件处理函数
- void Widget::mouseMoveEvent(QMouseEvent *event)
- {
- this->move(event->globalPos() - drap);
- }
实现:
思维导图: