qt制作漂亮的登录界面,仅供参考

看下效果图:
在这里插入图片描述
直接看代码,需要的直接复制过去,可以直接使用:

#ifndef LOGINWGT_H
#define LOGINWGT_H

#include <QWidget>
#include <QLineEdit>
#include <QPushButton>
#include <QLabel>
#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif


class LoginWgt : public QLabel
{
    Q_OBJECT
public:
    explicit LoginWgt(QWidget *parent = nullptr);
    ~LoginWgt();
    void Init();

protected:
    void mousePressEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
signals:

private:
    QPushButton *m_pLoginBtn=nullptr;
    QPushButton *m_pCancelBtn=nullptr;
    QPushButton *m_pRegisterBtn=nullptr;
    QLineEdit *m_pUserNameEdit=nullptr;
    QLineEdit *m_pPasswordEdit=nullptr;

    QPoint m_ClickePoint;
    bool m_bClick=false;


};

#endif // LOGINWGT_H

#include "loginwgt.h"
#include "function.h"
#include <QHBoxLayout>
#include <QFont>
#include <QTextCodec>
#include <QMouseEvent>

LoginWgt::LoginWgt(QWidget *parent) : QLabel(parent)
{
    this->resize(500,400);
    this->setWindowFlag(Qt::FramelessWindowHint);
    QPixmap pixMap(":/Image/loginbackground.jpg");
    this->setPixmap(pixMap);
    Init();

}

LoginWgt::~LoginWgt()
{

}

void LoginWgt::Init()
{
    QPushButton *minBtn=new QPushButton(this);
    minBtn->setFixedSize(40,40);
    minBtn->setIcon(QIcon(":/Image/min.png"));
    QPushButton *maxBtn=new QPushButton(this);
    maxBtn->setFixedSize(40,40);
    maxBtn->setIcon(QIcon(":/Image/max.png"));
    QPushButton *closeBtn=new QPushButton(this);
    closeBtn->setFixedSize(40,40);
    closeBtn->setIcon(QIcon(":/Image/close.png"));

    minBtn->setStyleSheet("QPushButton{border:none}"
            "QPushButton::hover{background:rgb(145,237,253)}"
                );
    maxBtn->setStyleSheet("QPushButton{border:none}"
            "QPushButton::hover{background:rgb(145,237,253)}"
                );
    closeBtn->setStyleSheet("QPushButton{border:none}"
            "QPushButton::hover{background:rgb(145,237,253)}"
                );

    QWidget *titleWgt=new QWidget(this);
    QHBoxLayout *titleLayout=new QHBoxLayout(this);
    titleLayout->addStretch();
    titleLayout->addWidget(minBtn);
    titleLayout->addWidget(maxBtn);
    titleLayout->addWidget(closeBtn);
    titleLayout->setSpacing(2);
    titleLayout->setMargin(0);
    titleWgt->setLayout(titleLayout);
    titleWgt->setStyleSheet("background:rgb(229,241,251)");
    titleWgt->setFixedHeight(35);

    QFont textFont=GetTextFont();
    m_pLoginBtn=new QPushButton(tr("登录"),this);
    m_pCancelBtn=new QPushButton(tr("取消"),this);
    m_pRegisterBtn=new QPushButton(tr("注册"),this);
    m_pUserNameEdit=new QLineEdit(this);
    m_pPasswordEdit=new QLineEdit(this);

    m_pLoginBtn->setStyleSheet("border:none;background:rgb(145,237,253)");
    m_pCancelBtn->setStyleSheet("border:none;background:rgb(145,237,253)");

    m_pLoginBtn->setFont(textFont);
    m_pCancelBtn->setFont(textFont);
    m_pRegisterBtn->setFont(textFont);
    m_pUserNameEdit->setFont(textFont);
    m_pPasswordEdit->setFont(textFont);

    m_pLoginBtn->setFixedSize(128,27);
    m_pCancelBtn->setFixedSize(128,27);
    m_pUserNameEdit->setFixedSize(250,27);
    m_pPasswordEdit->setFixedSize(250,27);
    m_pRegisterBtn->setFixedSize(40,27);
    m_pRegisterBtn->setStyleSheet("border:none;color:darkred");

    m_pUserNameEdit->setPlaceholderText(tr("请输入用户名"));
    m_pPasswordEdit->setPlaceholderText(tr("请输入密码"));

    m_pUserNameEdit->setStyleSheet("background:rgba(145,237,253,0.5);border:none;border-radius:2px");
    m_pPasswordEdit->setStyleSheet("background:rgba(145,237,253,0.5);border:none;border-radius:2px");

    QFont titleFont=GetTitleFont();
    QLabel *titleLabel=new QLabel(tr("欢迎"));
    titleLabel->setAlignment(Qt::AlignCenter);
    titleLabel->setFont(titleFont);
    titleLabel->setStyleSheet("color:red");

    //titleLabel->setFixedSize(200,50);

//    QLabel *userNameLabel=new QLabel(tr("用户名: "),this);
//    QLabel *passwordLabel=new QLabel(tr("密码: "),this);
//    userNameLabel->setFont(textFont);
//    passwordLabel->setFont(textFont);

//    userNameLabel->setStyleSheet("color:red");
//    passwordLabel->setStyleSheet("color:red");

//    userNameLabel->setFixedSize(50,27);
//    passwordLabel->setFixedSize(50,27);

    QWidget *userNameWgt=new QWidget(this);
    QHBoxLayout *userNameLayout=new QHBoxLayout(this);
    //userNameLayout->addWidget(userNameLabel);
    userNameLayout->addWidget(m_pUserNameEdit);
    userNameLayout->setMargin(0);
    userNameLayout->setSpacing(5);
    userNameWgt->setLayout(userNameLayout);

    QWidget *passWordWgt=new QWidget(this);
    QHBoxLayout *passWordLayout=new QHBoxLayout(this);
    //passWordLayout->addWidget(passwordLabel);
    passWordLayout->addWidget(m_pPasswordEdit);
    passWordLayout->setMargin(0);
    passWordLayout->setSpacing(5);
    passWordWgt->setLayout(passWordLayout);

    QWidget *btnWgt=new QWidget(this);
    QHBoxLayout *btnLayout=new QHBoxLayout(this);
    btnLayout->addStretch();
    btnLayout->addWidget(m_pLoginBtn);
    btnLayout->addWidget(m_pCancelBtn);
    btnLayout->setSpacing(20);
    btnLayout->setMargin(0);
    btnLayout->addStretch();
    btnWgt->setLayout(btnLayout);

    QWidget *RegisterWgt=new QWidget(this);
    QHBoxLayout *RegisterLayout=new QHBoxLayout(this);
    QLabel *regisLabel=new QLabel(tr("如果你还没有账号,请点击"));
    regisLabel->setFixedWidth(145);
    regisLabel->setFont(textFont);
    RegisterLayout->addWidget(regisLabel);
    RegisterLayout->addWidget(m_pRegisterBtn);
    RegisterLayout->setMargin(10);
    RegisterLayout->setSpacing(0);
    RegisterLayout->addStretch();
    RegisterWgt->setLayout(RegisterLayout);

    QVBoxLayout *mainLayout=new QVBoxLayout(this);
    mainLayout->addWidget(titleWgt);
    mainLayout->addWidget(titleLabel);
    mainLayout->addWidget(userNameWgt);
    mainLayout->addWidget(passWordWgt);
    mainLayout->addWidget(btnWgt);
    mainLayout->addWidget(RegisterWgt);
    mainLayout->setMargin(0);
    mainLayout->setSpacing(5);
    this->setLayout(mainLayout);
}

void LoginWgt::mousePressEvent(QMouseEvent *event)
{
    if(event->button()==Qt::LeftButton)
    {
        m_ClickePoint=event->globalPos()-this->pos();
        m_bClick=true;
    }
}

void LoginWgt::mouseReleaseEvent(QMouseEvent *event)
{
    m_bClick=false;
}

void LoginWgt::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons()==Qt::LeftButton&&m_bClick)
    {
        this->move(event->globalPos()-m_ClickePoint);
    }
}

图片自己网上找就可以了

  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

加油小杜(接qt定制功能,单模块开发等)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值