闹钟
pro文件:
头文件:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QTimer>
#include<QTime>
#include<QDebug>
#include<QTimerEvent>
#include<QDateTime>
#include<QTextToSpeech>
#include<QMessageBox>
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 *event);
private slots:
void on_startBtn_clicked();
void on_closeBtn_clicked();
void updateTime();
private:
Ui::Widget *ui;
QTextToSpeech *speecher;
QTimer *timer1;
int clockTimerID;
int eventTimerID;
};
#endif // WIDGET_H
源代码:
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
timer1 =new QTimer(this);
speecher =new QTextToSpeech(this);
connect(timer1,&QTimer::timeout,this,&Widget::updateTime);
timer1->start(1000);
updateTime();
}
Widget::~Widget()
{
delete ui;
}
void Widget::updateTime()
{
QTime sysTime =QTime::currentTime();
QString t=sysTime.toString("hh:mm:ss");
ui->timeLab->setText(t);
}
void Widget::timerEvent(QTimerEvent *event)
{
if(event->timerId()==clockTimerID)
{
if(ui->timeEdit->text()==ui->timeLab->text())
{
QString msg=ui->listenEdit->text();
speecher->say(msg);
}
}
}
void Widget::on_startBtn_clicked()
{
clockTimerID=this->startTimer(1000);
ui->startBtn->setEnabled(false);
ui->timeEdit->setEnabled(false);
}
void Widget::on_closeBtn_clicked()
{
ui->timeEdit->setText("");
this->killTimer(clockTimerID);
ui->startBtn->setEnabled(true);
ui->timeEdit->setEnabled(true);
}
聊天室的完善
客户端、服务器的pro文件:
客户端:
头文件:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QTcpSocket>
#include<QMessageBox>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_connectBtn_clicked();
void connected_slot();
void readyRead_slot();
void on_sendBtn_clicked();
void on_disconnectBtn_clicked();
void disconnected_slot();
private:
Ui::Widget *ui;
QTcpSocket *client;
QString userName;
};
#endif // WIDGET_H
源文件:
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//实例化一个客户端对象
client=new QTcpSocket(this);
//
connect(client,&QTcpSocket::connected,this,&Widget::connected_slot);
//
connect(client,&QTcpSocket::readyRead,this,&Widget::readyRead_slot);
connect(client,&QTcpSocket::disconnected,this,&Widget::disconnected_slot);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_connectBtn_clicked()
{
userName = ui->nameEdit->text();
QString ip=ui->ipEdit->text();
quint16 port =ui->portEdit->text().toUInt();
client->connectToHost(ip,port);
//程序执行至此,表示已经向多个服务器发送了连接请求如果连接成功服务器
//那么该套接字就会自动发射一个connected的信号,我们可以将该信号连接到自定义的槽函数中处理逻辑
//由于只需要连接一次,所以,可以写在构造函数中
}
void Widget::connected_slot()
{
ui->nameEdit->setEnabled(false);
ui->ipEdit->setEnabled(false);
ui->portEdit->setEnabled(false);
ui->connectBtn->setEnabled(false);
QMessageBox::information(this,"提示","连接服务器成功");
QString msg = userName +":进入聊天室";
client->write(msg.toLocal8Bit());
}
void Widget::readyRead_slot()
{
QByteArray msg =client->readAll();
ui->listWidget->addItem(QString::fromLocal8Bit(msg));
}
void Widget::on_sendBtn_clicked()
{
QString msg =userName + ":" + ui->msgEdit->text();
client->write(msg.toLocal8Bit());
ui->msgEdit->clear();
}
void Widget::on_disconnectBtn_clicked()
{
QString msg = userName +":离开聊天室";
client->write(msg.toLocal8Bit());
ui->nameEdit->setEnabled(true);
ui->ipEdit->setEnabled(true);
ui->portEdit->setEnabled(true);
ui->connectBtn->setEnabled(true);
client->disconnectFromHost();
}
void Widget::disconnected_slot()
{
QMessageBox::information(this,"提示","客户端断开连接");
}
服务器:
头文件:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QTcpServer> //服务器类
#include<QTcpSocket> //客户端类
#include<QList> //链表容器,用于存储连接过来的客户端
#include<QMessageBox>
#include<QDebug>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_serverBtn_clicked();
void newConnection_slot();
void readyRead_slot();
private:
Ui::Widget *ui;
//定义服务器指针
QTcpServer *server;
//定义客户端容器
QList<QTcpSocket *> clientList;
};
#endif // WIDGET_H
源文件:
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//实例化一个服务器对象
server=new QTcpServer(this);
connect(server,&QTcpServer::newConnection,this,&Widget::newConnection_slot);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_serverBtn_clicked()
{
if(ui->serverBtn->text() == "启动服务器")
{
//获取ui界面上的端口号
quint16 port =ui->portEdit->text().toUInt();
//启动监听
if(!server->listen(QHostAddress::Any,port))
{
QMessageBox::information(this,"提示","服务器启动失败");
return ;
}else
{
QMessageBox::information(this,"提示","服务器启动成功");
}
ui->portEdit->setEnabled(false);
ui->serverBtn->setText("关闭服务器");
}else if(ui->serverBtn->text()=="关闭服务器")
{
server->close();
ui->portEdit->setEnabled(true);
ui->serverBtn->setText("启动服务器");
}
}
void Widget::newConnection_slot()
{
//QMessageBox::information(this,"提示","有新客户发来连接请求");
qDebug()<<"有新客服端发来请求";
//获取最新连接的客户端套接字
//函数原型:QTcpSocket *nextPendingConnection();
//参数:无
//返回值:最新连接的套接字地址
QTcpSocket *s=server->nextPendingConnection();
//将该套接字放入客户端链表
clientList.push_back(s);
connect(s,&QTcpSocket::readyRead,this,&Widget::readyRead_slot);
}
void Widget::readyRead_slot()
{
//循环遍所有客户端,查看哪个客户端中有消息待读
for(auto val:clientList)
{
//判断当前客户端中是否有消息待读
//函数原型:qint64 bytesAvailable() const override;
//参数:无
//返回值:返回当前套接字中能被读取出来的字符个数
if(val->bytesAvailable())
{
QByteArray msg=val->readAll();
ui->listWidget->addItem(QString::fromLocal8Bit(msg));
for(auto key:clientList)
{
key->write(msg);
}
}
}
}