Qt製作登錄界面-Demo3

Qt製作登錄界面Demo3

登錄界面製作主要實現了類似QQ等常規登錄界面效果,在此基礎上增加了邊框陰影效果。

效果:
在這裏插入圖片描述

代碼:

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;}");
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章