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

Qt制作登录界面-Demo3

程序员文章站 2022-05-06 13:17:24
...
Qt制作登录界面Demo3

登录界面制作主要实现了类似QQ等常规登录界面效果,在此基础上增加了边框阴影效果。

效果:
Qt制作登录界面-Demo3

代码:

LogWidget.h

#ifndef LOGWIDGET_H
#define LOGWIDGET_H

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

#include <QWidget>

QT_BEGIN_NAMESPACE
class QPushButton;
class QLabel;
class QLineEdit;
QT_END_NAMESPACE

class LogWidget : public QWidget
{
    Q_OBJECT
public:
    explicit LogWidget(QWidget *parent = nullptr);
private:
    QWidget *shadowWidget;
    QLabel *_headLabel;
    QLabel *_nameLabel;
    QLabel *_pwdLabel;
    QLineEdit *_nameLineEdit;
    QLineEdit *_pwdLineEdit;
    QPushButton *_cancelBtn;
    QPushButton *_okBtn;
};
#endif // LOGWIDGET_H

LogWidget.cpp

#include "LogWidget.h"
#include <QDebug>
#include <QLineEdit>
#include <QPushButton>
#include <QLabel>
#include <QVBoxLayout>
#include <QGraphicsDropShadowEffect>
/**
* @FileName      LogWidget.cpp
* @brief         File Description 
* @author        Kongdemin
* @date          2020-04-24 
*/
LogWidget::LogWidget(QWidget *parent) : QWidget(parent)
{
    this->setWindowFlags(Qt::FramelessWindowHint | Qt::Window | windowFlags() );
    this->setFixedSize(600,400);

    shadowWidget = new QWidget;
    shadowWidget->setObjectName("shadowWidget");
    shadowWidget->setStyleSheet("QWidget#shadowWidget{background-color:#04203f;border-radius:5px;}");
    QVBoxLayout *layoutShow = new QVBoxLayout(this);
    layoutShow->setMargin(10);
    layoutShow->addWidget(shadowWidget);
    this->setLayout(layoutShow);

    this->setAttribute(Qt::WA_TranslucentBackground, true);
    QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
    shadow->setOffset(0, 0);
    shadow->setColor(QColor("#1037ac"));
    shadow->setBlurRadius(15);
    shadowWidget->setGraphicsEffect(shadow);

    QFont font;
    font.setFamily(QString::fromLocal8Bit("Microsoft YaHei"));
    font.setPointSize(18);

    _headLabel = new QLabel(this);
    _headLabel->setFont(font);
    _headLabel->setAlignment(Qt::AlignCenter);
    _headLabel->setText(QString::fromLocal8Bit("登录测试平台系统"));
    _headLabel->setGeometry(30,65,540,30);
    font.setPointSize(12);
    _nameLabel = new QLabel(this);
    _nameLabel->setFont(font);
    _nameLabel->setAlignment(Qt::AlignJustify);
    _nameLabel->setText(QString::fromLocal8Bit("用户名:"));
    _nameLabel->setGeometry(130,170,70,30);
    _pwdLabel = new QLabel(this);
    _pwdLabel->setFont(font);
    _pwdLabel->setAlignment(Qt::AlignJustify);
    _pwdLabel->setText(QString::fromLocal8Bit("密码:"));
    _pwdLabel->setGeometry(130,210,70,30);
    font.setPointSize(10);
    _nameLineEdit = new QLineEdit(this);
    _nameLineEdit->setFont(font);
    _nameLineEdit->setGeometry(210,170,260,30);
    _pwdLineEdit = new QLineEdit(this);
    _pwdLineEdit->setFont(font);
    _pwdLineEdit->setEchoMode(QLineEdit::Password);
    _pwdLineEdit->setGeometry(210,210,260,30);
    _cancelBtn = new QPushButton(this);
    _cancelBtn->setFont(font);
    _cancelBtn->setText(QString::fromLocal8Bit("取消"));
    _cancelBtn->setGeometry(130,300,90,40);
    _okBtn = new QPushButton(this);
    _okBtn->setFont(font);
    _okBtn->setText(QString::fromLocal8Bit("确定"));
    _okBtn->setGeometry(380,300,90,40);

    connect(_cancelBtn, &QPushButton::clicked, [=](){
        this->close();
    });

    connect(_okBtn, &QPushButton::clicked, [=](){
        if ((_nameLineEdit->text() == "admin") && (_pwdLineEdit->text() == "admin"))
        {
            qDebug() << "login successly!";
        }
    });
    this->setStyleSheet("QLabel{color:white} "
                        "QLineEdit{border:1px solid grey; border-radius:6px;}"
                        "QPushButton{color:white;border:1px solid grey; border-radius:6px;}");
}