day5QT套接字通信

Widget.cpp

cs 复制代码
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    objtimer = new QTimer (this);
    //连接定时器的timeout信号到启动的槽函数
    //connect(objtimer,&QTimer::timeout,this,&Widget::timeout_slto);
    sp =new QTextToSpeech(this);
    //初始化定时器id
    timerId = -1;

}

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


void Widget::on_pushButton_clicked()
{
    if(timerId==-1){
   timerId = startTimer(1000);
}
}

/*void Widget::timeout_slto()
{
    QTime sysTime = QTime::currentTime();
    QString time = sysTime.toString("hh:mm:ss");
    ui->label->setText(time);

}*/
//定时器处理函数的实现
void Widget::timerEvent(QTimerEvent *t)
{
    if(t->timerId() == timerId){
    QTime sytime = QTime::currentTime();
    QString t2 =sytime.toString("hh:mm:ss");
    ui->label->setText(t2);//更新标签显示时间

    QString  time1 = ui->lineEdit->text();
    if(t2==time1)
    {
        sp->say("还睡呢,收你们来了");
    }
}
}
void Widget::on_pushButton_2_clicked()
{
    //objtimer->stop();
    killTimer(timerId);
}

Widget.h

cs 复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include<QTime>
#include<QTimer>
#include<QTimerEvent>
#include<QTextToSpeech>


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 *t) override;
private slots:
    void on_pushButton_clicked();
    //void timeout_slto();

    void on_pushButton_2_clicked();



private:
    Ui::Widget *ui;

    //定义一个定时器指针
    QTimer *objtimer;
    //定义一个播报员指针

    QTextToSpeech *sp;
    int timerId;

};
#endif // WIDGET_H
相关推荐
小飞在努力5 小时前
【实现负载均匀在线判题③】从零散模块到统一,编译运行全链路封装
c语言·开发语言·c++·笔记
星恒随风9 小时前
从0实现C++ AI大模型接入SDK(四):大模型API接入、公共数据结构设计与日志库封装
linux·服务器·数据结构·c++·人工智能
炸膛坦客9 小时前
单片机/C/C++八股:(三十四)C 语言函数参数入栈顺序
c语言·开发语言·c++
码途行者10 小时前
每天10分钟学Go:第20课 泛型
c++·算法·golang
AIgorithmGEEK11 小时前
算法·哈希表|从“逐个比”到“按 key 算位置”
数据结构·c++·算法·leetcode·hash
星恒随风13 小时前
从0实现C++ AI大模型接入SDK(三):环境搭建与ChatSDK快速上手——实现我们的第一个AI聊天程序
linux·开发语言·c++·人工智能·笔记
h_a_o777oah14 小时前
【数据结构】FHQ Treap:无旋转的平衡 BST
数据结构·c++·算法·acm·平衡树·treap·fhq treap
撸码猿14 小时前
Ladybird 进入 alpha:从零写的浏览器引擎现在能下载了
c++·rust·开源·浏览器·ladybird
ShineWinsu16 小时前
对于Qt:重写事件步骤的解析
开发语言·c++·qt·客户端·事件·鼠标事件·步骤
爱和冰阔落17 小时前
【Linux网络】HTTP 服务器怎么返回网页和图片?从 404、301 到 Content-Type 与静态资源完整实战
linux·c++·http·网络编程·web服务器