qt-作业

该代码段展示了一个基于Qt的Widget应用程序,它包含两个按钮(btn1和btn2)、一个文本框和一个时间显示标签。当btn1被点击时,启动闹钟功能,禁用编辑字段并启用btn2。btn2用于关闭闹钟。使用QTimer每秒更新本地时间,并在时间匹配预设的闹钟时间时,通过QTextToSpeech模块朗读文本框的内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include "widget.h"
#include "ui_widget.h"
 
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
 
    speech = new QTextToSpeech(this);       //实例化播报员
 
    //开始先关闭按钮2
    ui->btn2->setDisabled(true);
 
    //给定时器指针实例化空间
    timer = new QTimer(this);
 
    connect(timer,&QTimer::timeout,this,&Widget::on_timeout_slot);
 
    timer->start(1000);     //当启动定时器后,会每隔1000ms,自动发射timeout信号
 
}
 
Widget::~Widget()
{
    delete ui;
}
 
 
void Widget::on_btn1_clicked()
{
 
    //执行启动功能
    
    //把文本框设置为不可以修改
    ui->alarmtext->setDisabled(true);
    ui->alarmtime->setDisabled(true);
 
    //将按钮1设置为不可用
    ui->btn1->setDisabled(true);
 
    //将按钮2设置为可用
    ui->btn2->setEnabled(true);
 
}
 
void Widget::on_btn2_clicked()
{
    //执行关闭功能
    //把文本框设置为可以修改
    ui->alarmtext->setEnabled(true);
    ui->alarmtime->setEnabled(true);
    //将按钮2设置为不可用
    ui->btn2->setDisabled(true);
 
    //将按钮1设置为可用
    ui->btn1->setEnabled(true);
}
 
//处理timeout信号对应的槽函数
void Widget::on_timeout_slot()
{
    QTime t = QTime::currentTime();         //获取系统时间
 
    t1 = t.toString("hh:mm:ss");    //将系统时间转换为字符串
 
    ui->localtimeLab->setText(t1);   //将时间设置到ui上
    
    //判断闹钟时间是否与本地时间相等
    if(t1 == ui->alarmtime->text())
    {
        speech->say(ui->alarmtext->toPlainText());  //播放文本上的内容
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值