QT登录客户端的功能简单实现

头文件

cpp 复制代码
#ifndef LOGINUI_H
#define LOGINUI_H

#include <QWidget>
#include <QLineEdit>
#include <QPushButton>
#include <QLabel>
#include <QMessageBox>

#include "client.h"

class LoginUI : public QWidget
{
    Q_OBJECT

private:
    //标签类组件
    QLabel* Background_Lab;
    QLabel* Titile_Lab;
    QLabel* Username_Lab;
    QLabel* Password_Lab;

    //按钮类组件
    QPushButton* Close_Btn;
    QPushButton* Hide_Btn;
    QPushButton* Login_Btn;

    //行编辑器类组件
    QLineEdit* Username_Edit;
    QLineEdit* Password_Edit;

    //构造函数
    LoginUI();
    bool construct();

private slots:
    //槽函数
    void On_CloseBtn_Clicked();
    void On_Loginbtn_Slots();


public:
    //实例化对象
    static LoginUI* NewInstance();
    void show();
    ~LoginUI();

signals:
    //跳转函数
    void Jump();

};
#endif //LOGINUI_H

函数实现

cpp 复制代码
#include "LoginUI.h"
#include <QDebug>

LoginUI::LoginUI(): QWidget(nullptr, Qt::WindowCloseButtonHint)
{
}



void LoginUI::show()
{
    setWindowFlag(Qt::FramelessWindowHint);

    setFixedSize(538, 373);

    QWidget::show();
}


LoginUI::~LoginUI()
{
}

//构造函数
bool LoginUI::construct()
{
    bool ret = true;

    Background_Lab = new QLabel(this);

    if( Background_Lab != nullptr)
    {
        Background_Lab->resize(538, 160);
        Background_Lab->setStyleSheet("background-color:rgb(115,70,240)");
    }
    else
    {
        ret = false;
    }

    Titile_Lab = new QLabel(this);

    if( Titile_Lab != nullptr )
    {
        Titile_Lab->resize(90, 50);
        Titile_Lab->setPixmap(QPixmap(":/QT_Icon/QQ_Icon.png"));
        Titile_Lab->setScaledContents(true);
    }
    else
    {
        ret = false;
    }

    Close_Btn = new QPushButton(this);

    if( Close_Btn != nullptr )
    {
        Close_Btn->resize(20, 20);
        Close_Btn->setIcon(QIcon(":/QT_Icon/close02.png"));
        Close_Btn->move(508, 10);
        connect(this->Close_Btn, &QPushButton::clicked, this, &LoginUI::On_CloseBtn_Clicked);
    }
    else
    {
        ret = false;
    }

    Hide_Btn = new QPushButton(this);

    if( Hide_Btn != nullptr )
    {
        Hide_Btn->resize(20, 20);
        Hide_Btn->setIcon(QIcon(":/QT_Icon/hide02.png"));
        Hide_Btn->move(468, 10);
        connect(this->Hide_Btn, &QPushButton::clicked, this, &LoginUI::hide);
    }
    else
    {
        ret = false;
    }

    Username_Edit = new QLineEdit(this);

    if( Username_Edit != nullptr )
    {
        Username_Edit->resize(260, 40);
        Username_Edit->move(159, 173);
        Username_Edit->setMaxLength(16);
        Username_Edit->setPlaceholderText("QQ账号/手机号/邮箱");
    }
    else
    {
        ret = false;
    }

    Password_Edit = new QLineEdit(this);

    if( Password_Edit != nullptr )
    {
        Password_Edit->resize(260, 40);
        Password_Edit->move(Username_Edit->x(), Username_Edit->y()+60);
        Password_Edit->setEchoMode(QLineEdit::Password);
        Password_Edit->setMaxLength(16);
    }
    else
    {
        ret = false;
    }

    Username_Lab = new QLabel(this);

    if( Username_Lab != nullptr )
    {
        Username_Lab->resize(30, 30);
        Username_Lab->move(Username_Edit->x()-40, Username_Edit->y()+5);
        Username_Lab->setPixmap(QPixmap(":/QT_Icon/windos_icon1.png"));
        Username_Lab->setScaledContents(true);
    }
    else
    {
        ret = false;
    }

    Password_Lab = new QLabel(this);

    if( Password_Lab != nullptr )
    {
        Password_Lab->resize(30, 30);
        Password_Lab->move(Password_Edit->x()-40, Password_Edit->y()+5);
        Password_Lab->setPixmap(QPixmap(":/QT_Icon/password.png"));
        Password_Lab->setScaledContents(true);
    }
    else
    {
        ret = false;
    }

    Login_Btn = new QPushButton("登录",this);

    if( Login_Btn != nullptr )
    {
        Login_Btn->resize(300, 40);
        Login_Btn->setStyleSheet("background-color:rgb(8,189,253);");
        Login_Btn->move(Password_Lab->x(), Password_Lab->y()+60);
        connect(this->Login_Btn, &QPushButton::clicked, this, &LoginUI::On_Loginbtn_Slots);
    }
    else
    {
        ret = false;
    }



    return ret;

}

void LoginUI::On_CloseBtn_Clicked()
{
    QMessageBox box(QMessageBox::Question, "关闭", "您确定要退出?", QMessageBox::Yes | QMessageBox::No, this);
    box.setDefaultButton(QMessageBox::No);

    int ret = box.exec();

    if(ret == QMessageBox::Yes)
    {
        this->close();
    }
    else if(ret == QMessageBox::No)
    {

    }
}

void LoginUI::On_Loginbtn_Slots()
{
    if(Username_Edit->text() == "admin" && Password_Edit->text() == "123456")
    {
        emit Jump();

        this->close();
    }
    else
    {
        int ret = QMessageBox::warning(this, "失败", "账号或密码错误", QMessageBox::Ok, QMessageBox::Ok);
        if(ret == QMessageBox::Ok)
        {

        }
    }
}

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

    if( ret == nullptr && !ret->construct() )
    {
        delete ret;

        ret = nullptr;
    }


    qDebug() << ret->construct();

    return ret;
}

跳转窗口头文件

cpp 复制代码
#ifndef CLIENT_H
#define CLIENT_H

#include <QWidget>

namespace Ui {
class Client;
}

class Client : public QWidget
{
    Q_OBJECT

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

public slots:
    void jump_solt();

private:
    Ui::Client *ui;
};

#endif // CLIENT_H

跳转窗口函数实现

cpp 复制代码
#include "client.h"
#include "ui_client.h"


Client::Client(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Client)
{
    ui->setupUi(this);
}

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

void Client::jump_solt()
{
    this->show();
}

main函数

cpp 复制代码
#include "LoginUI.h"
#include "client.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    LoginUI* cal = LoginUI::NewInstance();
    int ret = -1;
    Client* c1 = new Client;
    LoginUI::connect(cal, &LoginUI::Jump, c1, &Client::jump_solt);

    if( cal != NULL )
    {       
        cal->show();

        ret = a.exec();

        delete cal;
    }

    return ret;
}
相关推荐
SmartRadio8 小时前
CH585M+MK8000、DW1000 (UWB)+W25Q16的低功耗室内定位设计
c语言·开发语言·uwb
rfidunion8 小时前
QT5.7.0编译移植
开发语言·qt
rit84324998 小时前
MATLAB对组合巴克码抗干扰仿真的实现方案
开发语言·matlab
大、男人8 小时前
python之asynccontextmanager学习
开发语言·python·学习
hqwest9 小时前
码上通QT实战08--导航按钮切换界面
开发语言·qt·slot·信号与槽·connect·signals·emit
一只小bit9 小时前
Qt 常用控件详解:按钮类 / 显示类 / 输入类属性、信号与实战示例
前端·c++·qt·gui
AC赳赳老秦9 小时前
DeepSeek 私有化部署避坑指南:敏感数据本地化处理与合规性检测详解
大数据·开发语言·数据库·人工智能·自动化·php·deepseek
不知道累,只知道类9 小时前
深入理解 Java 虚拟线程 (Project Loom)
java·开发语言
国强_dev10 小时前
Python 的“非直接原因”报错
开发语言·python
YMatrix 官方技术社区10 小时前
YMatrix 存储引擎解密:MARS3 存储引擎如何超越传统行存、列存实现“时序+分析“场景性能大幅提升?
开发语言·数据库·时序数据库·数据库架构·智慧工厂·存储引擎·ymatrix