您的位置:首页 > 游戏 > 手游 > 优秀网络专题内容策划分享_广州冼村为什么叫土豪村_齐三seo顾问_seo外链代发

优秀网络专题内容策划分享_广州冼村为什么叫土豪村_齐三seo顾问_seo外链代发

2024/12/23 11:58:34 来源:https://blog.csdn.net/yangh1231/article/details/142811578  浏览:    关键词:优秀网络专题内容策划分享_广州冼村为什么叫土豪村_齐三seo顾问_seo外链代发
优秀网络专题内容策划分享_广州冼村为什么叫土豪村_齐三seo顾问_seo外链代发

闹钟 

头文件 

#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QTimer>
#include <QTextToSpeech>  // 添加此行以引入QTextToSpeech类QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();void timerEvent(QTimerEvent *event) override;int eventTimerId;int clockTimerId;  private slots:void on_btn1_clicked();void timeout_slot();void on_btn2_clicked();private:Ui::MainWindow *ui;QTimer *objTimer;QTextToSpeech *tts;  // 声明QTextToSpeech对象
};#endif // MAINWINDOW_H

源文件 

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>
#include <QDebug>
#include <QMessageBox>
#include <QTime>
#include <QTimerEvent>
#include <QDateTime>
#include <QTextToSpeech>MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);objTimer = new QTimer(this);connect(objTimer, &QTimer::timeout, this, &MainWindow::timeout_slot);tts = new QTextToSpeech(this);  // 初始化QTextToSpeech
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::timeout_slot()
{QTime systime = QTime::currentTime();QString t = systime.toString();
}void MainWindow::on_btn2_clicked()
{if (ui->btn2->text() == "启动"){eventTimerId = this->startTimer(1000);ui->btn2->setText("关闭");}else{this->killTimer(eventTimerId);ui->btn2->setText("启动");}
}void MainWindow::timerEvent(QTimerEvent *event)
{if (event->timerId() == eventTimerId){QDateTime sysdatatime = QDateTime::currentDateTime();QString d = sysdatatime.toString("yyyy-MM-dd  hh:mm:ss");ui->label_2->setText(d);}if (event->timerId() == clockTimerId){if (ui->lineEdit->text() == ui->label_2->text()){QString msg = "现在是北京时间 " + ui->label_2->text() + " 主人,可以起床了";tts->say(msg);  // 使用QTextToSpeech播放语音this->killTimer(clockTimerId);ui->btn1->setEnabled(true);}}
}void MainWindow::on_btn1_clicked()
{clockTimerId = this->startTimer(1000);  // 每隔1000毫秒,系统会自动调用timerEvent函数ui->btn1->setEnabled(false);
}

 客户端

头文件 

#ifndef WEIGHT_H
#define WEIGHT_H
#include<QTcpSocket>
#include <QMainWindow>QT_BEGIN_NAMESPACE
namespace Ui { class weight; }
QT_END_NAMESPACEclass weight : public QMainWindow
{Q_OBJECTpublic:weight(QWidget *parent = nullptr);~weight();private slots:void on_connectbtn_clicked();void connected_slot();void readyRead_slot();void disconnected_slot();void on_sendbtn_clicked();void on_releasebtn_clicked();private:Ui::weight *ui;QTcpSocket *client;QString userName;
};
#endif // WEIGHT_H

源文件 

#include "weight.h"
#include "ui_weight.h"
#include<QMessageBox>
weight::weight(QWidget *parent): QMainWindow(parent), ui(new Ui::weight)
{ui->setupUi(this);client =new QTcpSocket(this);connect(client,&QTcpSocket::connected,this,&weight::connected_slot);connect(client,&QTcpSocket::readyRead,this,&weight::readyRead_slot);connect(client,&QTcpSocket::disconnected,this,&weight::disconnected_slot);
}weight::~weight()
{delete ui;
}void weight::on_connectbtn_clicked()
{userName=ui->nameedit->text();QString ip=ui->ipedit->text();quint16 port=ui->portedit->text().toUInt();client->connectToHost(ip,port);
}
void weight::connected_slot()
{QMessageBox::information(this,"提示","连接成功");QString msg=userName+"进入聊天室";client->write(msg.toLocal8Bit());}void weight::readyRead_slot()
{QByteArray msg=client->readAll();ui->listWidget->addItem(QString::fromLocal8Bit(msg));
}void weight::on_sendbtn_clicked()
{QString msg=userName+": "+ui->lineEdit->text();client->write(msg.toLocal8Bit());ui->lineEdit->clear();
}void weight::on_releasebtn_clicked()
{QString msg=userName+":离开聊天室";client->write(msg.toLocal8Bit());client->disconnectFromHost();
}
void weight::disconnected_slot()
{QMessageBox::information(this,"提示","断开成功");
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com