c++Qt登录页面设计

使用手动连接,将登录框中的取消按钮使用qt4版本的连接到自定义的槽函数中,在自定义的槽函数中调用关闭函数

将登录按钮使用qt5版本的连接到自定义的槽函数中,在槽函数中判断ui界面上输入的账号是否为"admin",密码是否为"123456",如果账号密码匹配成功,则输出"登录成功",并关闭该界面,弹出另一个界面。如果匹配失败,则输出登录失败,并将密码框中的内容清空

widget.h

复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QDebug>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT


signals:
    void  my_signal();//自定义一个信号,只声明,不实现
    void my_jump();//跳转页面的信号
public slots:
    void my_slot();


public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private slots:
    void on_pushButton_clicked();

private:
    Ui::Widget *ui;
};
#endif // WIDGET_H

second.h

复制代码
#ifndef SECOND_H
#define SECOND_H

#include <QWidget>

namespace Ui {
class Second;
}

class Second : public QWidget
{
    Q_OBJECT

public:
    explicit Second(QWidget *parent = nullptr);
    ~Second();
public slots:
    void jump_slot();//第二个页面准备的槽函数声明
private:
    Ui::Second *ui;
};

#endif // SECOND_H

main.cpp

复制代码
#include "widget.h"
#include "second.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    //实例化第二界面
    Second s;
    //连接
    QObject::connect(&w,&Widget::my_jump,&s,&Second::jump_slot);
    return a.exec();
}

widgwt.cpp

复制代码
#include "widget.h"
#include "ui_widget.h"

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

    this->setWindowFlag(Qt::FramelessWindowHint);
    this->setAttribute(Qt::WA_TranslucentBackground);
    //点击按钮关掉窗口
    connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(my_slot()));
    //判断登录是否成功
    connect(ui->pushButton,&QPushButton::clicked,[&](){
        QString username = ui->lineEdit->text();
        QString password = ui->lineEdit_2->text();

        if (username == "admin" && password == "123456")
            {
                qDebug() << "登陆成功" ;
                // 创建并显示主窗口
                emit my_jump();
                // 关闭登录窗口
                this->close();
            }
            else
            {
               qDebug() << "登陆失败" ;
               // 清空密码输入框
                ui->lineEdit_2->clear();
            }



});

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

void Widget::my_slot()
{
    this->close();
}


void Widget::on_pushButton_clicked()
{
}

second.cpp

复制代码
#include "second.h"
#include "ui_second.h"

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

Second::~Second()
{
    delete ui;
}
//第二个页面的槽函数实现
void Second::jump_slot()
{

    //显示第二个界面
    this->show();

}

账号密码错误输出"登陆失败"信息,并清空密码

账号密码正确输出"登录成功"信息,并跳转新界面,关闭登录界面

xmind

相关推荐
Felix_One2 小时前
Qt 串口通信避坑指南:QSerialPort 的 5 个常见问题
qt
樱木Plus1 天前
深拷贝(Deep Copy)和浅拷贝(Shallow Copy)
c++
blasit3 天前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
肆忆_4 天前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
不想写代码的星星4 天前
虚函数表:C++ 多态背后的那个男人
c++
端平入洛6 天前
delete又未完全delete
c++
端平入洛7 天前
auto有时不auto
c++
郑州光合科技余经理8 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1238 天前
matlab画图工具
开发语言·matlab
dustcell.8 天前
haproxy七层代理
java·开发语言·前端