widget.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
widget.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 ();
}