QT_day5

client

cpp 复制代码
//widget.cpp
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
    , client(new QTcpSocket(this))
    , time(QTime::currentTime())
{
    ui->setupUi(this);
    ui->discon_btn->setEnabled(false);
    ui->msg_edit->setEnabled(false);
    ui->send_btn->setEnabled(false);

    ui->user_edit->setText("huang");
    ui->ip_edit->setText("127.0.0.1");
    ui->port_edit->setText("9999");

    connect(client,&QTcpSocket::connected,this,&Widget::connect_slot);

    connect(client,&QTcpSocket::readyRead,this,&Widget::recv_slot);

     connect(client,&QTcpSocket::disconnected,this,&Widget::disconnect_slot);
}

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


void Widget::on_con_btn_clicked()
{
    username = ui->user_edit->text();
    client->connectToHost(ui->ip_edit->text(),ui->port_edit->text().toUInt());
    ui->discon_btn->setEnabled(true);
    ui->msg_edit->setEnabled(true);
    ui->send_btn->setEnabled(true);
    ui->user_edit->setEnabled(false);
    ui->ip_edit->setEnabled(false);
    ui->port_edit->setEnabled(false);
}

void  Widget::connect_slot()
{
    QString msg = username+"进入聊天室";
    client->write(msg.toLocal8Bit());
}

void Widget::on_send_btn_clicked()
{
    QString msg = ui->msg_edit->text();
    msg = username +"\n"+ msg;
    client->write(msg.toLocal8Bit());
    ui->msg_edit->clear();
}

void Widget::recv_slot()
{
    QByteArray msg = client->readAll();
    QString m = QString::fromLocal8Bit(msg);
    //收到信息格式为  username   \n  msg
    QStringList list = m.split("\n");

    time = QTime::currentTime();
    QString t = time.toString("HH:mm:ss");
    m = t +":"+ m;
    //判断姓名和当前相同设置到右边显示
    if(list.at(0) == username)
    {
        QListWidgetItem* item = new QListWidgetItem(m);
        item->setTextAlignment(Qt::AlignRight);
        ui->listWidget->addItem(item);

    }else
    {
        ui->listWidget->addItem(m);
    }


}
//点击断开连接,调用disconnectFromHost
void Widget::on_discon_btn_clicked()
{
    QString msg = username+"离开聊天室";
    client->write(msg.toLocal8Bit());
    client->disconnectFromHost();
}
//调用disconnectFromHost后执行的槽函数
void Widget::disconnect_slot()
{
    ui->discon_btn->setEnabled(false);
    ui->msg_edit->setEnabled(false);
    ui->send_btn->setEnabled(false);
    ui->user_edit->setEnabled(true);
    ui->ip_edit->setEnabled(true);
    ui->port_edit->setEnabled(true);
}


//main.cpp
#include "widget.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}




//widget.h
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTcpSocket>
#include <QTime>
#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_con_btn_clicked();
    void connect_slot();
    void on_send_btn_clicked();
    void recv_slot();

    void on_discon_btn_clicked();
    void disconnect_slot();
private:
    Ui::Widget *ui;
    QString username;
    QTcpSocket *client;
    QTime time;
    int itemNum;
};
#endif // WIDGET_H

server

cpp 复制代码
//widget.cpp
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
    , server(new QTcpServer(this))
    , time(QTime::currentTime())
{
    ui->setupUi(this);
}

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



void Widget::on_run_btn_clicked()
{
    if(ui->port_edit->text().toUInt()==0)
    {
        QMessageBox::information(this,"","请输入端口号",QMessageBox::Ok);
        return;
    }
    bool ok = server->listen(QHostAddress::Any,ui->port_edit->text().toUInt());
    if(ok)
    {
        QMessageBox::information(this,"","开启成功",QMessageBox::Ok);
        connect(server,&QTcpServer::newConnection,this,&Widget::connect_slot);

    }else
    {
        QMessageBox::information(this,"","开启失败",QMessageBox::Ok);
        return;
    }

}

void Widget::connect_slot()
{
    QTcpSocket* client = server->nextPendingConnection();
    inlinelist.push_back(client);
    connect(client,&QTcpSocket::readyRead,this,&Widget::tran_slot);
}

void Widget::tran_slot()
{
    for(int i = 0;i<inlinelist.count();i++)
    {
        if(inlinelist.at(i)->state()==0)
        {
            inlinelist.removeAt(i);
        }
    }

    for(int i=0;i<inlinelist.count();i++)
    {
        if(inlinelist.at(i)->bytesAvailable()!=0)
        {
            //记录信息发送时间
            time.currentTime();
            QString t = time.toString("HH:mm:ss");
            //读取消息
            QByteArray msg =  inlinelist.at(i)->readAll();
            QString m = QString::fromLocal8Bit(msg);
            //拼接时间和信息
            m = "("+t+")"+":"  + m;
            ui->listWidget->addItem(m);
            for(int j = 0;j<inlinelist.count();j++)
            {
                inlinelist.at(j)->write(msg);
            }
        }
    }
}
//main.cpp
#include "widget.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}



//widget.h
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTcpServer>
#include <QMessageBox>
#include <QList>
#include <QTcpSocket>
#include <QTime>
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_run_btn_clicked();
    void connect_slot();
    void tran_slot();
private:
    Ui::Widget *ui;
    QTcpServer *server;
    QList<QTcpSocket*> inlinelist;
    QTime time;
};
#endif // WIDGET_H
相关推荐
乌啼霜满天2496 分钟前
JDBC编程---Java
java·开发语言·sql
色空大师19 分钟前
23种设计模式
java·开发语言·设计模式
Bruce小鬼31 分钟前
QT文件基本操作
开发语言·qt
2202_7544215437 分钟前
生成MPSOC以及ZYNQ的启动文件BOOT.BIN的小软件
java·linux·开发语言
我只会发热44 分钟前
Java SE 与 Java EE:基础与进阶的探索之旅
java·开发语言·java-ee
懷淰メ1 小时前
PyQt飞机大战游戏(附下载地址)
开发语言·python·qt·游戏·pyqt·游戏开发·pyqt5
hummhumm1 小时前
第 22 章 - Go语言 测试与基准测试
java·大数据·开发语言·前端·python·golang·log4j
宁静@星空1 小时前
006-自定义枚举注解
java·开发语言
hummhumm1 小时前
第 28 章 - Go语言 Web 开发入门
java·开发语言·前端·python·sql·golang·前端框架
武子康2 小时前
Java-07 深入浅出 MyBatis - 一对多模型 SqlMapConfig 与 Mapper 详细讲解测试
java·开发语言·数据库·sql·mybatis·springboot