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
相关推荐
娅娅梨21 分钟前
C++ 错题本--not found for architecture x86_64 问题
开发语言·c++
汤米粥27 分钟前
小皮PHP连接数据库提示could not find driver
开发语言·php
冰淇淋烤布蕾29 分钟前
EasyExcel使用
java·开发语言·excel
拾荒的小海螺36 分钟前
JAVA:探索 EasyExcel 的技术指南
java·开发语言
马剑威(威哥爱编程)1 小时前
哇喔!20种单例模式的实现与变异总结
java·开发语言·单例模式
白-胖-子1 小时前
【蓝桥等考C++真题】蓝桥杯等级考试C++组第13级L13真题原题(含答案)-统计数字
开发语言·c++·算法·蓝桥杯·等考·13级
好睡凯1 小时前
c++写一个死锁并且自己解锁
开发语言·c++·算法
java—大象1 小时前
基于java+springboot+layui的流浪动物交流信息平台设计实现
java·开发语言·spring boot·layui·课程设计
yyqzjw1 小时前
【qt】控件篇(Enable|geometry)
开发语言·qt
csdn_kike1 小时前
QT Unknown module(s) in QT 以及maintenance tool的更详细用法(qt6.6.0)
开发语言·qt