QT_day4

实现闹钟

QT_day4_第1张图片

.cpp

#include "widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    //设置窗体
    this->setFixedSize(800,400); //窗体大小
    this->setWindowTitle("闹钟"); //窗体名称
    
    //构造消息框
    t1_Edit = new QLineEdit(this);  //设定的时间
    t1_Edit->resize(300, 80);
    t1_Edit->move( 450,30);
    t1_Edit->setStyleSheet("background-color:skyblue");
    t1_Edit->setEnabled(false);
    
    t2_Label = new QLabel(this); 
    t2_Label->resize(400, 140);
    t2_Label->move( 20,30);
    t2_Label->setStyleSheet("background-color:gray");
    
    msg_Edit = new QLineEdit(this);
    msg_Edit->resize(750,180);
    msg_Edit->move(20, 200);
    msg_Edit->setEnabled(false);
    
    
    btn1 = new QPushButton(this); //启动按钮
    btn1->resize(100,50);
    btn1->move(450, 120);
    btn1->setText("启动");
    btn1->setStyleSheet("background-color:pink");
    
    btn2 = new QPushButton(this); //关闭按钮
    btn2->resize(100,50);
    btn2->move(650, 120);
    btn2->setText("关闭");
    btn2->setStyleSheet("background-color:red");
    btn2->setEnabled(false);
    
    t1 = new QTimer(this);
    
    connect(t1, &QTimer::timeout, this, &Widget::timeout_slot);
    connect(btn1, &QPushButton::clicked, this, &Widget::btn1_slot);
    connect(btn2, &QPushButton::clicked, this, &Widget::btn2_slot);
}

Widget::~Widget()
{
    
}

//启动按钮槽函数
void Widget::btn1_slot()
{
    t1_Edit->setEnabled(true);
    btn2->setEnabled(true);
    btn1->setEnabled(false);
    msg_Edit->setEnabled(true);
    
    t1->start(1000); //启动一个定时器
    
}

void Widget::btn2_slot()
{
    btn2->setEnabled(false);
    t1_Edit->setEnabled(false);
    btn1->setEnabled(true);
    msg_Edit->setEnabled(false);
    t1->stop();
    t2_Label->clear();
}

//处理timeout槽函数
void Widget::timeout_slot()
{
    //获取系统当前时间
    QTime sys_time = QTime::currentTime();
    
    //将QTime转换成字符串
    QString time = sys_time.toString("hh:mm:ss");
    
    //设置内容剧中显示
    t2_Label->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    
    //将时间展示到t2界面
    t2_Label->setText(time);
    
    if(t2_Label->text() == t1_Edit->text())
    {
        speaker= new QTextToSpeech(this);
        speaker->say(t1_Edit->text());
        
    }
}

.h 

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include          //定时器类头文件
#include          //时间类头文件
#include    //定时器事件处理类
#include 


class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
    ~Widget();

    QPushButton *btn1;  //启动按钮
    QPushButton *btn2;  //停止按钮
    QLineEdit *t1_Edit;  //设定时间框
    QLabel *t2_Label;  //系统时间框
    QLineEdit *msg_Edit;  //消息框
    QTextToSpeech *speaker;


    void btn1_slot();
    void btn2_slot();

    QTimer *t1;
    int tid;

public slots:
    void timeout_slot();   //自定义处理timeout信号的槽函数
    void on_objStartBtn_clicked();

};

#endif // WIDGET_H

QT_day4_第2张图片

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