当用户点击取消按钮,弹出问题对话框,询问是否要确定退出登录,并提供两个按钮,yes|No,如果用户点击的Yes,则关闭对话框,如果用户点击的No,则继续登录
当用户点击的登录按钮,进行账号和密码的匹配,如果匹配成功,则弹出信息对话框,给出信息为,登录成功,并给出一个确定按钮,当用户点击该按钮后,关闭登录界面,弹出另一个界面
当账号和密码不匹配是,给出错误对话框,给出信息为账号和密码不匹配,是否重新登录,并提供两个按钮 Yes|No,如果用户点击了Yes,则清空密码框后,继续登录。如果用户点击的取消,则关闭登录界面
要求:静态成员函数版本和对象版本各至少实现一个
1.main.cpp
#include "loginwindow.h"
#include <QApplication>int main(int argc, char *argv[]) {QApplication app(argc, argv);LoginWindow window;window.resize(300, 150);window.setWindowTitle("登录");app.setWindowIcon(QIcon(":/new/prefix1/pit/Character_sheet.png"));window.show();return app.exec();
}
2.loginwindow.h
#ifndef LOGINWINDOW_H
#define LOGINWINDOW_H#include <QWidget>
#include <QPushButton>
#include <QLineEdit>
#include <QVBoxLayout>
#include <QMessageBox>class LoginWindow : public QWidget {Q_OBJECTpublic:LoginWindow(QWidget *parent = nullptr); // 构造函数private slots:void onLoginButtonClicked(); // 登录按钮点击处理void onCancelButtonClicked(); // 取消按钮点击处理private:QLineEdit *usernameEdit; // 用户名输入框QLineEdit *passwordEdit; // 密码输入框QPushButton *loginButton; // 登录按钮QPushButton *cancelButton; // 取消按钮// 静态成员函数版本static void onStaticCancelButtonClicked(QApplication *app);
};#endif // LOGINWINDOW_H
3.loginwindow.cpp
#include "loginwindow.h"
#include "successwindow.h"
#include <QApplication>
#include <QMessageBox>// 构造函数实现
LoginWindow::LoginWindow(QWidget *parent) : QWidget(parent) {// 创建布局和控件QVBoxLayout *layout = new QVBoxLayout(this);usernameEdit = new QLineEdit(this);usernameEdit->setPlaceholderText("账号");passwordEdit = new QLineEdit(this);passwordEdit->setEchoMode(QLineEdit::Password);passwordEdit->setPlaceholderText("密码");loginButton = new QPushButton("登录", this);cancelButton = new QPushButton("取消", this);// 添加控件到布局layout->addWidget(usernameEdit);layout->addWidget(passwordEdit);layout->addWidget(loginButton);layout->addWidget(cancelButton);// 信号与槽连接connect(loginButton, &QPushButton::clicked, this, &LoginWindow::onLoginButtonClicked);connect(cancelButton, &QPushButton::clicked, this, &LoginWindow::onCancelButtonClicked);
}// 登录按钮点击处理
void LoginWindow::onLoginButtonClicked() {QString correctUsername = "123";QString correctPassword = "123";if (usernameEdit->text() == correctUsername && passwordEdit->text() == correctPassword) {QMessageBox::information(this, "登录成功", "欢迎进入系统!", QMessageBox::Ok);this->close(); SuccessWindow *successWindow = new SuccessWindow();successWindow->show();} else {QMessageBox::StandardButton reply;reply = QMessageBox::question(this, "登录失败", "账号和密码不匹配,是否重新登录?", QMessageBox::Yes | QMessageBox::No);if (reply == QMessageBox::Yes) {// 清空密码框passwordEdit->clear();} else {this->close(); }}
}// 取消按钮点击处理
void LoginWindow::onCancelButtonClicked() {// 弹出确认对话框QMessageBox::StandardButton reply;reply = QMessageBox::question(this, "确认退出", "您确定要退出登录吗?", QMessageBox::Yes | QMessageBox::No);if (reply == QMessageBox::Yes) {this->close();}
}// 静态成员函数版本
void LoginWindow::onStaticCancelButtonClicked(QApplication *app) {QMessageBox::StandardButton reply;reply = QMessageBox::question(nullptr, "确认退出", "您确定要退出登录吗?", QMessageBox::Yes | QMessageBox::No);if (reply == QMessageBox::Yes) {app->quit(); }
}
4.successwindow.h
#ifndef SUCCESSWINDOW_H #define SUCCESSWINDOW_H#include <QWidget> #include <QLabel> #include <QVBoxLayout>class SuccessWindow : public QWidget {Q_OBJECTpublic:SuccessWindow(QWidget *parent = nullptr); };#endif
5.successwindow.cpp
#include "successwindow.h"// 构造函数实现
SuccessWindow::SuccessWindow(QWidget *parent) : QWidget(parent) {QVBoxLayout *layout = new QVBoxLayout(this);QLabel *label = new QLabel("登录成功!欢迎使用本系统。", this);layout->addWidget(label);setLayout(layout);setWindowTitle("成功");resize(300, 100);
}