QTday2

作业

完善登录框

点击登录按钮后,判断账号(admin)和密码(123456)是否一致,如果匹配失败,则弹出错误对话框,文本内容“账号密码不匹配,是否重新登录”,给定两个按钮ok和cancel,点击ok后,会清除密码框中的内容,继续进行登录;如果点击cancel按钮,则关闭界面。

如果账号和密码匹配,则弹出信息对话框,给出提示信息为“登录成功”,给出一个按钮ok,点击ok后,关闭整个登录界面,跳转到其他界面

点击取消按钮后,弹出问题对话框,询问是否确定要退出登录,给出两个按钮,yes|no,点击yes,则直接关闭整个登录界面,如果点击no则进行进行登录

要求:消息对话框,对象版和静态成员函数版至少各实现一个

form.h
#ifndef FORM_H
#define FORM_H
#include<QMessageBox>//消息对话框
#include <QMainWindow>
#include <QWidget>
namespace Ui {
class Form;
}

class Form : public QWidget
{
    Q_OBJECT

public slots:
    void jump_slot();
 void on_bt3_clicked();
public:
    explicit Form(QWidget *parent = nullptr);
    ~Form();



private:
    Ui::Form *ui;
};

#endif // FORM_H
mainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include<QDebug>
#include<QPushButton>
#include<QLineEdit>
#include<QLabel>
#include <QMainWindow>
#include<QMessageBox>//消息对话框                     //输出函数对应的头文件
#include <QIcon>
#include"form.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

signals:
    void jump();//自定义跳转信号函数
private slots:
    void on_btn1_clicked();

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
     QPushButton *btn1;
     QPushButton *btn2;
      QLineEdit *edit1;
       QLineEdit *edit2;
};
#endif // MAINWINDOW_H

 

form.cpp
#include "form.h"
#include "ui_form.h"

Form::Form(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form)
{
    ui->setupUi(this);

}

Form::~Form()
{
    delete ui;
}
//跳转
void Form::jump_slot()
{
    //直接调用静态成员
//    QMessageBox box(QMessageBox::Information,"信息","登录成功",QMessageBox::Yes,
//                    this);

//    box.setDefaultButton(QMessageBox::Yes);//默认的
//    box.setButtonText(QMessageBox::Yes,"OK");

//    int ret2=box.exec();
//    if(ret2==QMessageBox::Yes)
//    {
//        this->close();
//    }
    //connect(ui->btn3,&QPushButton::clicked,func);
     this->show();

}


void Form::on_bt3_clicked()
{
    close();
}
mainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setFixedSize(600,400);//设置固定尺寸
    this->setWindowIcon(QIcon(":/new/prefix1/tuku/yh.png"));//改变左上角图标的位
    this->setWindowTitle("易碗浆糊");//窗口的名字
    //this->setStyleSheet("background-color:pink;");//背景颜色
    QLabel *lab1=new QLabel(this);//实例化一个标签
    lab1->resize(600,170);//重新设置尺寸
    lab1->setStyleSheet("background-color:yellow");
    lab1->setAlignment(Qt::AlignCenter);
    lab1->setPixmap(QPixmap(":/new/prefix1/tuku/fjh.png"));
    lab1->setScaledContents(true);
    this->setStyleSheet("background-color:white;");//背景颜色

    //用户名框
    edit1=new QLineEdit(this);
    edit1->resize(230,45);
    edit1->move(lab1->x()+200,lab1->y()+200);
    edit1->setPlaceholderText("用户名");//设置占位文本
    //密码框
    edit2=new QLineEdit(this);
    edit2->resize(230,45);
    edit2->move(edit1->x(),edit1->y()+75);
    edit2->setPlaceholderText("密码");//设置占位文本
    edit2->setEchoMode(QLineEdit::Password);//设置回显模式
    //实例化一个标签
    QLabel *lab2=new QLabel(this);
    lab2->resize(45,45);//重新设置尺寸
    lab2->setAlignment(Qt::AlignCenter);
    lab2->setPixmap(QPixmap(":/new/prefix1/tuku/wxn.png"));
    lab2->setScaledContents(true);
    lab2->move(lab1->x()+140,lab1->y()+200);
    //实例化一个标签
    QLabel *lab3=new QLabel(this);
    lab3->resize(45,45);//重新设置尺寸
    lab3->setAlignment(Qt::AlignCenter);
    lab3->setPixmap(QPixmap(":/new/prefix1/tuku/wxnnn.png"));
    lab3->setScaledContents(true);
    lab3->move(lab1->x()+140,lab1->y()+275);
    //实例化一个按钮
    //QPushButton *btn1=new QPushButton(this);
    this->btn1=new QPushButton("btn1",this);
    btn1->setText("登录");
    btn1->resize(70,40);
    btn1->move(lab3->x()+80,lab3->y()+70);
    btn1->setIcon(QIcon(":/new/prefix1/tuku/wq.png"));
    connect(btn1,&QPushButton::clicked,this,&MainWindow::on_btn1_clicked);

    // btn1->setStyleSheet("background-color:white;border-radius:10px;");//设置组件背景色

    //实例化一个按钮
    QPushButton *btn2=new QPushButton(this);
    btn2->setText("退出");
    btn2->resize(70,40);
    btn2->move(btn1->x()+130,btn1->y());
    // btn2->setStyleSheet("background-color:white;border-radius:10px;");//设置组件背景色
    btn2->setIcon(QIcon(":/new/prefix1/tuku/wq2.png"));
}
void MainWindow::on_btn1_clicked()
{

    if(edit1->text()=="admin" &&edit2->text()=="12345")
    {

        emit jump();
        this->hide();
    }
    else
    {

        QMessageBox box(QMessageBox::Critical,"密码错误","账号密码不匹配,是否重新登录",
                        QMessageBox::Yes|QMessageBox::No,
                        this);
        box.setButtonText(QMessageBox::Yes,"OK");
        box.setButtonText(QMessageBox::No,"cancel");
        int ret=box.exec();
        if(ret==QMessageBox::Yes)
        {
            edit1->clear();//清空
            edit2->clear();//清空
        }else if(ret==QMessageBox::No)
        {

            int ret1= QMessageBox::question(this,"问题","是否确定要退出登录",QMessageBox::Yes|QMessageBox::No,QMessageBox::No);
            //对用户选中的按钮进行判断
            if(ret1==QMessageBox::Yes)
            {
                this->close();
            }else if(ret1==QMessageBox::No)
            {
                edit1->clear();//清空
                edit2->clear();//清空

            }
        }
    }

}
MainWindow::~MainWindow()
{
    delete ui;
}

 main.cpp

#include "mainwindow.h"
#include "form.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    Form s;//定义第二个界面
    QObject::connect(&w,&MainWindow::jump,&s,&Form::jump_slot);
    return a.exec();
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值