看下效果图:
直接看代码,需要的直接复制过去,可以直接使用:
#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);
}
}
图片自己网上找就可以了