C++&QT day9

完善登录框

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

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

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

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

widget.cpp

#include "widget.h"


widget::widget(QWidget *parent)
    : QWidget(parent)
{
    s1=new Second;
    connect(this,&widget::jump,s1,&Second::jump_slot);
    this->setFixedSize(550,400);//设置固定尺寸
    this->setWindowTitle("Widget");//设置窗口标题
    this->setWindowIcon(QIcon("D:\\icon\\wodepeizhenshi.png"));//设置窗口图标

    //实例化一个标签
    QLabel *lab1 = new QLabel;
    lab1->setParent(this);
    lab1->resize(550,215);//重新设置尺寸
    //lab1->setAlignment(Qt::AlignCenter);//文本对齐
    lab1->setPixmap(QPixmap("D:\\icon\\logo.png"));
    lab1->setScaledContents(true);//设置内容自适应

    QLabel *lab3 = new QLabel;
    lab3->setParent(this);//指定父组件
    lab3->resize(40,40);//重新设置尺寸
    lab3->move(100,220);
    //lab3->setAlignment(Qt::AlignCenter);
    lab3->setPixmap(QPixmap("D:\\icon\\userName.jpg"));
    lab3->setScaledContents(true);//设置内容自适应

    QLabel *lab2 = new QLabel;
    lab2->setParent(this);
    lab2->resize(40,40);//重新设置尺寸
    lab2->move(100,270);

    lab2->setPixmap(QPixmap("D:\\icon\\passwd.jpg"));
    lab2->setScaledContents(true);//设置内容自适应

    // 实例化一个行编辑器
    edit2 = new QLineEdit(this);
    edit2->resize(250,40);
    edit2->move(lab3->x()+50,lab3->y());
    // 实例化一个行编辑器
    edit1 = new QLineEdit(this);
    edit1->resize(250,40);
    edit1->move(lab2->x()+50,lab2->y());
    edit1->setEchoMode(QLineEdit::Password);//设置回显模式

    //实例化一个按钮并给定图标,文本内容,父组件
    btn1 = new QPushButton(QIcon("D:\\icon\\login.png"), "登录", this);
    btn1->resize(100,50);//设置按钮大小
    btn1->move(150,325);//设置按钮移动位置
    connect(this->btn1,&QPushButton::clicked,this,&widget::my_slot);

    btn2 = new QPushButton(QIcon("D:\\icon\\cancel.png"),"取消", this);
    btn2->resize(btn1->size());
    btn2->move(btn1->x()+150,btn1->y());
    connect(this->btn2,&QPushButton::clicked,this,&widget::on_btn2_clicked);
}
void widget::my_slot()
{
    if(edit2->text().trimmed()==tr("admin") && edit1->text().trimmed()==tr("123456"))
    {
        //1、调用构造函数实例化对象
        QMessageBox box(QMessageBox::Information,//图标
                        "信息对话框",//对话框标题
                        "登录成功",//对话框文本内容
                        QMessageBox::Ok,//提供的按钮
                        this);//父组件
        box.setDefaultButton(QMessageBox::Ok);//将OK设置为默认按钮
        //2、调用exec函数运行对话框
        int ret=box.exec();
        //3、对结果进行判断
        if(ret==QMessageBox::Ok)
        {
            emit jump();
            this->hide();
        }
    }
    else if(edit2->text()!="admin" && edit1->text()!="123456")
    {
        //直接调用静态成员函数完成对话框的实现
        int num=QMessageBox::critical(this,//父组件
                             "错误对话框",//对话框标题
                             "账号密码不匹配,是否重新登录",//文本内容
                             QMessageBox::Ok|QMessageBox::Cancel,//对话框提供的按钮
                             QMessageBox::Ok);//默认选中按钮
        //对用户选中的按钮进行判断
        if(num==QMessageBox::Ok)
        {
            edit2->clear();
            edit1->clear();
        }
        else if(num==QMessageBox::Cancel)
        {
            close();
        }
    }
}
void widget::on_btn1_clicked()
{
    emit jump();
    this->hide();
}
void widget::on_btn2_clicked()
{
    //1、调用构造函数实例化对象
    QMessageBox box(QMessageBox::Question,//图标
                    "问题对话框",//对话框标题
                    "是否确定取消登录?",//对话框文本内容
                    QMessageBox::Yes|QMessageBox::No,//提供的按钮
                    this);//父组件
    box.setDefaultButton(QMessageBox::No);//将no设置为默认按钮
    //2、调用exec函数运行对话框
    int ret=box.exec();
    //3、对结果进行判断
    if(ret==QMessageBox::Yes)
    {
        close();
    }
    else if(ret==QMessageBox::No)
    {
        connect(this->btn2,SIGNAL(clicked()),this,SLOT(close()));
    }
}

widget::~widget()
{
}

second.cpp

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QIcon>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel>
#include <QDebug>
#include <QMessageBox>
#include "second.h"
QT_BEGIN_NAMESPACE
namespace Ui { class widget; }
QT_END_NAMESPACE

class widget : public QWidget
{
    Q_OBJECT
signals://该权限下定义属于自己的信号
    void my_signal(QString msg);//自定义一个有参无返回值的信号函数
private:
    void my_slot();//自定义无参无返回值的槽函数
    void my_slot1();//自定义无参无返回值的槽函数
    void on_btn1_clicked();//自定义的槽函数声明
    void on_btn2_clicked();//自定义的槽函数声明
public:
    widget(QWidget *parent = nullptr);
    ~widget();
signals:
    void jump();//自定义跳转信号函数

private:
    Ui::widget *ui;
    //自定义一个btn1
    QPushButton *btn1;
    QPushButton *btn2;
    QLineEdit *edit2;
    QLineEdit *edit1;
    Second *s1;
};
#endif // WIDGET_H

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QIcon>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel>
#include <QDebug>
#include <QMessageBox>
#include "second.h"
QT_BEGIN_NAMESPACE
namespace Ui { class widget; }
QT_END_NAMESPACE

class widget : public QWidget
{
    Q_OBJECT
signals://该权限下定义属于自己的信号
    void my_signal(QString msg);//自定义一个有参无返回值的信号函数
private:
    void my_slot();//自定义无参无返回值的槽函数
    void my_slot1();//自定义无参无返回值的槽函数
    void on_btn1_clicked();//自定义的槽函数声明
    void on_btn2_clicked();//自定义的槽函数声明
public:
    widget(QWidget *parent = nullptr);
    ~widget();
signals:
    void jump();//自定义跳转信号函数

private:
    Ui::widget *ui;
    //自定义一个btn1
    QPushButton *btn1;
    QPushButton *btn2;
    QLineEdit *edit2;
    QLineEdit *edit1;
    Second *s1;
};
#endif // WIDGET_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 "widget.h"
#include "second.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    widget w;
    w.show();
    return a.exec();
}

思维导图:

猜你喜欢

转载自blog.csdn.net/m0_59031281/article/details/132992290