qt实现小闹钟

weight.cpp

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //实例化
    timer = new QTimer(this);
    lab1 = new QLabel(this);
    edit = new QLineEdit(this);
    text1 = new QTextEdit(this);
    speecher = new QTextToSpeech(this);
    //界面
    this->setFixedSize(620,380);
    //时钟
    lab1->resize(385,80);
    lab1->move(30,30);
    //行编辑器
    edit->resize(165,35);
    edit->move(430,30);
    //文本
    text1->resize(570,220);
    text1->move(30,150);
    text1->setText("三更灯火五更鸡\n正是男儿读书时\n黑发不知勤学早\n白首方悔读书迟");
    QFont font("Arial", 20);
    text1->setFont(font);
    //启动停止按钮
    btn1 = new QPushButton("启动",this);
    btn2 = new QPushButton("停止",this);
    btn1->resize(80,30);
    btn2->resize(80,30);
    btn1->move(430,70);
    btn2->move(510,70);
    //启动定时器
    timer->start(1000);
    //连接槽函数
    connect(timer, &QTimer::timeout,this,&Widget::flag_slot);
    connect(timer,&QTimer::timeout,this,&Widget::lab1_slot);
    connect(this->btn1,&QPushButton::clicked,this,&Widget::on_btn1_clicked);
    connect(this, &Widget::singal_1, this, &Widget::singal_1_slot);
    connect(this->btn2,&QPushButton::clicked,this,&Widget::on_btn2_clicked);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::lab1_slot()
{
    //调用Qtime 的静态成员函数,获取当前系统时间
    QTime sys_time = QTime::currentTime();

    //将时间类对象调用函数转换为字符串
    QString t = sys_time.toString("hh:mm:ss");
    lab1->setText(t);
    lab1->setAlignment(Qt::AlignCenter);                //文本对齐居中
    lab1->setFont(QFont("微软雅黑",20));
}
//启动闹钟
void Widget::on_btn1_clicked()
{
    flag1 = 1;
}
//停止闹钟
void Widget::on_btn2_clicked()
{

    flag1 = 0;
    speecher->stop();
}
//判断时间
void Widget::singal_1_slot()
{
    QString t = text1->toPlainText();
        if(lab1->text() == edit->text())
        {
            speecher->say(t);
        }
}
//发送信号
void Widget::flag_slot()
{
    if(flag1==1)
            emit singal_1();
}

weight.h

#ifndef WIDGET_H
#define WIDGET_H

#include                            //QWidget类所在的头文件,父类头文件
#include
#include 
#include                        //按钮类头文件
#include                             //输出类头文件
#include                          //行编辑器头文件
#include                             //标签头文件
#include "ui_widget.h"                       //ui界面对应的头文件
#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();

signals:
    void singal_1();
private slots:
    void lab1_slot();
    void on_btn1_clicked();
    void on_btn2_clicked();
    void singal_1_slot();
    void flag_slot();


private:
    Ui::Widget *ui;
    QLabel *lab1;
    QLineEdit *edit;
    QPushButton *btn1;
    QPushButton *btn2;
    QTextEdit *text1;
    QTimer *timer;               
    QTextToSpeech *speecher;
    int flag1=0;
};
#endif // WIDGET_H

你可能感兴趣的:(qt,开发语言)