10.10每日作业
时钟
#include <QPainter>
#include <QTimerEvent>
#include <QTime>
#include <widget.h>
ClockWidget::ClockWidget(QWidget *parent) : QWidget(parent) {startTimer(1000); // 每秒更新一次
}void ClockWidget::paintEvent(QPaintEvent *event) {Q_UNUSED(event);QPainter painter(this);drawClock(painter);
}void ClockWidget::timerEvent(QTimerEvent *event) {Q_UNUSED(event);update(); // 触发重绘
}void ClockWidget::drawClock(QPainter &painter) {// 绘制圆形时钟int side = qMin(width(), height());painter.setRenderHint(QPainter::Antialiasing);painter.translate(width() / 2, height() / 2);painter.scale(side / 200.0, side / 200.0);// 绘制时钟边框painter.setPen(Qt::black);painter.drawEllipse(-90, -90, 180, 180);// 绘制刻度和数字painter.setPen(Qt::black);QFont font = painter.font();font.setPointSize(12); // 设置字体大小painter.setFont(font);for (int i = 1; i <= 12; ++i) {painter.save();painter.rotate(i * 30); // 每个刻度30度painter.drawLine(0, -90, 0, -80); // 刻度线从-90到-80// 绘制数字painter.drawText(-10, -75, QString::number(i)); // 在刻度位置绘制数字painter.restore();}// 获取当前时间QTime time = QTime::currentTime();int hour = time.hour() % 12;int minute = time.minute();int second = time.second();// 绘制时针painter.setPen(Qt::black);painter.save();painter.rotate(30.0 * hour + 0.5 * minute); // 每小时30度,每分钟0.5度painter.drawLine(0, 0, 0, -40);painter.restore();// 绘制分针painter.setPen(Qt::blue);painter.save();painter.rotate(6.0 * minute + 0.1 * second); // 每分钟6度,每秒0.1度painter.drawLine(0, 0, 0, -60);painter.restore();// 绘制秒针painter.setPen(Qt::red);painter.save();painter.rotate(6.0 * second); // 每秒6度painter.drawLine(0, 0, 0, -80);painter.restore();
}
闹钟播报
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);speecher = new QTextToSpeech(this);timer1 = new QTimer(this);timer2 = new QTimer(this);timer1->start(1000);connect(timer1, &QTimer::timeout, this, &Widget::timeout_slot1);connect(timer2, &QTimer::timeout, this, &Widget::timeout_slot2);}Widget::~Widget()
{delete ui;
}void Widget::timeout_slot1()
{QTime sysTime = QTime::currentTime();QString t = sysTime.toString("hh:mm:ss");ui->curtimelab->setText(t);
}
void Widget::timeout_slot2()
{if(ui->curtimelab->text()==ui->settimeEdit->text()){qDebug()<<"时间到";QString msg ="现在是北京时间"+ui->settimeEdit->text()+ui->textEdit->toPlainText();speecher->say(msg);timer2->stop();ui->startButton->setDisabled(false);ui->textEdit->setDisabled(false);}
}void Widget::on_startButton_clicked()
{timer2->start(1000);ui->startButton->setDisabled(true);ui->textEdit->setDisabled(true);}void Widget::on_closeButton_clicked()
{timer2->stop();ui->startButton->setDisabled(false);ui->textEdit->setDisabled(false);}
server
#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() == "启动服务器"){quint16 port = ui->portEdit->text().toUInt();if(!server->listen(QHostAddress::Any,port)){QMessageBox::information(this,"提示","服务器启动失败");return ;}else{QMessageBox::information(this,"提示","服务器启动成功");}ui->serverBtn->setText("关闭服务器");}else{server->close();ui->serverBtn->setText("启动服务器");}
}void Widget::newConnection_slot()
{//QMessageBox::information(this,"提示","有新客户发来连接请求了");qDebug() << "有新客户端发来连接请求";QTcpSocket *s = server->nextPendingConnection();clientList.push_back(s);connect(s,&QTcpSocket::readyRead,this,&Widget::readyRead_slot);}void Widget::readyRead_slot()
{for(auto val:clientList){if(val->bytesAvailable()){QByteArray msg = val->readAll();ui->listWidget->addItem(QString::fromLocal8Bit(msg));for(auto key:clientList){key->write(msg);}}}
}
client
#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);ui->connectBtn->setEnabled(true);ui->disconnectBtn->setEnabled(false);
}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);
}void Widget::connected_slot()
{QMessageBox::information(this,"提示","连接服务器成功");ui->connectBtn->setEnabled(false);ui->disconnectBtn->setEnabled(true);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()
{client->disconnectFromHost();
}void Widget::disconnected_slot()
{QMessageBox::information(this,"提示","断开成功");ui->connectBtn->setEnabled(true);ui->disconnectBtn->setEnabled(false);
}