4/26作业

weiget.h

#ifndef WIDGET_H
#define WIDGET_H
 
#include <QWidget>
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
#include <QCheckBox>
 
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
 
class Widget : public QWidget
{
    Q_OBJECT
signals:
    void my_signal();
 
private slots:
    // 账号输入槽函数
    void input_user_name();
 
    // 密码输入槽函数
    void input_pwd();
 
    // 登录按钮槽函数
    void login_slots();
 
    // 取消按钮槽函数
    void cancel_slots();
 
public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
 
private:
    Ui::Widget *ui;
    QLabel *head_portrait; // 头像
    QLabel *account_icon; // 账户图标
    QLabel *pwd_icon; // 密码图标
    QLineEdit *account; // 账户输入框
    QLineEdit *pwd; // 密码输入框
    QCheckBox *autologin; // 自动登录
    QCheckBox *remember_pwd; // 记住密码
    QPushButton *retrieve_pwd; // 找回密码
    QPushButton *login; // 登录
    QPushButton *cancel; // 取消
    QPushButton *sign_in; // 注册账号
 
 
};
#endif // WIDGET_H

weiget.cpp

#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
 
 
 
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    // 设置窗口大小
    this->setFixedSize (500, 400);
    // 设置标题栏
    this->setWindowIcon (QIcon(":/icon/logo.png")); // 设置图标
    this->setWindowTitle ("QQ"); // 设置标题
    // 设置头像
    head_portrait = new QLabel(this);
    head_portrait->resize (80, 80);
    head_portrait->move (209, 80);
    head_portrait->setScaledContents (true);
    head_portrait->setPixmap (QPixmap(":/icon/uericon.png"));
 
    // 设置账号输入图标
    account_icon = new QLabel(this);
    account_icon->resize (30, 30);
    account_icon->move (160, 200);
    account_icon->setScaledContents (true);
    account_icon->setPixmap (QPixmap(":/icon/userName.jpg"));
    // 设置账户输入框
    account = new QLineEdit(this);
    account->move (191, 200);
    account->setPlaceholderText ("账户");
    // 信号与槽函数连接
//    connect (account, SIGNAL(textChanged(QString)), this, SLOT (input_user_name())); // QT4版本,不友好,不检查错误
    connect (account, &QLineEdit::textChanged, this, &Widget::input_user_name); // QT5版本,友好
 
    // 设置密码输入图标
    pwd_icon = new QLabel(this);
    pwd_icon->resize (30, 30);
    pwd_icon->move (160, 250);
    pwd_icon->setScaledContents (true);
    pwd_icon->setPixmap (QPixmap(":/icon/passwd.jpg"));
    // 设置密码输入框
    pwd = new QLineEdit(this);
    pwd->move (191, 250);
    pwd->setPlaceholderText ("密码");
    pwd->setEchoMode (QLineEdit::Password);
    // 信号与槽函数连接
//    connect (pwd, SIGNAL(textChanged(QString)), this, SLOT (input_pwd())); // QT4版本,不友好,不检查错误
    connect (pwd, &QLineEdit::textChanged, this, &Widget::input_pwd); // QT5版本,友好
 
 
    // 设置登录按钮
    login = new QPushButton(QIcon(":/icon/login.png"), "登录", this);
    login->move (160, 300);
    login->setEnabled (false);
    // 信号与槽函数连接
//    connect (login, SIGNAL(clicked()), this, SLOT (login_slots())); // QT4版本,不友好,不检查错误
    connect (login, &QPushButton::clicked, this, &Widget::login_slots); // QT5版本,友好
 
    // 设置取消按钮
    cancel = new QPushButton(QIcon(":/icon/cancel.png"), "取消", this);
    cancel->move (240, 300);
    // 信号与槽函数连接
//    connect (login, SIGNAL(clicked()), this, SLOT (cancel_slots())); // QT4版本,不友好,不检查错误
    connect (cancel, &QPushButton::clicked, this, &Widget::cancel_slots); // QT5版本,友好
 
}
 
Widget::~Widget()
{
    delete ui;
}
 
// 自定义账户输入框槽函数
void Widget::input_user_name()
{
    // 判断密码输入框是否空
    if (account->text ().isEmpty () || pwd->text ().isEmpty ()){
        login->setEnabled (false); // 空,设置登录按钮不可用
    } else {
        login->setEnabled (true); // 不空,设置登录按钮可用
    }
}
 
// 自定义密码输入框槽函数
void Widget::input_pwd()
{
    // 判断账户输入框是否空
    if (account->text ().isEmpty () || pwd->text ().isEmpty ()){
        login->setEnabled (false); // 空,设置登录按钮不可用
    } else {
        login->setEnabled (true); // 不空,设置登录按钮可用
    }
}
 
// 自定义登录按钮槽函数
void Widget::login_slots()
{
    if (account->text () == "admin") {
        if (pwd->text () == "123456") {
            qDebug() << "登录成功";
            this->close ();
        }
    } else {
        qDebug() << "登录失败";
        pwd->clear ();
    }
 
}
 
// 自定义槽函数
void Widget::cancel_slots(){
    this->close ();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

malingshu404

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

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

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

打赏作者

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

抵扣说明:

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

余额充值