Qt登录界面实现以及跳转不同界面

先上效果图:
Qt登录界面实现以及跳转不同界面_第1张图片
Qt登录界面实现以及跳转不同界面_第2张图片
Qt登录界面实现以及跳转不同界面_第3张图片
实现的逻辑代码也非常简单,主要就是用到了QDialog(注意在此基础上才可以使用accept()函数和Accepted配合使用)。通过全局变量传递权限的选择。enter是登录界面,Info是登录后需要跳转的界面。下面是完整代码:
enter.cpp

#include "enter.h"
#include "ui_enter.h"

/** 全局变量声明 */
login Index_record;

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

    // 设置无边框
    setWindowFlags(Qt::FramelessWindowHint);

    // 设置窗口标题
    this->setWindowTitle("登录界面");

    // 设置密文输入
    ui->lEdit_passward->setEchoMode(QLineEdit::Password);

    // 添加下拉框内容
//    QString member = "用户,管理员,维护人员";
//    QStringList str = member.split(",");
//    QStringList list;
//    list.append(str);
//    ui->comboBox->addItems(list);
    ui->comboBox->addItem("用户");
    ui->comboBox->addItem("管理员");
    ui->comboBox->addItem("维护人员");

}

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


/**
 * @brief Enter::on_pushButton_2_clicked 取消按键
 */
void Enter::on_pushButton_2_clicked()
{
    this->close();
}


/**
 * @brief Enter::on_pushButton_clicked 登录按键
 */
void Enter::on_pushButton_clicked()
{
    // 普通会员
    if(Index_record.Jurisdiction == 1){
        if(ui->lEdit_user->text() == "123456" && ui->lEdit_passward->text() == "123456"){
            accept();
        }else{
            QMessageBox::warning(this,tr("提示"),tr("用户名或密码错误"),QMessageBox::Ok);
            ui->lEdit_user->clear();
            ui->lEdit_passward->clear();
            ui->lEdit_user->setFocus();
        }
    }

    // 尊贵会员
    if(Index_record.Jurisdiction == 2){
        if(ui->lEdit_user->text() == "2" && ui->lEdit_passward->text() == "2"){
            accept();
        }else{
            QMessageBox::warning(this,tr("提示"),tr("用户名或密码错误"),QMessageBox::Ok);
            ui->lEdit_user->clear();
            ui->lEdit_passward->clear();
            ui->lEdit_user->setFocus();
        }
    }

    // 超级会员
    if(Index_record.Jurisdiction == 3){
        if(ui->lEdit_user->text() == "3" && ui->lEdit_passward->text() == "3"){
            accept();
        }else{
            QMessageBox::warning(this,tr("提示"),tr("用户名或密码错误"),QMessageBox::Ok);
            ui->lEdit_user->clear();
            ui->lEdit_passward->clear();
            ui->lEdit_user->setFocus();
        }
    }


}

/**
 * @brief Enter::on_comboBox_currentIndexChanged 下拉框索引改变
 * @param index
 */
void Enter::on_comboBox_currentIndexChanged(int index)
{
    // 如果权限下拉框改变,就清空用户名和密码,光标默认位置为用户名
    if(ui->comboBox->currentIndex() != 0)
    {
        ui->lEdit_user->clear();
        ui->lEdit_passward->clear();
        ui->lEdit_user->setFocus();
    }

    // 记录下拉框选择的内容,存入全局变量当中
    if(ui->comboBox->currentIndex() == 0){
        Index_record.Jurisdiction = 1;
        qDebug()<<"当前索引"<<Index_record.Jurisdiction;
    }

    if(ui->comboBox->currentIndex() == 1){
        Index_record.Jurisdiction = 2;
        qDebug()<<"当前索引"<<Index_record.Jurisdiction;
    }

    if(ui->comboBox->currentIndex() == 2){
        Index_record.Jurisdiction = 3;
        qDebug()<<"当前索引"<<Index_record.Jurisdiction;
    }
}

enter.h

#ifndef ENTER_H
#define ENTER_H

#include 
#include 
#include 
#include 

namespace Ui {
class Enter;
}

class Enter : public QDialog
{
    Q_OBJECT

public:
    explicit Enter(QWidget *parent = 0);
    ~Enter();

private slots:
    void on_pushButton_2_clicked(); // 取消按键

    void on_pushButton_clicked();   // 登录按键

    void on_comboBox_currentIndexChanged(int index);

private:
    Ui::Enter *ui;
};

class login
{
   public:

    int Jurisdiction;   // 记录选择权限
};

/**  定义全局变量  */

extern login Index_record;

#endif // ENTER_H

Info.cpp

#include "info.h"
#include "ui_info.h"

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


}

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


/**
 * @brief Info::choose 界面选择
 */
void Info::choose()
{
    if(Index_record.Jurisdiction == 1){
        ui->stackedWidget->setCurrentIndex(0);
        this->setWindowTitle("用户");
    }

    if(Index_record.Jurisdiction == 2){
        ui->stackedWidget->setCurrentIndex(1);
        this->setWindowTitle("管理员");
    }

    if(Index_record.Jurisdiction == 3){
        ui->stackedWidget->setCurrentIndex(2);
        this->setWindowTitle("维护人员");
    }

}

Info.h

#ifndef INFO_H
#define INFO_H

#include 
#include "enter.h"

namespace Ui {
class Info;
}

class Info : public QWidget
{
    Q_OBJECT

public:
    explicit Info(QWidget *parent = 0);
    ~Info();

private slots:
    void choose();

private:
    Ui::Info *ui;
};

#endif // INFO_H

mian.h

#include "enter.h"
#include "info.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Enter w;
    if(w.exec() == QDialog::Accepted){
        Info *p = new Info();
        p->show();
    }
    else return 0;

    return a.exec();
}

以上就是完整代码,如果对你有帮助,记得点个赞再走呦~

你可能感兴趣的:(登录界面,界面跳转,qt,ui,c++)