完善登录框
点击登录按钮后,判断账号(admin)和密码(123456)是否一致,如果匹配失败,则弹出错误对话框,文本内容“账号密码不匹配,是否重新登录”,给定两个按钮ok和cancel,点击ok后,会清除密码框中的内容,继续进行登录;如果点击cancel按钮,则关闭界面。
如果账号和密码匹配,则弹出信息对话框,给出提示信息为“登录成功”,给出一个按钮ok,点击ok后,关闭整个登录界面,跳转到其他界面
点击取消按钮后,弹出问题对话框,询问是否确定要退出登录,给出两个按钮,yes|no,点击yes,则直接关闭整个登录界面,如果点击no则进行进行登录
要求:消息对话框,对象版和静态成员函数版至少各实现一个
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include
#include
#include
#include
#include
#include
QT_BEGIN_NAMESPACE
namespace Ui {class MainWindow;}
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
public slots:
//void on_btn1_clicked();
void my_slot1();
private:
Ui::MainWindow *ui;
QLineEdit *edti1;
QLineEdit *edti2;
QPushButton *btn1;
QPushButton *btn2;
QLabel *lab1;
QLabel *lab2;
QLabel *lab3;
signals:
void btn1_signal();
void jump();
};
#endif // MAINWINDOW_H
second.h
#ifndef SECOND_H
#define SECOND_H
#include
namespace Ui {
class second;
}
class second : public QWidget
{
Q_OBJECT
public slots:
void jump_slot();
public:
explicit second(QWidget *parent = nullptr);
~second();
private:
Ui::second *ui;
};
#endif // SECOND_H
main.cpp
#include "mainwindow.h"
#include "second.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
second s;
QObject::connect(&w,&MainWindow::jump,&s,&second::jump_slot);
return a.exec();
}
mainwindow.cpp
#include "mainwindow.h"
#include
#include
#include
#include
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
//界面
this->setFixedSize(800,600); //设置固定尺寸
this->setWindowTitle("摇篮游行"); //设置窗口标签
this->setWindowIcon(QIcon(":/pict/123.webp")); //设置窗口图标
//按钮
btn1 = new QPushButton(QIcon(":/pict/213.jpg"),"登录",this); //构造一个按钮
btn1->resize(100,50); //设置按钮大小
btn1->move(550,420); //移动按钮
btn2 = new QPushButton(QIcon(":/pict/12345.jpg"),"退出",this); //构造一个按钮
btn2->resize(btn1->size());
btn2->move(550,520);
//行编辑器
edti1 = new QLineEdit(this); //构造一个行编辑器
edti1->setPlaceholderText("繁星凝望着海洋");
edti1->resize(300,50);
edti1->move(180,420);
edti2 = new QLineEdit(this); //构造一个行编辑器
edti2->setPlaceholderText("海洋拥抱着风帆");
edti2->resize(300,50);
edti2->move(180,520);
//按钮1事件
connect(btn1,SIGNAL(clicked()),this,SLOT(my_slot1()));
//标签
lab1 = new QLabel(this);
lab1->resize(80,50);
lab1->setPixmap(QPixmap(":/pict/321.webp"));
lab1->setScaledContents(true);
lab1->move(100,420);
lab2 = new QLabel(this);
lab2->resize(80,50);
lab2->setPixmap(QPixmap(":/pict/4321.webp"));
lab2->setScaledContents(true);
lab2->move(100,520);
//gif
QMovie *movie = new QMovie(":/pict/6.gif");
lab3 = new QLabel(this);
lab3->resize(800,400);
lab3->setMovie(movie);
movie->start();
lab3->setScaledContents(true);
lab3->move(0,0);
}
void MainWindow::my_slot1()
{
QMessageBox msgbox;
if(edti1->text()=="admin"&&edti2->text()=="123456")
{
QMessageBox msgbox(QMessageBox::Information,
"摇篮夜行",
"登录成功",
QMessageBox::Ok,
this);
emit jump();
this->close();
}else
{
int ret = QMessageBox::critical(
this,tr("摇篮夜行"),
tr("账号或密码不正确,是否重新登录"),
QMessageBox::Yes | QMessageBox::Cancel,
QMessageBox::Cancel
);
if(ret == QMessageBox::Cancel)
{
int ret2 = QMessageBox::question(
this,
tr("摇篮夜行"),
tr("是否退出"),
QMessageBox::Yes | QMessageBox::No,
QMessageBox::No
);
if(ret2 == QMessageBox::Yes)
{
this->close();
}
}
this->edti1->clear();
this->edti2->clear();
}
}
MainWindow::~MainWindow()
{
}
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();
}