QTday5

客户端再写

头文件:

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include
#include
namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

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

private slots:
    void on_connepushButton_clicked();
    void connected_slot();
    void readyread_slot();
    void on_sendButton_clicked();
    void disconnect_slot();

private:
    Ui::Widget *ui;
    QTcpSocket *socket;
    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);
    socket = new QTcpSocket(this);//实例化一个套接字
    connect(socket,&QTcpSocket::connected,this,&Widget::connected_slot);
    connect(socket,&QTcpSocket::readyRead,this,&Widget::readyread_slot);
    connect(socket,&QTcpSocket::disconnected,this,&Widget::disconnect_slot);
}

Widget::~Widget()
{
    delete ui;
}
//连接按钮的槽函数
void Widget::on_connepushButton_clicked()
{
    QString ip = ui->addresslineEdit_3->text();//获取ip地址
    quint16 port = ui->portlineEdit_2->text().toUInt();//获端口号

    username = ui->usernamelineEdit->text();

    if(ui->connepushButton->text() == "连接服务器")
    {
        socket->connectToHost(ip,port);
        ui->connepushButton->setText("断开服务器");
    }
    else {
        //告诉服务器一声
        QString msg = username +"+离开了聊天室";
        socket->write(msg.toLocal8Bit());

        //断开连接
        socket->disconnectFromHost();
        ui->connepushButton->setText("连接服务器");
    }
}
//connected_slot处理函数
void Widget::connected_slot()
{
    //顺便给服务器发消息
    QMessageBox::information(this,"成功","连接服务器成功");
    QString msg = username + ":进入了聊天室";
    socket->write(msg.toLocal8Bit());
}
//连接成功后的处理函数
void Widget::readyread_slot()
{
    if(socket->bytesAvailable()!=0)
    {
        QString msg = QString::fromLocal8Bit(socket->readAll());
        ui->listWidget->addItem(msg);//将数据添加到ui界面
    }
}
//发送按钮对应的槽函数
void Widget::on_sendButton_clicked()
{
    QString msg = ui->lineEdit->text();
    socket->write(msg.toLocal8Bit());
    ui->lineEdit->clear();
}

void Widget::disconnect_slot()
{
    QMessageBox::information(this,"","您已断开连接");
}

效果如下

QTday5_第1张图片

 服务器重写

头文件

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include//客户端类

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

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

private slots:
    void on_sendButton_clicked();
    void connected_slots();
    void readyRead_slots();
    void on_connectButton_clicked();
    void disconnect_slots();

private:
    Ui::Widget *ui;
    QTcpSocket *socket;//定义客户端指针

    //定义变量存储变量用户名
    QString userName;
};

#endif // WIDGET_H

源文件

#include "widget.h"
#include "ui_widget.h"
#include
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    ui->listWidget->setStyleSheet("background-color:white");
    ui->sendButton->setStyleSheet("background-color:lightblue");
    ui->connectButton->setStyleSheet("background-color:lightgreen");

    socket = new QTcpSocket(this);
    //如果连接成功,客户端自动发射一个connected信号、
    //由于只要连接一次就行,所以定义在构造函数中
    connect(socket,&QTcpSocket::connected,this,&Widget:: connected_slots);

    //客户端连接服务器后,将信号连接到自定义的槽函数中
    connect(socket,&QTcpSocket::readyRead,this,&Widget::readyRead_slots);

    connect(socket,&QTcpSocket::disconnected,this,&Widget::disconnect_slots);

}

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

void Widget::connected_slots()
{
    QMessageBox::information(this,"成功","连接服务器成功");
    //顺便给服务器发送消息:某某某进入聊天室
    QString msg = userName + ": 进入聊天室";
    socket->write(msg.toLocal8Bit());
}

void Widget::readyRead_slots()
{
    if(socket->bytesAvailable() !=0)
    {
        QString msg =  QString::fromLocal8Bit(socket->readAll());

        //将数据展示到ui界面
        ui->listWidget->addItem(msg);
    }
}

//连接服务器按钮对应的槽函数
void Widget::on_connectButton_clicked()
{
    QString ip = ui->AddresslineEdit->text();
    quint16 port = ui->portlineEdit->text().toUInt();

    userName = ui->UsernamelineEdit->text();

    if(ui->connectButton->text() == "连接服务器")
    {
        //连接服务器
        socket->connectToHost(ip,port);
        //将按钮文本改为断开
        ui->connectButton->setText("断开服务器");
    }
    else
    {
        //告诉服务器一声
        QString msg = userName +":离开聊天室";
        socket->write(msg.toLocal8Bit());

        //断开连接
        socket->disconnectFromHost();
        //客户端成功断开后

        //将按钮文本改为连接
        ui->connectButton->setText("连接服务器");
    }
}

//发送按钮对应的槽函数
void Widget::on_sendButton_clicked()
{
    QString msg = ui->sendlineEdit->text();
    //
    socket->write(msg.toLocal8Bit());

    ui->sendlineEdit->clear();

}
//disconnect槽函数的实现
void Widget::disconnect_slots()
{
    QMessageBox::information(this,"","您已断开连接");
}

运行效果

QTday5_第2张图片

 

你可能感兴趣的:(qt)