欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C++-QT聊天工具-界面切换

程序员文章站 2022-05-22 12:45:40
...

本文实现QT聊天工具的界面,一个类实现一个界面,通过信号和槽槽机制切换界面,主界面通过按钮切换置子界面,子界面通过关闭事件和按钮返回主界面。

效果图

整个工程文件免费下载:https://download.csdn.net/download/qq_40788199/12045186

主界面

C++-QT聊天工具-界面切换

设置界面

C++-QT聊天工具-界面切换

注册界面

C++-QT聊天工具-界面切换

聊天界面

C++-QT聊天工具-界面切换

信号(signals)
信号是定义在类里面的一个函数,必须在函数前加入关键字signals,其实也可以理解为宏,这个signals涉及到qt的另一个核心机制(模板元编程),想必度过effective C++的同学对该技术并不陌生,扯远了,继续说信号,当拥有信号的类声明了一个对象时,该对象便有了发送该信号的能力,当然,该类的派生类也具有该能力。定义信号函数时,返回值必须是void,可以有形参,但是没有成员限定访问符的限定,我认为它是public,但是好像是真没有,定义一个signals给大家看一下:

signals:
槽(slots)
槽函数就有意思了,它是信号的响应函数,与信号函数不同,它有成员限定访问符,可以是public ,protectde和private,我最近阅读的文档来看,大部分都只采用public和private,当用public限定槽函数,其它类的信号可以connect它,自己就更不用说啦,但是private的时候,就只能自己类的对象来关联了,但是protected就有点,嗯,怎么说,继承和派生时候贡献挺大的,但是在这不受待见,可能是我理解还不够,好了写个简单的槽来看一下

public slots:
连接(connect)
连接有多重写法,主要体现在信号函数和槽函数加入的方式,

QObject::connect(&m_signal,SIGNAL(signals()),&m_slot,SLOT(slot()));
 

由于每个界面都很简单很类似,就懒得都去注释了,第一个界面注释了一些关键部分

主界面头文件

#ifndef LOGINUI_H
#define LOGINUI_H

#include <QWidget>
#include <QPushButton>
#include <QLabel>
#include <QTextEdit>
#include <QApplication>
#include <QLineEdit>
#include <QMessageBox>
#include <QDesktopWidget>
#include <QCloseEvent>
#include <pthread.h>
#include "cclientui.h"
#include "cclientnet.h"
#include "registerui.h"
#include "settingui.h"
using namespace std;

class RegisterUI;
class SettingUI;
class CClientUI;

class LoginUI : public QWidget
{
    Q_OBJECT

    QPushButton *RegisterButton;
    QPushButton *LoginButton;
    QPushButton *SettingskButton;

    QLabel *UserLabel;
    QLabel *PasswdLabel;

    QLineEdit *UserLineEdit;
    QLineEdit *PasswdLineEdit;

    QString IP;
    QString Port;

    //其他界面,子类
    RegisterUI *Register;
    SettingUI *Setting;
    CClientUI *client;

public:
    LoginUI();
    ~LoginUI();
    //二次构造函数
    bool LoginConstruct();
    static LoginUI *LoginNewInstance();
    //界面显示函数
    void LoginShow();

private slots:
    //登陆按钮槽函数,登陆成功切换置聊天界面
    void on_LoginButton_clicket();
    //注册按钮槽函数,切换置注册界面
    void on_RegisterButton_clicket();
    //设置按钮槽函数,切换置设置界面
    void on_SettingskButton_clicket();
    //从注册界面切换登陆界面
    void RegisterSlotShow();
    //从设置界面切换设置界面
    void SettingSlotShow();
};

#endif // LOGINUI_H

主页面源文件

#include "loginui.h"

LoginUI::LoginUI() :
    QWidget(NULL,Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint)
{

}

LoginUI::~LoginUI()
{

}

LoginUI *LoginUI::LoginNewInstance()
{
    LoginUI *ret = new LoginUI;

    if(!(ret && ret->LoginConstruct()))
    {
        delete ret;
        ret = NULL;
    }

    return ret;
}

bool LoginUI::LoginConstruct()
{
    //界面标题
    this->setWindowTitle("用户登录");
    //实例化注册界面
    Register = RegisterUI::RegisterNewInstance();
    if(Register == NULL)
        return false;
    //实例化设置界面
    Setting = SettingUI::SettingNewInstance();
    if(Setting == NULL)
        return false;
    //实例化聊天界面
    client = CClientUI::NewInstance();
    if(client == NULL)
        return false;

    UserLabel = new QLabel(this);
    if(UserLabel == NULL)
        return false;
    UserLabel->resize(100,30);//设置大小
    UserLabel->move(80,80);//布置位置
    UserLabel->setText("用户名: ");//设置文字
    UserLabel->setFont(QFont("Microsoft YaHei",18));//设置字体及大小

    UserLineEdit = new QLineEdit(this);
    if(UserLineEdit == NULL)
        return false;
    UserLineEdit->resize(180,35);
    UserLineEdit->move(190,80);
    UserLineEdit->setFont(QFont("Microsoft YaHei",18));

    PasswdLabel = new QLabel(this);
    if(PasswdLabel == NULL)
        return false;
    PasswdLabel->resize(100,30);
    PasswdLabel->move(80,150);
    PasswdLabel->setText(" 密码  :");
    PasswdLabel->setFont(QFont("Microsoft YaHei",18));

    PasswdLineEdit = new QLineEdit(this);
    if(PasswdLineEdit == NULL)
        return false;
    PasswdLineEdit->resize(180,35);
    PasswdLineEdit->move(190,150);
    PasswdLineEdit->setFont(QFont("Microsoft YaHei",18));

    LoginButton = new QPushButton(this);
    if(LoginButton == NULL)
        return 0;
    LoginButton->resize(80,50);
    LoginButton->move(100,250);
    LoginButton->setText("登录");
    LoginButton->setFont(QFont("Microsoft YaHei",18));

    RegisterButton = new QPushButton(this);
    if(RegisterButton == NULL)
        return false;
    RegisterButton->resize(80,50);
    RegisterButton->move(260,250);
    RegisterButton->setText("注册");
    RegisterButton->setFont(QFont("Microsoft YaHei",18));

    SettingskButton  = new QPushButton(this);
    if(SettingskButton == NULL)
        return false;
    SettingskButton->resize(80,50);
    SettingskButton->move(180,350);
    SettingskButton->setText("设置");
    SettingskButton->setFont(QFont("Microsoft YaHei",18));

    //信号槽机制
    connect(LoginButton,SIGNAL(clicked(bool)),this,SLOT(on_LoginButton_clicket()));
    connect(RegisterButton,SIGNAL(clicked(bool)),this,SLOT(on_RegisterButton_clicket()));
    connect(SettingskButton,SIGNAL(clicked(bool)),this,SLOT(on_SettingskButton_clicket()));
    //信号是其他类的
    connect(Register,SIGNAL(RegisterSignal()),this,SLOT(RegisterSlotShow()));
    connect(Setting,SIGNAL(SettingSignal()),this,SLOT(SettingSlotShow()));

    return true;
}

void LoginUI::on_LoginButton_clicket()
{
    this->hide();//隐藏本界面
    client->move(this->frameGeometry().x(),this->frameGeometry().y());//用之前界面的位置
    client->CClientShow();//显示子界面
}
void LoginUI::on_RegisterButton_clicket()
{
    this->hide();
    Register->move(this->frameGeometry().x(),this->frameGeometry().y());
    Register->RegisterShow();
}

void LoginUI::on_SettingskButton_clicket()
{
    this->hide();
    Setting->move(this->frameGeometry().x(),this->frameGeometry().y());
    Setting->SettingShow();
}


void LoginUI::LoginShow()
{
    QWidget::show();
    setFixedSize(width(),height());//设置界面大小
}

void LoginUI::RegisterSlotShow()
{
    this->show();
    setFixedSize(width(),height());
}

void LoginUI::SettingSlotShow()
{
    this->show();
    setFixedSize(width(),height());
}

设置界面头文件,这里需要注意的时关闭事件,closeEvent(QCloseEvent *enent),函数必须一模一样,具体原因问度娘

#ifndef SETTINGUI_H
#define SETTINGUI_H

#include <QWidget>
#include <QPushButton>
#include <QLabel>
#include <QTextEdit>
#include <QApplication>
#include <QLineEdit>
#include <QMessageBox>
#include <QCloseEvent>
#include <QString>
#include <QDebug>
#include "loginui.h"
#include "registerui.h"

class SettingUI : public QWidget
{
    Q_OBJECT

    QLabel *IPLabel;
    QLabel *PortLabel;

    QLineEdit *IPLineEdit;
    QLineEdit *PortLineEdit;

    QPushButton *ReturnButton;

public:
    SettingUI();
    ~SettingUI();

    bool SettingConstruct();

    static SettingUI *SettingNewInstance();

    void SettingShow();
    //关闭事件
    void closeEvent(QCloseEvent *enent);

private slots:
    void on_ReturnButton_clicket();

signals:
    void SettingSignal();

};

#endif // SETTINGUI_H
设置界面源文件
#include "settingui.h"

SettingUI::SettingUI() :
    QWidget(NULL,Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint)
{

}

SettingUI::~SettingUI()
{

}

bool SettingUI::SettingConstruct()
{
    this->setWindowTitle("设置窗口");

    IPLabel = new QLabel(this);
    if(IPLabel == NULL)
        return false;
    IPLabel->resize(180,30);
    IPLabel->move(50,50);
    IPLabel->setText("服务器IP地址: ");
    IPLabel->setFont(QFont("Microsoft YaHei",18));

    IPLineEdit = new QLineEdit(this);
    if(IPLineEdit == NULL)
        return false;
    IPLineEdit->resize(200,30);
    IPLineEdit->move(230,50);
    IPLineEdit->setText("192.168.3.11");
    IPLineEdit->setFont(QFont("Microsoft YaHei",18));

    PortLabel = new QLabel(this);
    if(PortLabel == NULL)
        return false;
    PortLabel->resize(180,30);
    PortLabel->move(50,100);
    PortLabel->setText("服务器端口号: ");
    PortLabel->setFont(QFont("Microsoft YaHei",18));

    PortLineEdit = new QLineEdit(this);
    if(PortLineEdit == NULL)
        return false;
    PortLineEdit->resize(200,30);
    PortLineEdit->move(230,100);
    PortLineEdit->setText("8888");
    PortLineEdit->setFont(QFont("Microsoft YaHei",18));

    ReturnButton = new QPushButton(this);
    if(ReturnButton == NULL)
        return false;
    ReturnButton->resize(100,50);
    ReturnButton->move(200,200);
    ReturnButton->setText("返回");
    ReturnButton->setFont(QFont("Microsoft YaHei",18));

    connect(ReturnButton,SIGNAL(clicked(bool)),this,SLOT(on_ReturnButton_clicket()));

    return true;
}

void SettingUI::on_ReturnButton_clicket()
{
    this->hide();
    emit SettingSignal();
}

SettingUI *SettingUI::SettingNewInstance()
{
    SettingUI *ret = new SettingUI;

    if(!(ret && ret->SettingConstruct()))
    {
        delete ret;
        ret = NULL;
    }

   return ret;
}

void SettingUI::closeEvent(QCloseEvent *enent)
{
    emit SettingSignal();
    enent->accept();
}

void SettingUI::SettingShow()
{
    QWidget::show();
    setFixedSize(width(),height());
}

注册界面头文件

#ifndef REGISTERUI_H
#define REGISTERUI_H

#include <QWidget>
#include <QPushButton>
#include <QLabel>
#include <QTextEdit>
#include <QApplication>
#include <QLineEdit>
#include <QMessageBox>
#include <QCloseEvent>
#include "cclientnet.h"
#include "loginui.h"
#include "settingui.h"


class RegisterUI : public QWidget
{
    Q_OBJECT

    QPushButton *ConfirmButton;
    QPushButton *ReturnButton;

    QLabel *UserLabel;
    QLabel *PasswdLabel;
    QLabel *CheckPasswdLabel;

    QLineEdit *UserLineEdit;
    QLineEdit *PasswdLineEdit;
    QLineEdit *CheckPasswdLineEdit;

    QMessageBox *RegisterMsgBox;

public:
    RegisterUI();
    ~RegisterUI();

    bool RegisterConstruct();

    static RegisterUI *RegisterNewInstance();

    void RegisterShow();

    int ValidatePass();

    int GetUser(QLineEdit *Edit);

    int GetIPPotr(QString IP,QString Port);

    void closeEvent(QCloseEvent *event);

private slots:
    int on_ConfirmButton_clicket();
    void on_ReturnButton_clicket();

signals:
    void RegisterSignal();

};

#endif // REGISTERUI_H

注册界面源文件

#include "registerui.h"

RegisterUI::RegisterUI() :
    QWidget(NULL,Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint)
{

}

RegisterUI::~RegisterUI()
{

}

bool RegisterUI::RegisterConstruct()
{

    this->setWindowTitle("注册界面");

    UserLabel = new QLabel(this);
    if(UserLabel == NULL)
        return false;
    UserLabel->resize(100,30);
    UserLabel->move(80,80);
    UserLabel->setText("用户名: ");
    UserLabel->setFont(QFont("Microsoft YaHei",18));

    UserLineEdit = new QLineEdit(this);
    if(UserLineEdit == NULL)
        return false;
    UserLineEdit->resize(180,35);
    UserLineEdit->move(190,80);
    UserLineEdit->setFont(QFont("Microsoft YaHei",18));

    PasswdLabel = new QLabel(this);
    if(PasswdLabel == NULL)
        return false;
    PasswdLabel->resize(100,30);
    PasswdLabel->move(80,150);
    PasswdLabel->setText(" 密码  :");
    PasswdLabel->setFont(QFont("Microsoft YaHei",18));

    PasswdLineEdit = new QLineEdit(this);
    if(PasswdLineEdit == NULL)
        return false;
    PasswdLineEdit->resize(180,35);
    PasswdLineEdit->move(190,150);
    PasswdLineEdit->setFont(QFont("Microsoft YaHei",18));

    CheckPasswdLabel = new QLabel(this);
    if(CheckPasswdLabel == NULL)
        return false;
    CheckPasswdLabel->resize(100,30);
    CheckPasswdLabel->move(80,220);
    CheckPasswdLabel->setText(" 确认  :");
    CheckPasswdLabel->setFont(QFont("Microsoft YaHei",18));

    CheckPasswdLineEdit = new QLineEdit(this);
    if(CheckPasswdLineEdit == NULL)
        return false;
    CheckPasswdLineEdit->resize(180,35);
    CheckPasswdLineEdit->move(190,220);
    CheckPasswdLineEdit->setFont(QFont("Microsoft YaHei",18));

    ConfirmButton = new QPushButton(this);
    if(ConfirmButton == NULL)
        return false;
    ConfirmButton->resize(80,50);
    ConfirmButton->move(80,320);
    ConfirmButton->setText("确定");
    ConfirmButton->setFont(QFont("Microsoft YaHei",15));

    ReturnButton = new QPushButton(this);
    if(ReturnButton == NULL)
        return false;
    ReturnButton->resize(80,50);
    ReturnButton->move(250,320);
    ReturnButton->setText("返回");
    ReturnButton->setFont(QFont("Microsoft YaHei",15));


    connect(ConfirmButton,SIGNAL(clicked(bool)),this,SLOT(on_ConfirmButton_clicket()));
    connect(ReturnButton,SIGNAL(clicked(bool)),this,SLOT(on_ReturnButton_clicket()));

    return true;
}

void RegisterUI::on_ReturnButton_clicket()
{
    this->hide();
    emit RegisterSignal();
}

RegisterUI *RegisterUI::RegisterNewInstance()
{
    RegisterUI *ret = new RegisterUI();

    if(!(ret && ret->RegisterConstruct()))
    {
        delete ret;
        ret = NULL;
    }

   return ret;
}

int RegisterUI::on_ConfirmButton_clicket()
{

    return 0;
}

void RegisterUI::closeEvent(QCloseEvent *event)
{
    emit RegisterSignal();
    event->accept();
}

void RegisterUI::RegisterShow()
{
    QWidget::show();
    setFixedSize(width(),height());
}

聊天界面头文件

#ifndef LoginUI_H
#define LoginUI_H

#include <QWidget>
#include <QPushButton>
#include <QLabel>
#include <QTextEdit>
#include <QApplication>
#include <QLineEdit>
#include <QToolBox>
#include <QToolButton>
#include <QListView>
#include <QMessageBox>
#include <QGroupBox>
#include <QVector>
#include <QVBoxLayout>
#include <QCloseEvent>
#include "loginui.h"
#include "registerui.h"
#include "settingui.h"

const int UibufSZ = 1024;

class CClientUI: public QWidget
{
    Q_OBJECT

    QPushButton *SendButton;

    QTextEdit *MsgTextEdit;
    QTextEdit *SendTextEdit;

    QToolBox *FriendToolBox;
    QGroupBox *box;
    QVBoxLayout *layout;

    QVector<QPushButton*> Button;

    QString IP;
    QString Port;

    int ID = 0;
    QString User;
    QString Passwd;
    QString Receiver;
    QString MsgBuf;

    char friendBuf[UibufSZ];
    char Sender[32];
    char msgBuf[UibufSZ];

public:
    CClientUI();
    ~CClientUI();

    bool Construct();

    static CClientUI * NewInstance();

    int FriendsList();

    void closeEvent(QCloseEvent *enent);

    void CClientShow();

    void RemoveAllButtons();

private slots:
    void on_FriendButton_clicket();

    int on_SendButton_clicket();
};

#endif // LoginUI_H

聊天界面源文件

#include "cclientui.h"

CClientUI::CClientUI():
       QWidget(NULL,Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint)
{

}

CClientUI::~CClientUI()
{
}

bool CClientUI::Construct()
{
    this->setWindowTitle("消息窗口");

    MsgTextEdit = new QTextEdit(this);
    if(MsgTextEdit == NULL)
        return false;
    MsgTextEdit->resize(500,300);
    MsgTextEdit->move(20,20);
    MsgTextEdit->setReadOnly(true);
    MsgTextEdit->setFont(QFont("Microsoft YaHei",18));

    SendTextEdit = new QTextEdit(this);
    if(SendTextEdit == NULL)
        return false;
    SendTextEdit->resize(500,150);
    SendTextEdit->move(20,340);
    SendTextEdit->setFont(QFont("Microsoft YaHei",18));

    FriendToolBox = new QToolBox(this);
    if(FriendToolBox == NULL)
        return false;
    FriendToolBox->resize(150,300);
    FriendToolBox->move(540,20);
    FriendToolBox->setFont(QFont("Microsoft YaHei",18));
    FriendToolBox->setStyleSheet("background:white");

    box = new QGroupBox(FriendToolBox);
    layout = new QVBoxLayout(box);
    FriendToolBox->addItem(box,"Friends list");

    SendButton = new QPushButton(this);
    if(SendButton == NULL)
        return false;
    SendButton->resize(150,150);
    SendButton->move(540,340);
    SendButton->setFont(QFont("Microsoft YaHei",18));
    SendButton->setText("发送");

    connect(SendButton,SIGNAL(clicked(bool)),this,SLOT(on_SendButton_clicket()));

    return true;
}

CClientUI *CClientUI::NewInstance()
{
    CClientUI *ret = new CClientUI;

    if(!(ret && ret->Construct()))
    {
        delete ret;
        ret = NULL;
    }

    return ret;

}

void CClientUI::on_FriendButton_clicket()
{
    QPushButton *clickedButton = qobject_cast<QPushButton *>(sender());
    Receiver = clickedButton->text();
    this->setWindowTitle(clickedButton->text());
    this->CClientShow();
}

int CClientUI::on_SendButton_clicket()
{

    return 0;
}

void CClientUI::closeEvent(QCloseEvent *enent)
{
    enent->accept();
}

void CClientUI::RemoveAllButtons()
{
    for(int i = 0; i < layout->count(); i++)
    {
         layout->itemAt(i)->widget()->deleteLater();
    }
}

void CClientUI::CClientShow()
{
    if(Sender != "")
    {
        QString str(msgBuf);
        MsgTextEdit->setText(str);
    }
//    cout << "show sender " << &Sender << Sender << endl;;
    RemoveAllButtons();
    QWidget::show();
    setFixedSize(width(),height());
}

 

相关标签: C/C++程序 QT