完善登录框
点击登录按钮后,判断账号(admin)和密码(123456)是否一致,如果匹配失败,则弹出错误对话框,文本内容“账号密码不匹配,是否重新登录”,给定两个按钮ok和cancel,点击ok后,会清除密码框中的内容,继续进行登录;如果点击cancel按钮,则关闭界面。
如果账号和密码匹配,则弹出信息对话框,给出提示信息为“登录成功”,给出一个按钮ok,点击ok后,关闭整个登录界面,跳转到其他界面
点击取消按钮后,弹出问题对话框,询问是否确定要退出登录,给出两个按钮,yes|no,点击yes,则直接关闭整个登录界面,如果点击no则进行进行登录
要求:消息对话框,对象版和静态成员函数版至少各实现一个
加载资源文件
mywnd.h
#ifndef MYWND_H
#define MYWND_H
#include <QWidget>
#include <QMessageBox>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel>
#include <QDebug>
class MyWnd : public QWidget
{
Q_OBJECT
signals:
void my_signal();
void jumpsignal();
public:
MyWnd(QWidget *parent = nullptr);
~MyWnd();
private slots:
void loginbton1_clicked();
void cancelbton2_clicked();
private:
QPushButton *bton1;
QPushButton *bton2;
QLineEdit *edit1;
QLineEdit *edit2;
QLabel *lab1;
QLabel *lab2;
QLabel *lab3;
};
#endif // MYWND_H
second.h
#ifndef SECOND_H
#define SECOND_H
#include <QWidget>
namespace Ui {
class Second;
}
class Second : public QWidget
{
Q_OBJECT
public:
void jump_slot();
public:
explicit Second(QWidget *parent = nullptr);
~Second();
private:
Ui::Second *ui;
};
#endif // SECOND_H
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::jumpsignal,&s,&Second::jump_slot);
return a.exec();
}
mywnd.cpp
#include "mywnd.h"
#include <iostream>
#include <QDebug>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel>
MyWnd::MyWnd(QWidget *parent)
: QWidget(parent)
{
this->setFixedSize(400,350);
this->setWindowTitle("Widget");
this->setWindowIcon(QIcon(":/icon/wodepeizhenshi.png"));
QLabel *lab1 = new QLabel(this);
lab1->resize(400,150);
lab1->setPixmap(QPixmap(":/icon/logo.png"));
lab1->setScaledContents(true);
QLabel *lab2 = new QLabel(this);
lab2->resize(60,55);
lab2->move(75,160);
lab2->setPixmap(QPixmap(":/icon/userName.jpg"));
lab2->setScaledContents(true);
QLabel *lab3 = new QLabel(this);
lab3->resize(60,55);
lab3->move(lab2->x(),lab2->y()+65);
lab3->setPixmap(QPixmap(":/icon/passwd.jpg"));
lab3->setScaledContents(true);
QLineEdit *edit1 = new QLineEdit("admin",this);
edit1->resize(200,55);
edit1->move(lab2->x()+65,lab2->y());
QLineEdit *edit2 = new QLineEdit("123456",this);
edit2->resize(200,55);
edit2->move(lab3->x()+65,lab3->y());
edit2->setEchoMode(QLineEdit::Password);
QPushButton *bton1 = new QPushButton(QIcon(":/icon/login.png"),"登录",this);
bton1->resize(90,55);
bton1->move(edit2->x()+20,edit2->y()+65);
QPushButton *bton2 = new QPushButton(QIcon(":/icon/cancel.png"),"取消",this);
bton2->resize(90,55);
bton2->move(bton1->x()+105,bton1->y());
connect(bton1,&QPushButton::clicked,this,&MyWnd::loginbton1_clicked);
connect(bton2,&QPushButton::clicked,this,&MyWnd::cancelbton2_clicked);
}
void MyWnd::loginbton1_clicked()
{
if(edit1->text()=="admin"&&edit2->text()=="123456"){
QMessageBox sucbox(QMessageBox::Information,
"登录",
"登录成功",
QMessageBox::Ok,
this);
int ret = sucbox.exec();
if(ret == QMessageBox::Ok){
this->close();
emit jumpsignal();
}
}else{
QMessageBox errbox(QMessageBox::NoIcon,
"登录",
"账号密码不匹配,是否重新登录",
QMessageBox::Ok|QMessageBox::Cancel,
this);
int ret = errbox.exec();
if(ret == QMessageBox::Ok){
edit2->clear();
}else if(ret == QMessageBox::Cancel){
this->close();
}
}
}
void MyWnd::cancelbton2_clicked()
{
int ret = QMessageBox::question(this,
"取消",
"是否确定要退出登录",
QMessageBox::Yes|QMessageBox::No,
QMessageBox::Yes);
if(ret == QMessageBox::Yes){
this->close();
}
}
MyWnd::~MyWnd()
{
}
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();
}
测试结果
思维导图