10.10 Qt作业

Tcp通信

头文件

#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_disconnetBtn_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);
}

void Widget::connected_slot()
{
    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_disconnetBtn_clicked()
{
    QString msg = userName + ":离开聊天室";

    client->write(msg.toLocal8Bit());

    client->disconnectFromHost();

}

void Widget::disconnected_slot()
{
    QMessageBox::information(this,"提示","断开成功");
}

绘制时钟

头文件

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QPainter>
#include <QPixmap>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

    void paintEvent(QPaintEvent *event) override;

private:
    Ui::Widget *ui;
    QTimer *timer;

    int count = 0;
    int hour;
    int minute;
    int second;

};
#endif // WIDGET_H

源文件

#include "widget.h"
#include "ui_widget.h"


#include <QDateTime>
#include <QTimer>
#include <QDebug>


Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    timer = new QTimer;

    timer->start(1000);

    connect(timer,&QTimer::timeout,[&](){count++;update(); });

    QDateTime sysdt = QDateTime::currentDateTime();
    QString t = QTime::currentTime().toString("h:m:s ap");
    QStringList list1 = t.split(" ");
    QStringList list2 = list1[0].split(":");

    hour = list2[0].toUInt();
    minute = list2[1].toUInt();
    second = list2[2].toUInt();

     qDebug()<<hour<<":"<<minute<<":"<<second;
}

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

void Widget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);


       QPen pen;
       pen.setStyle(Qt::SolidLine);
       pen.setColor(QColor("pink"));
       pen.setWidth(3);
       QBrush brush("yellow");

       painter.setPen(pen);
       painter.setBrush(brush);


       painter.translate(this->width()/2,this->height()/2);

       painter.drawEllipse(QPoint(0,0), 200, 200);


       for(int i=0;i<60;i++)
       {
           painter.rotate(6);
           painter.drawLine(QPoint(200,0),QPoint(195,0));
       }
       pen.setWidth(5);
       painter.setPen(pen);
       for(int i=0;i<12;i++)
       {
           painter.drawLine(QPoint(200,0),QPoint(190,0));
           painter.rotate(30);
           painter.drawText(QPoint(0,-170),QString::number(i+1));
       }


       pen.setWidth(10);
       pen.setColor(QColor("black"));
       painter.setPen(pen);
       painter.rotate(hour*30+6*second/60/12+30*minute/60+6*count/60/12);
       painter.drawLine(QPoint(0,-50),QPoint(0,5));




       QPainter painter2(this);
       painter2.translate(this->width()/2,this->height()/2);
       pen.setWidth(6);
       pen.setColor(QColor("skyblue"));
       painter2.setPen(pen);
       painter2.rotate(6*count/60+minute*6+6*second/60);
       painter2.drawLine(QPoint(0,-80),QPoint(0,8));


       QPainter painter3(this);
       painter3.translate(this->width()/2,this->height()/2);
       pen.setWidth(3);
       pen.setColor(QColor("grey"));
       painter3.setPen(pen);
       painter3.rotate(6*count+second/60);
       painter3.drawLine(QPoint(0,-120),QPoint(0,12));

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值