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

Qt制作登录界面-Demo4

程序员文章站 2022-05-06 13:18:36
...

Qt制作登录界面-Demo4

Qt制作局域网即时通讯软件用到的登录界面,模仿网络上html的登录界面,再次进行记录一下,需要的朋友可以拿去用。

效果:

Qt制作登录界面-Demo4

代码:

LogInDemo4.h
#ifndef LOGINDEMO4_H
#define LOGINDEMO4_H

/**
* @FileName      LogInDemo4.h
* @brief         File Description
* @author        Kongdemin
* @date          2020-04-24
*/

#include <QWidget>

namespace Ui {
class LogInDemo4;
}

namespace KDM {
class LogInDemo4 : public QWidget
{
    Q_OBJECT

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

private:
    Ui::LogInDemo4 *ui;

private slots:
    void on_BtnClose_clicked();
    void on_BtnOK_clicked();

signals:
    void LoginOk();
};
}

#endif // LOGINDEMO4_H
LogInDemo4.cpp
#include "LogInDemo4.h"
#include "ui_LogInDemo4.h"
#include <QMovie>
#include <QGraphicsDropShadowEffect>
#include <QMessageBox>
#include <QMouseEvent>
#include <QAction>

/**
* @FileName      LogInDemo4.cpp
* @brief         File Description
* @author        Kongdemin
* @date          2020-04-24
*/

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

    setWindowFlags (Qt::FramelessWindowHint);
    this->setAttribute(Qt::WA_TranslucentBackground, true);
    this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
    QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
    shadow->setOffset(0, 0);
    shadow->setColor(QColor("#444444"));
    shadow->setBlurRadius(30);
    ui->shadowWidget->setGraphicsEffect(shadow);

    ui->BtnOK->setFlat(true);
    ui->lineEditName->setStyleSheet("background-color: rgba(255, 255, 224, 60%);border-radius:2px;border: 0px;font: 75 10pt \"宋体\";");
    ui->lineEditPw->setStyleSheet("background-color: rgba(255, 255, 224, 60%);border-radius:2px;border: 0px;font: 75 10pt \"宋体\";");
    ui->lineEditName->setPlaceholderText(tr("Username"));
    ui->lineEditPw->setPlaceholderText(tr("Password"));

    QAction *nameAction = new QAction( ui->lineEditName);
    nameAction->setIcon(QIcon(":/image/man.png"));
    ui->lineEditName->addAction(nameAction,QLineEdit::LeadingPosition);
    QAction *pwAction = new QAction( ui->lineEditPw);
    pwAction->setIcon(QIcon(":/image/pw.png"));
    ui->lineEditPw->addAction(pwAction,QLineEdit::LeadingPosition);

    ui->lineEditName->setFocus();
    QWidget::setTabOrder(ui->lineEditName, ui->lineEditPw);
    QWidget::setTabOrder(ui->lineEditPw, ui->BtnOK);
    ui->BtnOK->setShortcut(QKeySequence::InsertParagraphSeparator);//将小键盘回车键与登录按钮绑定在一起
    ui->BtnOK->setShortcut(Qt::Key_Enter);//将字母区回车键与登录按钮绑定在一起
    ui->BtnOK->setShortcut(Qt::Key_Return);//将小键盘回车键与登录按钮绑定在一起
}

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


void LogInDemo4::on_BtnClose_clicked()
{
    this->close();
}

void LogInDemo4::on_BtnOK_clicked()
{
    if((ui->lineEditName->text() == "admin") && (ui->lineEditPw->text() == "admin"))
    {
        emit LoginOk();
    }
    else
    {
        QMessageBox msgBox;
        msgBox.setWindowTitle(QString::fromLocal8Bit("警告"));
        msgBox.setText(QString::fromLocal8Bit("请确认用户名或密码!"));
        msgBox.exec();
    }
}
}