新乡营销型网站建设软文发布平台与板块
完善登录框
点击登录按钮后,判断账号(admin)和密码(123456)是否一致,如果匹配失败,则弹出错误对话框,文本内容“账号密码不匹配,是否重新登录”,给定两个按钮ok和cancel,点击ok后,会清除密码框中的内容,继续进行登录;如果点击cancel按钮,则关闭界面。
如果账号和密码匹配,则弹出信息对话框,给出提示信息为“登录成功”,给出一个按钮ok,点击ok后,关闭整个登录界面,跳转到其他界面
点击取消按钮后,弹出问题对话框,询问是否确定要退出登录,给出两个按钮,yes|no,点击yes,则直接关闭整个登录界面,如果点击no则进行进行登录
要求:消息对话框,对象版和静态成员函数版至少各实现一个
登录失败功能、取消键功能
登录成功弹窗、登录后界面跳转
second.h
#ifndef SECOND_H
#define SECOND_H
#include <QWidget>
namespace Ui {
class Second;
}
class Second : public QWidget
{Q_OBJECT
public:explicit Second(QWidget *parent = nullptr);~Second();private:Ui::Second *ui;
public slots:void jump_slot();
};
#endif // SECOND_H
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QMessageBox>
#include<QPushButton>
#include<QLabel>
#include<QLineEdit>
#include "second.h"
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECT
public:Widget(QWidget *parent = nullptr);~Widget();void fun();QLabel *lab1;QLabel *lab2;QLabel *lab3;QLineEdit *edit1;QLineEdit *edit2;QPushButton *btn1;QPushButton *btn2;
private slots:void login_clicked();void cancel_clicked();
private:Ui::Widget *ui;Second *s1;
signals:void jump(); //自定义跳转函数
};
#endif // WIDGET_H
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{QApplication a(argc, argv);Widget w;w.show();return a.exec();
}
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();//展示
}
widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);s1=new Second;connect(this,&Widget::jump,s1,&Second::jump_slot);resize(QSize(960,1200)); //使用匿名对象作为函数参数设置当前组件的尺寸setFixedSize(960,1200); //设置固定尺寸setWindowTitle("miHoYo"); //设置窗口标题setWindowIcon(QIcon(":/icon/qq.png")); //设置窗口图标setStyleSheet("background-color:white;"); //设置样式表setWindowOpacity(2); //设置窗口透明度lab1 = new QLabel(this);lab1->resize(960,532); //重新设置尺寸lab1->setAlignment(Qt::AlignCenter);// lab1->setStyleSheet("background-color:yellow;");lab1->setPixmap(QPixmap(":/icon/logo.png")); //设置图片lab1->setScaledContents(true);lab2 = new QLabel(this);lab2->resize(65,71); //重新设置尺寸lab2->move(lab1->x()+200,lab1->y()+700);//lab2->setStyleSheet("background-color:yellow;");lab2->setPixmap(QPixmap(":/icon/user.jpg")); //设置图片lab2->setScaledContents(true);lab3 = new QLabel(this);lab3->resize(65,71); //重新设置尺寸lab3->move(lab2->x(),lab2->y()+150);lab3->setAlignment(Qt::AlignCenter);//lab3->setStyleSheet("background-color:yellow;");lab3->setPixmap(QPixmap(":/icon/passwd.jpg")); //设置图片lab3->setScaledContents(true);edit1 = new QLineEdit(this);edit1->resize(350,50);edit1->move(lab2->x()+120, lab2->y());edit1->setEchoMode(QLineEdit::Normal); //设置回显模式edit1->setMaxLength(11);edit1->setPlaceholderText("账号/手机/邮箱"); //设置占位文本edit2 = new QLineEdit(this);edit2->resize(350,50);edit2->move(lab3->x()+120, lab3->y());edit2->setEchoMode(QLineEdit::Password); //设置回显模式edit2->setMaxLength(10);edit2->setPlaceholderText("密码"); //设置占位文本btn1 = new QPushButton;//无参构造connect(btn1,&QPushButton::clicked,this,&Widget::login_clicked);btn1->setParent(this); //将当前界面作为父组件btn1->setText("登录"); //设置按钮上的文本内容btn1->resize(100,30); //重新设置组件尺寸btn1->setIcon(QIcon(":/icon/login.png")); //设置图标btn1->move(edit2->x()-20,edit2->y()+150); //移动组件位置btn2 = new QPushButton; //无参构造connect(btn2,&QPushButton::clicked,this,&Widget::cancel_clicked);btn2->setParent(this); //将当前界面作为父组件btn2->setText("取消"); //设置按钮上的文本内容btn2->resize(100,30); //重新设置组件尺寸btn2->setIcon(QIcon(":/icon/cancel.png")); //设置图标btn2->move(btn1->x()+250,btn1->y());}Widget::~Widget()
{delete ui;
}//登录按钮对应的槽函数
void Widget::login_clicked()
{//登录失败if(edit1->text()!="admin"&&edit2->text()!="123456"){//调用构造函数实例化对象QMessageBox box(QMessageBox::Critical,//图标"错误",//对话框标题"账号密码不匹配,是否重新登录",//对话框文本QMessageBox::Yes|QMessageBox::No,//提供的按钮this);//父组件box.setDefaultButton(QMessageBox::Yes);box.setButtonText(QMessageBox::Yes,"OK");box.setButtonText(QMessageBox::No,"cancel");//调用exec函数运行对话框int ret = box.exec();//对结果进行判断if(ret==QMessageBox::Yes){//清空内容edit1->clear();}else{//关闭界面this->close();}}//登录成功else{//调用构造函数实例化对象QMessageBox box(QMessageBox::NoIcon,//图标" ",//对话框标题"登录成功",//对话框文本QMessageBox::Ok,//提供的按钮this);//父组件box.exec();//运行对话框emit jump();//跳转界面this->close();//关闭当前界面}}//退出按钮
void Widget::cancel_clicked()
{int ret=QMessageBox::warning(this,"警告","是否确定要退出登录",QMessageBox::Yes | QMessageBox::No,QMessageBox::No);//对结果进行判断如果点yes则关闭if(ret==QMessageBox::Yes){this->close();//关闭界面}
}