【qtday4】作业

该代码实现了一个基于Qt的无边框窗口应用,功能包括显示当前时间、启动和停止定时器以及使用QTextToSpeech进行语音报时。当用户设定的时间与当前时间匹配时,程序会读出时间。
摘要由CSDN通过智能技术生成

1.设置闹钟在这里插入图片描述
widget.h:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QPainter>
#include <QTimer>
#include <QTime>
#include <QPushButton>
#include <QDebug>
#include <QtTextToSpeech/QTextToSpeech>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget();
    //重写绘制事件
    void paintEvent(QPaintEvent *event) override;

private slots:
    void on_startButton_clicked();
    void timeout_slot();

    void on_stopButton_clicked();

private:
    Ui::Widget *ui;
    //定义一个定时器变量
    QTimer *obj_timer;
    QTextToSpeech *speecher;
};

#endif // WIDGET_H

widget.cpp:

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

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setWindowFlag(Qt::FramelessWindowHint);
    this->setFixedSize(1080/1.5,568/1.5);
    //设置定时器
    obj_timer = new QTimer(this);
    //设置信号处理函数
    connect(obj_timer,&QTimer::timeout,this,& Widget::timeout_slot);
    speecher = new QTextToSpeech(this);
}

Widget::~Widget()
{
    delete ui;
}
void Widget::paintEvent(QPaintEvent *event)
{
     QPainter p(this);
     p.drawPixmap(this->rect(),QPixmap("E:\\qt\\qtday4\\tiger.jpg"));
}

void Widget::on_startButton_clicked()
{
    ui->setlineEdit->setEnabled(false);
    ui->startButton->setEnabled(false);
    ui->stopButton->setEnabled(true);
    obj_timer->start(1000);
}
void Widget::timeout_slot()
{
    ui->label->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
//    static int num = 1;
//    ui->objLab->setNum(num++);
    //调用时间类的静态成员函数获取当前的系统时间
    QTime sys_time = QTime::currentTime();
//    int h = sys_time.hour();
//    int m = sys_time.minute();
//    int s = sys_time.second();
    //调用成员函数将时间转换成字符串
    QString t = sys_time.toString("hh:mm:ss");
    ui->label->setText(t);
    if(ui->setlineEdit->text()==ui->label->text())
    {
        speecher->say(ui->label_2->text());
        ui->startButton->setEnabled(true);
        ui->stopButton->setEnabled(false);
        ui->setlineEdit->setEnabled(true);
        obj_timer->stop();
    }
}


void Widget::on_stopButton_clicked()
{
    obj_timer->stop();

    ui->startButton->setEnabled(true);
    ui->stopButton->setEnabled(false);
    ui->setlineEdit->setEnabled(true);
}

main.cpp:

#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

输出:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值