客户端
最编程
2024-10-13 08:34:11
...
同样的,别忘了在.pro文件中修改
QT += core gui network
#ifndef CHAT_H
#define CHAT_H
#include <QWidget>
#include<QTcpSocket>
namespace Ui {
class chat;
}
class chat : public QWidget
{
Q_OBJECT
public:
explicit chat(QTcpSocket *socket,QWidget *parent = nullptr);
~chat();
private slots:
void on_sendButton_clicked();
void on_clearButton_clicked();
void clientReadHandler();
private:
Ui::chat *ui;
QTcpSocket *socket;
};
#endif // CHAT_H
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QTcpsocket>
#include<QHostAddress>
#include<QMessageBox>
#include "chat.h"
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_cancelButton_clicked();
void on_connectButton_clicked();
private:
Ui::Widget *ui;
QTcpSocket *socket;
chat *ch;
};
#endif // WIDGET_H
#include "chat.h"
#include "ui_chat.h"
chat::chat(QTcpSocket *s,QWidget *parent) :
QWidget(parent),
ui(new Ui::chat)
{
ui->setupUi(this);
socket = s;
connect(socket,&QTcpSocket::readyRead,this,&chat::clientReadHandler);
}
chat::~chat()
{
delete ui;
}
void chat::on_sendButton_clicked()
{
QByteArray ba;
ba.append(ui->lineEdit->text().toLatin1());
socket->write(ba);
}
void chat::on_clearButton_clicked()
{
ui->lineEdit->clear();
}
void chat::clientReadHandler()
{
ui->revlineEdit->setText(QString(socket->readAll()));
}
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
socket = new QTcpSocket;
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_connectButton_clicked()
{
QString ip = ui->iplineEdit->text();
QString port = ui->portlineEdit->text();
qDebug() << ip;
qDebug() << port;
socket->connectToHost(QHostAddress(ip),port.toShort());
connect(socket,&QTcpSocket::connected,[this]{
QMessageBox::information(this,"Tip","连接成功");
this->hide();
ch = new chat(socket);
ch->show();
});
connect(socket,&QTcpSocket::disconnected,[this]{
QMessageBox::information(this,"Tip","连接失败!");
});
}
void Widget::on_cancelButton_clicked()
{
this->close();
}
上一篇: 基于变压器的电压发生器
推荐阅读
-
客户端
-
使用 TortoiseGit 和 Git 的 Git 客户端
-
ubuntu24 finalshell 无法连接到 ubuntu 服务器,客户端无法连接到 ubuntu,无法远程连接到 ubuntu。
-
同时一键启动客户端和服务器
-
撸了一个可调试 gRPC 的 GUI 客户端
-
直播ios 版_第三方pptp客户端ios
-
VNC Viewer客户端的Linux版本
-
使用 C# Net Core 使用 ClientWebSocket 实现 WebSocket 客户端的方法 或 C# Net Core 实现 WebSocket 客户端的方法:使用 ClientWebSocket
-
SCCM部署(十一)---SCCM客户端安装
-
推荐七种强大易用的Git GUI客户端工具