基于Qt的登录框制作

学习Qt过程中制作了登录框,记录于此,便于以后开发重复利用。

效果图:

ui代码:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Login</class>
 <widget class="QDialog" name="Login">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>360</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Dialog</string>
  </property>
  <property name="styleSheet">
   <string notr="true">background-color: qconicalgradient(cx:1, cy:0, angle:0, stop:0.422886 rgba(176, 187, 31, 255), stop:0.810945 rgba(168, 227, 176, 255), stop:0.945274 rgba(255, 255, 255, 255));
border: 2px solid rgba(176, 187, 31, 255); 
border-radius: 10px;</string>
  </property>
  <widget class="QPushButton" name="pushButton_login">
   <property name="geometry">
    <rect>
     <x>60</x>
     <y>450</y>
     <width>240</width>
     <height>50</height>
    </rect>
   </property>
   <property name="styleSheet">
    <string notr="true">font: 87 16pt &quot;Arial Black&quot;;
background-color: rgb(144, 193, 71);

</string>
   </property>
   <property name="text">
    <string>LOGIN</string>
   </property>
   <property name="default">
    <bool>false</bool>
   </property>
   <property name="flat">
    <bool>false</bool>
   </property>
  </widget>
  <widget class="QLabel" name="label_picture">
   <property name="geometry">
    <rect>
     <x>90</x>
     <y>100</y>
     <width>180</width>
     <height>180</height>
    </rect>
   </property>
   <property name="styleSheet">
    <string notr="true">border-radius: 20px;
background-color: rgb(107, 207, 101);
</string>
   </property>
   <property name="text">
    <string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p align=&quot;center&quot;&gt;&lt;span style=&quot; font-size:72pt; font-weight:600; color:#0000ff;&quot;&gt;M&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
   </property>
  </widget>
  <widget class="QLineEdit" name="lineEdit_login_name">
   <property name="geometry">
    <rect>
     <x>90</x>
     <y>320</y>
     <width>180</width>
     <height>35</height>
    </rect>
   </property>
   <property name="styleSheet">
    <string notr="true">background-color: rgb(212, 212, 212);
font: 75 12pt &quot;Aharoni&quot;;
</string>
   </property>
  </widget>
  <widget class="QLineEdit" name="lineEdit_login_pwd">
   <property name="geometry">
    <rect>
     <x>90</x>
     <y>370</y>
     <width>180</width>
     <height>35</height>
    </rect>
   </property>
   <property name="styleSheet">
    <string notr="true">background-color: rgb(208, 208, 208);
font: 75 12pt &quot;Aharoni&quot;;</string>
   </property>
   <property name="frame">
    <bool>false</bool>
   </property>
   <property name="echoMode">
    <enum>QLineEdit::Password</enum>
   </property>
  </widget>
  <widget class="QLabel" name="label_error_info">
   <property name="geometry">
    <rect>
     <x>90</x>
     <y>300</y>
     <width>180</width>
     <height>15</height>
    </rect>
   </property>
   <property name="styleSheet">
    <string notr="true">color: rgb(255, 0, 0);
background-color: rgba(255, 255, 255, 0);
font: 75 10pt &quot;Aharoni&quot;;
border-width:0px; </string>
   </property>
   <property name="text">
    <string/>
   </property>
  </widget>
  <widget class="QPushButton" name="pushButton_window_close">
   <property name="geometry">
    <rect>
     <x>320</x>
     <y>0</y>
     <width>40</width>
     <height>40</height>
    </rect>
   </property>
   <property name="styleSheet">
    <string notr="true">font: 75 12pt &quot;Aharoni&quot;;
background-color: rgba(255, 255, 255, 0);
border-width:0px; </string>
   </property>
   <property name="text">
    <string>X</string>
   </property>
  </widget>
  <widget class="QPushButton" name="pushButton_window_mini">
   <property name="geometry">
    <rect>
     <x>280</x>
     <y>0</y>
     <width>40</width>
     <height>40</height>
    </rect>
   </property>
   <property name="styleSheet">
    <string notr="true">font: 75 12pt &quot;Aharoni&quot;;
background-color: rgba(255, 255, 255, 0);
border-width:0px; </string>
   </property>
   <property name="text">
    <string>—</string>
   </property>
  </widget>
  <widget class="QLabel" name="label_title">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>10</y>
     <width>150</width>
     <height>24</height>
    </rect>
   </property>
   <property name="styleSheet">
    <string notr="true">background-color: rgba(255, 255, 255, 0);
color: rgb(9, 103, 59);
border-width:0px;
font: 87 10pt &quot;Arial Black&quot;; </string>
   </property>
   <property name="text">
    <string/>
   </property>
  </widget>
  <widget class="QCheckBox" name="checkBox_remember_pwd">
   <property name="geometry">
    <rect>
     <x>90</x>
     <y>410</y>
     <width>91</width>
     <height>19</height>
    </rect>
   </property>
   <property name="styleSheet">
    <string notr="true"/>
   </property>
   <property name="text">
    <string>记住密码</string>
   </property>
  </widget>
  <widget class="QCheckBox" name="checkBox_auto_login">
   <property name="geometry">
    <rect>
     <x>190</x>
     <y>410</y>
     <width>91</width>
     <height>19</height>
    </rect>
   </property>
   <property name="text">
    <string>自动登录</string>
   </property>
  </widget>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

类声明:

/*
 * 登录窗口制作,基于QDialog
 * skynet
 * 2019-12-20
 */
#ifndef USERlOGIN_H
#define USERlOGIN_H

#include <QDialog>
#include <QMouseEvent>
#include <QPixmap>
#include <QValidator>

namespace Ui {
class Login;
}

class UserLogin : public QDialog
{
    Q_OBJECT

public:
    explicit UserLogin(QWidget *parent = 0,QString title_name=QObject::tr("USER LOGIN"));
    ~UserLogin();

    void set_window_title(QString temp_title);  //设置窗口标题
    void set_user_picture(QPixmap *pixmap);  //设置用户图片
    void set_user_name_validator(QValidator *validator);  //设置用户名输入验证
    void set_pwd_validator(QValidator *validator);    //设置密码输入验证

    //拖拽窗口
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);


private slots:
    void on_pushButton_window_close_clicked();  //关闭按钮槽
    void on_pushButton_window_mini_clicked();   //最小化按钮槽
    void on_pushButton_login_clicked();      //登录按钮槽

private:
    Ui::Login *ui;

    //拖拽窗口中间数据
    bool   m_bDrag;
    QPoint mouseStartPoint;
    QPoint windowTopLeftPoint;
};

#endif // USERlOGIN_H

类定义:

#include "userLogin.h"
#include "ui_dialog.h"
#include <Qt>

UserLogin::UserLogin(QWidget *parent,QString title_name) :
    QDialog(parent),
    ui(new Ui::Login)
{
    ui->setupUi(this);
    this->setWindowFlags(windowFlags()|Qt::FramelessWindowHint);
    ui->label_title->setText(title_name);
    ui->lineEdit_login_name->setPlaceholderText(QObject::tr("user name"));
    ui->lineEdit_login_pwd->setPlaceholderText(QObject::tr("password"));
    ui->lineEdit_login_name->setAlignment(Qt::AlignCenter);
    ui->lineEdit_login_pwd->setAlignment(Qt::AlignCenter);
}

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

void UserLogin::set_window_title(QString temp_title)
{
    this->ui->label_title->clear();
    this->ui->label_title->setText(temp_title);
}

void UserLogin::set_user_picture(QPixmap *pixmap)
{
    ui->label_picture->clear();
    pixmap->scaled(ui->label_picture->size(),Qt::KeepAspectRatio);
    ui->label_picture->setScaledContents(true);
    ui->label_picture->setPixmap(*pixmap);
}

void UserLogin::set_user_name_validator(QValidator *validator)
{
    ui->lineEdit_login_name->setValidator(validator);
}

void UserLogin::set_pwd_validator(QValidator *validator)
{
    ui->lineEdit_login_pwd->setValidator(validator);
}

void UserLogin::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        m_bDrag = true;
        //获得鼠标的初始位置
        mouseStartPoint = event->globalPos();
        //获得窗口的初始位置
        windowTopLeftPoint = this->frameGeometry().topLeft();
    }
}

void UserLogin::mouseMoveEvent(QMouseEvent *event)
{
    if(m_bDrag)
    {
        //获得鼠标移动的距离
        QPoint distance = event->globalPos() - mouseStartPoint;
        //QPoint distance = event->pos() - mouseStartPoint;
        //改变窗口的位置
        this->move(windowTopLeftPoint + distance);
    }
}

void UserLogin::mouseReleaseEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        m_bDrag = false;
    }
}

void UserLogin::on_pushButton_window_close_clicked()
{
    this->close();
}

void UserLogin::on_pushButton_window_mini_clicked()
{
    this->setWindowState(Qt::WindowMinimized);
}

void UserLogin::on_pushButton_login_clicked()
{
    if(ui->lineEdit_login_name->hasAcceptableInput())
    {
        if(ui->lineEdit_login_pwd->hasAcceptableInput())
        {
            //写入对输入用户名和密码的验证
        }
        else
        {
            ui->label_error_info->setText(QObject::tr("user pwd error"));
        }
    }
    else
    {
        ui->label_error_info->setText(QObject::tr("user name error"));
    }
}

Qt登录框

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值