Appearance
将登录框的两个按钮进行实现,登录按钮需要手动连接自定义的槽函数,取消按钮右键转到槽
对于登录按钮对应的槽函数中实现:判断输入的账号是否等于"admin",密码是否为“123456”,如果匹配成功,则输出登录成功后关闭界面
如果匹配失败,输出账号和密码不匹配,请重新输入,并将密码框内容清空
对于取消按钮,在对应的槽函数中,关闭整个界面即可
cpp
#ifndef WIDGET_H
#define WIDGET_H
#include <QDebug>
#include <QIcon>
#include <QLabel>
#include <QLineEdit>
#include <QMessageBox>
#include <QPushButton>
#include <QWidget>
class Widget : public QWidget {
Q_OBJECT
QLabel* logo;
QLabel* label_user;
QLabel* label_pwd;
QLineEdit* edit_user;
QLineEdit* edit_pwd;
QPushButton* btn_login;
QPushButton* btn_cancel;
public:
Widget(QWidget* parent = nullptr);
~Widget();
};
#endif // WIDGET_Hcpp
#include "widget.h"
Widget::Widget(QWidget* parent) : QWidget(parent) {
// 设置 windowicon
this->setWindowIcon(QIcon(":/qq.png"));
this->setWindowTitle("myQQ");
this->setFixedSize(600, 400);
const int input_line_width = 350;
const int username_y = 200;
const int password_y = 275;
this->logo = new QLabel(this);
this->logo->resize(600, 150);
this->logo->setScaledContents(true);
this->logo->setPixmap(QPixmap(":/logo.png"));
this->label_user = new QLabel("账号", this);
this->label_user->setPixmap(QPixmap(":/denglu.png"));
this->label_user->resize(50, 50);
this->label_user->setScaledContents(true);
this->label_user->move(100, username_y);
this->label_pwd = new QLabel("密码", this);
this->label_pwd->setPixmap(QPixmap(":/denglumima.png"));
this->label_pwd->resize(50, 50);
this->label_pwd->setScaledContents(true);
this->label_pwd->move(100, 200);
this->label_pwd->move(100, password_y);
this->edit_user = new QLineEdit(this);
this->edit_user->resize(input_line_width, 50);
this->edit_user->move(150, username_y);
this->edit_user->setPlaceholderText("请输入帐号");
this->edit_pwd = new QLineEdit(this);
this->edit_pwd->resize(input_line_width, 50);
this->edit_pwd->move(150, password_y);
this->edit_pwd->setEchoMode(QLineEdit::Password);
this->edit_pwd->setPlaceholderText("请输入密码");
this->btn_login = new QPushButton("登录", this);
this->btn_login->setIcon(QIcon(":/denglu_1.png"));
this->btn_login->resize(100, 50);
this->btn_login->move(250, 335);
connect(this->btn_login, &QPushButton::clicked, [this]() {
QString username = this->edit_user->text();
QString password = this->edit_pwd->text();
if (username == "admin" && password == "123456") {
QMessageBox::information(this, "登录成功", "登录成功");
} else {
QMessageBox::information(this, "登录失败",
"输入的帐号密码不匹配,请重新输入");
this->edit_pwd->clear();
}
});
this->btn_cancel = new QPushButton("取消", this);
this->btn_cancel->setIcon(QIcon(":/quxiao.png"));
this->btn_cancel->resize(100, 50);
this->btn_cancel->move(375, 335);
connect(this->btn_cancel, &QPushButton::clicked, [this]() { this->close(); });
}
Widget::~Widget() {
delete this->logo;
delete this->label_user;
delete this->label_pwd;
delete this->edit_user;
delete this->edit_pwd;
delete this->btn_login;
delete this->btn_cancel;
}cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char* argv[]) {
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}