闹钟
头文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTimer>
#include <QTextToSpeech> // 添加此行以引入QTextToSpeech类
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
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_NAMESPACE
class weight : public QMainWindow
{
Q_OBJECT
public:
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,"提示","断开成功");
}