//chatClient 头文件
#ifndef CHATCLIENT_H
#define CHATCLIENT_H
#include <QWidget>
#include <QTcpSocket>
#include <QMessageBox>
#include <QNetworkInterface>
namespace Ui {
class chatClient;
}
class chatClient : public QWidget
{
Q_OBJECT
public slots:
void on_showSend();
public:
explicit chatClient(QWidget *parent = nullptr);
~chatClient();
private slots:
void on_sendBtn_clicked(); //发送槽函数
void on_conBtn_clicked(); //连接服务器槽函数
void on_connected();
void on_readyRead();
void on_disconnected();
private:
Ui::chatClient *ui;
//客户端
QTcpSocket *socket;
//用户名
QString userName;
};
#endif // CHATCLIENT_H
//login 头文件
#ifndef LOGIN_H
#define LOGIN_H
#include <QMainWindow> //父类头文件
#include <QPushButton> //按钮类
#include <QDebug> //输出日志信息
#include <QLineEdit> //文本框
#include <QLabel> //标签类
#include <QMessageBox> //消息弹窗
QT_BEGIN_NAMESPACE
namespace Ui { class login; } //全名空间声明
QT_END_NAMESPACE
class login : public QMainWindow
{
Q_OBJECT
protected: //按钮指针声明
QPushButton *btn1;
QPushButton *btn2;
QLineEdit *edit2;
QLabel *lable3;
QLabel *lable2;
QLabel *lable1;
signals:
void login_signal(); //自定义登录信号函数
void on_showSignal();
private slots:
void my_slot(); //自定义槽函数
void my_cancel();
public:
login(QWidget *parent = nullptr); //构造函数
~login(); //析构函数
private:
Ui::login *ui; //ui指针
};
#endif // LOGIN_H
//chatClient.cpp
#include "chatclient.h"
#include "ui_chatclient.h"
chatClient::chatClient(QWidget *parent) :
QWidget(parent),
ui(new Ui::chatClient)
{
ui->setupUi(this);
this->setWindowIcon(QIcon(":/icon/QQ.png"));
this->setWindowTitle("地平线");
socket = new QTcpSocket(this);
//如果客户端成功连接到服务器,那么该客户端就会自动发射一个connected的信号
//我们将该信号连接到自定义的槽函数中,处理相关逻辑
//由于客户端只有一个,所以,该连接写在构造函数中即可
connect(socket, &QTcpSocket::connected, this, &chatClient::on_connected);
connect(socket, &QTcpSocket::readyRead, this, &chatClient::on_readyRead);
connect(socket, &QTcpSocket::disconnected, this, &chatClient::on_disconnected);
}
chatClient::~chatClient()
{
delete ui;
}
//槽函数显示客户端对话框
void chatClient::on_showSend()
{
this->show();
}
//发送
void chatClient::on_sendBtn_clicked()
{
QString ui_data = ui->lineEdit->text();
QString msg = userName + ": " + ui_data;
socket->write(msg.toLocal8Bit());
ui->lineEdit->clear();
}
//连接服务器
void chatClient::on_conBtn_clicked()
{
QString ip = ui->iplineEdit->text();
quint16 port = ui->portlineEdit->text().toUInt();
if(ip.isEmpty() || !port)
{
QMessageBox::warning(this, "", "设置连接信息 ip or port");
return;
}
if(ui->conBtn->text() == "连接服务器")
{
userName = ui->userlineEdit->text();
//连接操作
socket->connectToHost(ip, port);
ui->conBtn->setText("断开连接");
}else
{
//告诉大家 离开聊天室
QString msg = userName + ": 离开聊天室";
socket->write(msg.toLocal8Bit());
//断开连接
socket->disconnectFromHost();
ui->conBtn->setText("连接服务器");
}
}
//连接服务器
void chatClient::on_connected()
{
QMessageBox::information(this, "", "连接成功");
//并向服务器发送一条消息
QString msg = userName + ": 进入聊天室";
socket->write(msg.toLocal8Bit());
}
//处理信号槽函数
void chatClient::on_readyRead()
{
QByteArray msg = socket->readAll();
ui->listWidget->addItem(QString::fromLocal8Bit(msg));
}
//断开连接
void chatClient::on_disconnected()
{
QMessageBox::information(this, "", "断开连接");
}
//login.cpp
#include "login.h" //自定义头文件
#include "ui_login.h" //ui界面头文件
login::login(QWidget *parent) //构造函数实现
: QMainWindow(parent) //调用父类构造函数
, ui(new Ui::login) //给指针成员初始化
{
ui->setupUi(this); //调用ui界面设置函数
//本窗体
this->setWindowTitle("QQ");
this->setWindowIcon(QIcon("D:/23031C++QT/day8/03login/icon/QQ.png"));
this->setFixedSize(430, 330);
//登录按钮实例化
btn1 = new QPushButton(QIcon("D:/23031C++QT/day8/03login/icon/denglu.png"),
"登录",
this);
btn1->move(200, 200);
//取消按钮实例化
btn2 = new QPushButton(QIcon("D:/23031C++QT/day8/03login/icon/quxiao.png"),
"取消",
this);
btn2->resize(btn1->size());
btn2->move(320, 200);
//密码文本框
ui->edit1->setEchoMode(QLineEdit::Password);
ui->edit1->setPlaceholderText("密码");
//账号文本框
edit2 = new QLineEdit(this);
edit2->resize(ui->edit1->size());
edit2->move(110, 80);
edit2->setPlaceholderText("QQ号码/手机/邮箱");
//标签
lable1 = new QLabel(this);
lable1->setPixmap(QPixmap("D:/23031C++QT/day8/03login/icon/zhanghao.png"));
lable1->resize(30, 30);
lable1->move(70, 80);
lable1->setScaledContents(true);
//标签
lable2 = new QLabel(this);
lable2->setPixmap(QPixmap("D:/23031C++QT/day8/03login/icon/mima.png"));
lable2->resize(30, 30);
lable2->move(70, 130);
lable2->setScaledContents(true);
//标签
lable3 = new QLabel(this);
lable3->setPixmap(QPixmap("D:/23031C++QT/day8/03login/icon/logo.png"));
lable3->resize(90, 90);
lable3->move(170, 10);
lable3->setScaledContents(true);
//登录按钮连接槽
connect(btn1, SIGNAL(clicked()), this, SLOT(my_slot()));
connect(btn2, SIGNAL(clicked()), this, SLOT(my_cancel()));
}
//实现槽函数
void login::my_slot()
{
QString name = edit2->text();
QString password = ui->edit1->text();
if(name != "admin" || password != "123456")
{
QMessageBox box(QMessageBox::Critical,
"错误提示",
"账号错误,请重新输入",
QMessageBox::Ok | QMessageBox::No
);
int button = box.exec();
if(button == QMessageBox::Ok)
{
ui->edit1->clear();
ui->edit1->setFocus();
}else if(button == QMessageBox::No)
{
this->close();
}
return;
}
QMessageBox::information(this,
"成功提示",
"恭喜登录成功",
QMessageBox::Ok
);
emit on_showSignal();
this->close();
}
//取消执行槽函数
void login::my_cancel()
{
int ret = QMessageBox::information(this,
"取消提示",
"您确定要退出登录吗?",
QMessageBox::Ok | QMessageBox::No
);
if(ret == QMessageBox::Ok)
{
this->close();
}
}
login::~login() //析构函数
{
delete ui;
}
//main.cpp
#include "login.h"
#include "chatclient.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
login w;
w.show();
//登录成功页面发送信号给槽函数
chatClient chat;
QObject::connect(&w, &login::on_showSignal, &chat, &chatClient::on_showSend);
return a.exec();
}