QT完善登录界面Ⅱ

功能添加:
1.弹窗提示
2.页面跳转 === 信号的发送,槽函数执行

form.h
 public slots:
    void mySlot();  //槽函数
widget.h
signals:
    void mySignal(QString e);  //自定义属于自己的信号函数
//widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //设置固定大小
    this->setFixedSize(650,450);
    //改变窗体名称
    this->setWindowTitle("客户端");
    //设置窗口图标
    this->setWindowIcon(QIcon(":/WindowsIcon/icon/logo.png"));
    //上半部分背景颜色
    QLabel *lab2 = new QLabel(this);
    lab2->setFixedSize(650, 225);
    lab2->setAutoFillBackground(true); // 开启自动填充背景色功能
    QLinearGradient gradient(0, 0, 650, 225);
    gradient.setColorAt(0.0, QColor(157, 116, 178)); // 渐变起始颜色为红色
    gradient.setColorAt(0.5, QColor(99, 102, 178)); // 渐变中间颜色为黄色
    gradient.setColorAt(1.0, QColor(95, 168, 178)); // 渐变结束颜色为绿色
    QBrush brush(gradient);
    QPalette palette;
    palette.setBrush(QPalette::Background, brush);
    lab2->setPalette(palette);
    //中央logo
    QLabel *lab = new QLabel;
    lab->setPixmap(QPixmap(":/WindowsIcon/icon/1.png"));
    lab->setParent(this);
    lab->resize(125,125);
    lab->setScaledContents(true);//设置组件内容自适应
    lab->move(250,100);
    //用户名logo
    QLabel *lab_username = new QLabel(this);
    lab_username->setPixmap(QPixmap(":/WindowsIcon/icon/userName.jpg"));
    lab_username->resize(50,50);
    lab_username->setScaledContents(true);//设置组件内容自适应
    lab_username->move(100,250);
    //密码logo
    QLabel *lab_pswd = new QLabel(this);
    lab_pswd->setPixmap(QPixmap(":/WindowsIcon/icon/passwd.jpg"));
    lab_pswd->resize(50,50);
    lab_pswd->setScaledContents(true);//设置组件内容自适应
    lab_pswd->move(100,320);
    //用户名
    QLineEdit *line_username = new QLineEdit(this);
    line_username->setPlaceholderText("请输入用户名:"); // 设置提示文本
    line_username->resize(400, 50);
    line_username->move(150, 250);
    line_username->setStyleSheet("border: none; border-bottom: 1px solid black;");
    //密码
    QLineEdit *line_pswd = new QLineEdit(this);
    line_pswd->setPlaceholderText("请输入密码:"); // 设置提示文本
    line_pswd->setEchoMode(QLineEdit::Password);
    line_pswd->resize(400,50);
    line_pswd->move(150,320);
    line_pswd->setStyleSheet("border: none; border-bottom: 1px solid black;");

    //登录按钮
    QPushButton *btn_login = new QPushButton(this);
    btn_login->setIcon(QIcon(":/WindowsIcon/icon/login.png"));
    btn_login->setIconSize(QSize(60, 60)); // 设置图标大小
    btn_login->setFixedSize(60, 60); // 设置按钮大小
    btn_login->move(175, 385);
    btn_login->setStyleSheet("QPushButton { border: none; background-position: center; background-repeat: none; }");
    //取消按钮
    QPushButton *btn_cancel = new QPushButton(this);
    btn_cancel->setIcon(QIcon(":/WindowsIcon/icon/cancel.png"));
    btn_cancel->setIconSize(QSize(60, 60)); // 设置图标大小
    btn_cancel->setFixedSize(60, 60); // 设置按钮大小
    btn_cancel->move(375, 385);
    btn_cancel->setStyleSheet("QPushButton { border: none;  background-position: center; background-repeat: none; }");

    //连接
    connect(btn_login,&QPushButton::clicked,this,[=](){
        QString username = line_username->text();
        QString password = line_pswd->text();
        if(username == "admin" && password == "123456")
        {

            int ret = QMessageBox::information(this,"登录","登录中   ..."
                                               ,QMessageBox::Ok|QMessageBox::Cancel);
            if(ret == QMessageBox::Ok)
            {
                emit mySignal("login success"); // 发射自定义信号,进行页面跳转
            }
            else if(ret == QMessageBox::Cancel)
            {
                line_pswd->clear();
            }

        }
        else if(password == "")
        {
             QMessageBox::warning(this,"warning","密码不能为空");
        }
        else
        {
            QMessageBox::warning(this,"warning","用户名或者密码错误");
            line_pswd->clear();
        }

    });
    connect(btn_cancel,&QPushButton::clicked,this,&Widget::clicked_cancel);
    //将自定义的函数与自定义槽函数进行连接
    connect(this,&Widget::mySignal,[](QString e){
        qDebug()<<e;
    });
}
Widget::~Widget()
{
    delete ui;
}

void Widget::clicked_cancel()
{
//点击取消后,关闭整个界面
    this->close();
}
//form.cpp
#include "form.h"
#include "ui_form.h"



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

}
void Form::mySlot()
{
    this->show();
}
Form::~Form()
{
    delete ui;
}

QT完善登录界面Ⅱ_第1张图片
QT完善登录界面Ⅱ_第2张图片
QT完善登录界面Ⅱ_第3张图片

你可能感兴趣的:(qt,qt,ui,开发语言)