2023/5/20 QT实现登录并聊天室聊天

//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();
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现Qt聊天室登录界面,可以采用以下步骤: 1. 创建一个Qt窗口应用程序,并在窗口中添加QLineEdit控件和QPushButton控件,用于输入用户名和密码以及登录按钮。 2. 在登录按钮的clicked()信号中,获取QLineEdit控件中的用户名和密码,并与预先定义的用户名和密码进行比较,如果匹配,则登录成功,否则弹出提示框。 3. 为窗口设置样式,使其看起来更加美观。 以下是一个简单的示例代码: ```cpp #include <QApplication> #include <QLineEdit> #include <QPushButton> #include <QMessageBox> #include <QVBoxLayout> #include <QLabel> int main(int argc, char *argv[]) { QApplication a(argc, argv); // 创建窗口 QWidget window; window.setWindowTitle("登录"); // 创建控件 QLabel *usernameLabel = new QLabel("用户名:"); QLineEdit *usernameEdit = new QLineEdit; QLabel *passwordLabel = new QLabel("密码:"); QLineEdit *passwordEdit = new QLineEdit; passwordEdit->setEchoMode(QLineEdit::Password); // 隐藏密码输入 QPushButton *loginButton = new QPushButton("登录"); // 布局 QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(usernameLabel); layout->addWidget(usernameEdit); layout->addWidget(passwordLabel); layout->addWidget(passwordEdit); layout->addWidget(loginButton); window.setLayout(layout); // 登录按钮点击事件 QObject::connect(loginButton, &QPushButton::clicked, [&](){ QString username = usernameEdit->text(); QString password = passwordEdit->text(); if(username == "admin" && password == "123456"){ QMessageBox::information(&window, "提示", "登录成功!"); } else{ QMessageBox::warning(&window, "提示", "用户名或密码错误!"); } }); // 设置窗口样式 window.setStyleSheet("QLineEdit{padding: 2px 8px; border: 1px solid #ccc; border-radius: 4px;}" "QPushButton{padding: 2px 8px; border: none; border-radius: 4px; background-color: #4CAF50; color: #fff;}" "QPushButton:hover{background-color: #3E8E41;}" "QLabel{font-weight: bold;}"); // 显示窗口 window.show(); return a.exec(); } ``` 通过以上步骤,就可以实现一个简单的Qt聊天室登录界面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值