思维导图

小练习
完善登录框
点击登录按钮后,判断账号(admin)和密码(123456)是否一致,如果匹配失败,则弹出错误对话框,文本内容“账号密码不匹配,是否重新登录”,给定两个按钮ok和cancel,点击ok后,会清除密码框中的内容,继续进行登录;如果点击cancel按钮,则关闭界面。
如果账号和密码匹配,则弹出信息对话框,给出提示信息为“登录成功”,给出一个按钮ok,点击ok后,关闭整个登录界面,跳转到其他界面
点击取消按钮后,弹出问题对话框,询问是否确定要退出登录,给出两个按钮,yes|no,点击yes,则直接关闭整个登录界面,如果点击no则进行进行登录
要求:消息对话框,对象版和静态成员函数版至少各实现一个
mywnd.h
#ifndef MYWND_H
#define MYWND_H#include <QMainWindow>
#include<QLabel>
#include<QLineEdit>
#include<QPushButton>
#include<QDebug>
#include<QMessageBox>
#include"second.h"QT_BEGIN_NAMESPACE
namespace Ui { class MyWnd; }
QT_END_NAMESPACEclass MyWnd : public QMainWindow
{Q_OBJECTsignals:void btn1_signal();public slots:void btn1_slot();void btn2_slot();public:MyWnd(QWidget *parent = nullptr);~MyWnd();private:Ui::MyWnd *ui;QPushButton *btn1;QPushButton *btn2;QLineEdit *edit1;QLineEdit *edit2;QLabel *lab1;QLabel *lab2;QLabel *lab3;Second *s1;
};
#endif // MYWND_H
second.h
#ifndef SECOND_H
#define SECOND_H#include <QWidget>namespace Ui {
class Second;
}class Second : public QWidget
{Q_OBJECTpublic slots:void jump_slot();public:explicit Second(QWidget *parent = nullptr);~Second();private:Ui::Second *ui;
};#endif // SECOND_H
mywnd.cpp
#include "mywnd.h"
#include "ui_mywnd.h"MyWnd::MyWnd(QWidget *parent): QMainWindow(parent), ui(new Ui::MyWnd)
{ui->setupUi(this);//将当前界面的信号,与s1界面的槽函数进行连接s1=new Second;connect(this,&MyWnd::btn1_signal,s1,&Second::jump_slot);//对页面进行修改this->setFixedSize(QSize(800,700)); //固定文件框的大小this->setWindowTitle("华清远见"); //设置文件的标题this->setWindowIcon(QIcon(":/icon/wodepeizhenshi.png")); //为标题加图片//设置logo图片lab1=new QLabel(this); //构造一个lab,指定父组件lab1->resize(800,250); //设置图片尺寸lab1->setPixmap(QPixmap(":/icon/logo.png")); //设置logo图片lab1->setScaledContents(true); //设置图片自适应//设置username图片lab2=new QLabel(this); //构造一个lab,指定父组件lab2->resize(50,50); //设置图片尺寸lab2->move(230,300); //设置图片位置lab2->setPixmap(QPixmap(":/icon/username.jpg")); //设置logo图片lab2->setScaledContents(true); //设置图片自适应//设置passwd图片lab3=new QLabel(this); //构造一个lab,指定父组件lab3->resize(50,50); //设置图片尺寸lab3->move(230,380); //设置图片位置lab3->setPixmap(QPixmap(":/icon/passwd.jpg")); //设置logo图片lab3->setScaledContents(true); //设置图片自适应//设置username输入文本框edit1=new QLineEdit(this); //构造一个行编辑器,指定父组件edit1->resize(300,50); //设置行编辑器大小edit1->move(300,300); //设置行编辑器位置edit1->setEchoMode(QLineEdit::Normal); //设置明文模式edit1->setPlaceholderText("username"); //设置占位符//设置passwd输入文本框edit2=new QLineEdit(this); //构造一个行编辑器,指定父组件edit2->resize(300,50); //设置行编辑器大小edit2->move(300,380); //设置行编辑器位置edit2->setEchoMode(QLineEdit::Password); //设置密文模式edit2->setPlaceholderText("passwd"); //设置占位符//设置登录按钮btn1=new QPushButton("登录",this); //构造登录按键,指定父组件btn1->resize(380,70); //设置按键大小btn1->setIcon(QIcon(":/icon/login.png")); //设置按键图标btn1->move(lab3->x(),lab3->y()+80); //设置按键位置//设置取消按钮btn2=new QPushButton("取消",this); //构造取消按键,指定父组件btn2->resize(380,70); //设置按键大小btn2->setIcon(QIcon(":/icon/cancel.png")); //设置按键图标btn2->move(btn1->x(),btn1->y()+90); //设置按键位置//登录按键的信号与槽连接connect(btn1,&QPushButton::clicked,this,&MyWnd::btn1_slot);//取消按键的信号与槽连接connect(btn2,&QPushButton::clicked,this,&MyWnd::btn2_slot);
}MyWnd::~MyWnd()
{delete ui;
}void MyWnd::btn1_slot(){QMessageBox box(this); //基于属性版本实现消息对话框if(edit1->text()=="admin"&&edit2->text()=="123456"){box.setIcon(QMessageBox::Information);box.setWindowTitle("登陆成功");box.setText("登陆成功");box.setStandardButtons(QMessageBox::Ok);box.setDefaultButton(QMessageBox::Ok);box.exec();emit btn1_signal(); //发射跳转的信号this->hide(); //隐藏第一个页面}else{box.setIcon(QMessageBox::Question);box.setWindowTitle("匹配错误");box.setText("账号密码不匹配,是否重新登录?");box.setStandardButtons(QMessageBox::Ok|QMessageBox::Cancel);box.setDefaultButton(QMessageBox::Ok);int ret=box.exec();if(ret==QMessageBox::Ok){edit1->clear(); //清空用户框中的内容edit2->clear(); //清空密码框中的内容}else{this->close(); //关闭页面}}
}void MyWnd::btn2_slot(){int ret=QMessageBox::warning(this, //基于静态成员函数版本的消息对话框"是否退出","是否确定要退出登录?",QMessageBox::Yes|QMessageBox::No,QMessageBox::No);if(ret==QMessageBox::Yes){this->close();}
}
second.cpp
#include "second.h"
#include "ui_second.h"Second::Second(QWidget *parent) :QWidget(parent),ui(new Ui::Second)
{ui->setupUi(this);
}Second::~Second()
{delete ui;
}void Second::jump_slot(){this->show();
}
main.cpp
#include "mywnd.h"
#include "second.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);MyWnd w;w.show();Second s;//在主调函数中将第一个界面的信号与第二个界面的槽函数连接//QObject::connect(&w,&MyWnd::btn1_signal,&s,&Second::jump_slot);return a.exec();
}