作业
优化登录框:
当用户点击取消按钮,弹出问题对话框,询问是否要确定退出登录,并提供两个按钮,yes|No,如果用户点击的Yes,则关闭对话框,如果用户点击的No,则继续登录
当用户点击的登录按钮,进行账号和密码的匹配,如果匹配成功,则弹出信息对话框,给出信息为,登录成功,并给出一个确定按钮,当用户点击该按钮后,关闭登录界面,弹出另一个界面
当账号和密码不匹配是,给出错误对话框,给出信息为账号和密码不匹配,是否重新登录,并提供两个按钮 Yes|No,如果用户点击了Yes,则清空密码框后,继续登录。如果用户点击的取消,则关闭登录界面
要求:静态成员函数版本和对象版本各至少实现一个
cpp//主界面的头文件 #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QLineEdit> #include <QLabel> #include <QPushButton> #include <QCheckBox> #include <QPainter> #include <QBitmap> #include <QPainterPath> #include <QDebug> #include <QApplication> #include <QMessageBox> class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private slots: void onLoginButtonClicked(); void onCancelButtonClicked(); private: void setupWindow(); void setupWidgets(); void setupPlaceholderText(); bool validateLogin(); QLineEdit *accountEdit; QLineEdit *passwordEdit; QLabel *accountLabel; QLabel *passwordLabel; QPushButton *loginButton; QPushButton *cancelButton; QCheckBox *rememberCheckBox; QCheckBox *autoLoginCheckBox; QLabel *avatarLabel; signals: void jump(); }; #endif // MAINWINDOW_H
cpp//主界面的源文件 #include "mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { // 设置窗口固定尺寸 setFixedSize(600, 800); // 设置窗口标题 setWindowTitle("QQ"); setWindowIcon(QIcon(":/pictrue/Q.jpg")); //设置窗口样式表 setWindowOpacity(0.9); setStyleSheet("background-color:rgb(74, 255, 219);"); //设置背景色 // 头像标签 avatarLabel = new QLabel(this); avatarLabel->setGeometry(230, 150, 151, 151); QPixmap originalPixmap("E:/a-U盘备份/a资料/证件/头像.jpg"); if (!originalPixmap.isNull()) { QPixmap circularPixmap(avatarLabel->size()); circularPixmap.fill(Qt::transparent); QPainter painter(&circularPixmap); painter.setRenderHint(QPainter::Antialiasing); painter.setPen(Qt::NoPen); painter.drawRoundedRect(0, 0, circularPixmap.width(), circularPixmap.height(), circularPixmap.width() / 2, circularPixmap.width() / 2); QPainterPath path; path.addRoundedRect(0, 0, circularPixmap.width(), circularPixmap.height(), circularPixmap.width() / 2, circularPixmap.width() / 2); painter.setClipPath(path); painter.drawPixmap(0, 0, originalPixmap.scaled(circularPixmap.size(), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation)); avatarLabel->setPixmap(circularPixmap); } // 账号标签 accountLabel = new QLabel("账号:", this); accountLabel->setGeometry(60, 380, 81, 60); // 账号输入框 accountEdit = new QLineEdit(this); accountEdit->setGeometry(160, 380, 400, 60); accountEdit->setAlignment(Qt::AlignCenter); accountEdit->setPlaceholderText("请输入账号"); // 设置账号输入框圆角 accountEdit->setStyleSheet("QLineEdit { background-color: white; border-radius:10px }"); // 密码标签 passwordLabel = new QLabel("密码:", this); passwordLabel->setGeometry(60, 480, 81, 60); // 密码输入框 passwordEdit = new QLineEdit(this); passwordEdit->setGeometry(160, 480, 400, 60); passwordEdit->setEchoMode(QLineEdit::Password); passwordEdit->setAlignment(Qt::AlignCenter); passwordEdit->setPlaceholderText("请输入密码"); // 设置密码输入框圆角 passwordEdit->setStyleSheet("QLineEdit { background-color: white; border-radius:10px }"); // 登录按钮 loginButton = new QPushButton("登录", this); loginButton->setGeometry(130, 660, 150, 50); // 设置登录按钮圆角 loginButton->setStyleSheet("QPushButton { background-color: skyblue; border-radius:10px }"); // 取消按钮 cancelButton = new QPushButton("取消", this); cancelButton->setGeometry(330, 660, 150, 50); // 设置取消按钮圆角 cancelButton->setStyleSheet("QPushButton { background-color: skyblue; border-radius:10px }"); // 记住密码复选框 rememberCheckBox = new QCheckBox("记住密码", this); rememberCheckBox->setGeometry(120, 580, 200, 30); // 自动登录复选框 autoLoginCheckBox = new QCheckBox("自动登录", this); autoLoginCheckBox->setGeometry(330, 580, 200, 30); // 连接按钮点击信号到对应的槽函数 bool connectResult1 = connect(loginButton, &QPushButton::clicked, this, &MainWindow::onLoginButtonClicked); bool connectResult2 = connect(cancelButton, &QPushButton::clicked, this, &MainWindow::onCancelButtonClicked); if (!connectResult1 ||!connectResult2) { qDebug() << "信号 - 槽连接失败"; } } MainWindow::~MainWindow() { } bool MainWindow::validateLogin() { QString account = accountEdit->text(); QString password = passwordEdit->text(); // 这里是简单的校验逻辑,可根据实际需求修改 return account == "123456" && password == "654321"; } void MainWindow::onLoginButtonClicked() { if (validateLogin()) { qDebug() << "登录成功"; //调用静态成员函数,创建一个消息对话框 int res = QMessageBox::information(this, "提示", "登陆成功!", QMessageBox::Ok, QMessageBox::Ok); //对用户点击的按钮进行判断 if(res == QMessageBox::Ok) { //关闭当前界面 close(); //发射一个跳转信号 emit jump(); } } else { qDebug() << "登录失败"; //实例化一个消息对话框对象 QMessageBox box(QMessageBox::Critical, "提示", "密码错误!", QMessageBox::Yes | QMessageBox::No, this); //调用成员函数更改相关属性 box.setButtonText(QMessageBox::Yes,"重新登陆"); box.setButtonText(QMessageBox::No,"退出"); //调用exec函数,将对话框展示出来 int res = box.exec(); //对用户点击的按钮进行判断 if(res == QMessageBox::Yes) { qDebug()<<"重新登录"; passwordEdit->clear(); } else if(res == QMessageBox::No) { close(); qDebug()<<"程序已关闭"; } } } void MainWindow::onCancelButtonClicked() { //实例化一个消息对话框对象 QMessageBox box(QMessageBox::Question, "提示", "确定要退出吗?", QMessageBox::Yes | QMessageBox::No, this); //调用成员函数更改相关属性 box.setButtonText(QMessageBox::Yes,"确定"); box.setButtonText(QMessageBox::No,"取消"); //调用exec函数,将对话框展示出来 int res = box.exec(); //对用户点击的按钮进行判断 if(res == QMessageBox::Yes) { close(); qDebug()<<"程序已关闭"; } else if(res == QMessageBox::No) { qDebug()<<"继续登录"; } }
cpp//第二个界面的头文件 #ifndef SECONDWINDOW_H #define SECONDWINDOW_H #include <QWidget> namespace Ui { class SecondWindow; } class SecondWindow : public QWidget { Q_OBJECT public: void jump_slot(); public: explicit SecondWindow(QWidget *parent = nullptr); ~SecondWindow(); private: Ui::SecondWindow *ui; }; #endif // SECONDWINDOW_H
cpp//第二个界面的源文件 #include "secondwindow.h" #include "ui_secondwindow.h" SecondWindow::SecondWindow(QWidget *parent) : QWidget(parent), ui(new Ui::SecondWindow) { ui->setupUi(this); } SecondWindow::~SecondWindow() { delete ui; } void SecondWindow::jump_slot() { this->show(); }
cpp//主函数 #include "mainwindow.h" #include "secondwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; //第一个界面的实例 w.show(); //展示第一个界面 SecondWindow s; //第二个界面的实例 QObject::connect(&w, &MainWindow::jump, &s, &SecondWindow::jump_slot); return a.exec(); }
思维导图