day51——登录,跳转

优化登录框:


当用户点击取消按钮,弹出问题对话框,询问是否要确定退出登录,并提供两个按钮,yes|No,如果用户点击的Yes,则关闭对话框,如果用户点击的No,则继续登录
当用户点击的登录按钮,进行账号和密码的匹配,如果匹配成功,则弹出信息对话框,给出信息为,登录成功,并给出一个确定按钮,当用户点击该按钮后,关闭登录界面,弹出另一个界面
当账号和密码不匹配是,给出错误对话框,给出信息为账号和密码不匹配,是否重新登录,并提供两个按钮 Yes|No,如果用户点击了Yes,则清空密码框后,继续登录。如果用户点击的取消,则关闭登录界面

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
private slots:
    void on_btn1_clicked();
    void close_btn2();
signals:
    void jump();          //自定义跳转函数
private:
    Ui::Widget *ui;
};
#endif // WIDGET_H

form.h

#ifndef FORM_H
#define FORM_H

#include <QWidget>

namespace Ui {
class Form;
}

class Form : public QWidget
{
    Q_OBJECT

public:
    explicit Form(QWidget *parent = nullptr);
    ~Form();
public slots:
    void jump_slot();      //跳转信号对应的槽函数
private:
    Ui::Form *ui;
};

#endif // FORM_H

main.cpp

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

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    Form form;
    QObject::connect(&w,&Widget::jump,&form,&Form::jump_slot);
    w.show();
    return a.exec();
}

widget.cpp

#include "widget.h"
#include"form.h"
#include "ui_widget.h"
#include<QtDebug>
#include <QIcon>
#include <QPushButton>
#include<QLineEdit>
#include<QLabel>
#include<QMovie>
#include<QString>
#include<QMessageBox>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //1、有关QT中的信息调试类的使用
    qDebug("hello %d", 520) ;         //类似于printf的使用
    qDebug() << "hello world" << 520;  //类似于cout

    //2、有关组件尺寸大小的相关内容
    qDebug() << "this->size = "<< this->size();     //800  600
    qDebug()<<"width = "<<this->width()<<"    heigh = "<<this->height()<<endl;   //800   600
    qDebug()<<"width = "<<this->rect().width()<<"    height = "<< this->rect().height();  //800  600

    this->resize(400, 300);        //更改当前界面的尺寸
    this->resize(QSize(1000,800));  //使用类对象进行更改尺寸
    this->setMaximumSize(1000,900);     //设置最大尺寸
    this->setMinimumSize(200,100);         //设置最小尺寸
    this->setFixedSize(500,400);          //设置固定尺寸

    //2、有关组件的名称
    qDebug() << "this.tittle = "<<this->windowTitle();        //获取当前窗口标题
    this->setWindowTitle("My First Window");                  //设置窗口标题

    //3、设置窗体图标
    this->setWindowIcon(QIcon("F:\\QQfile\\pictrue\\pictrue\\logo.png"));
    //this->setWindowFlag(Qt::FramelessWindowHint);      //设置去除头部的窗口
    //this->showMaximized();                       //设置窗口最大化

    //4、设置窗口的样式表
    //this->setStyleSheet("background-color:pink;");
    //this->setWindowOpacity(0.1);                      //设置窗口透明度

    //5、有关窗口的为止
    this->move(200,300);          //相对于整个屏幕左上角进行偏移
    qDebug()<<"this.posion = "<<this->pos();    //当前窗口的为止

    //1、使用无参构造添加一个按钮
    QPushButton *btn1 = new QPushButton;    //无参构造
    btn1->setParent(this);    //给组件指定父组件,让其依附于界面而存在
    btn1->setText("登录");         //给组件设置文本内容
    btn1->resize(QSize(100,50));        //设置按钮组件的大小
    btn1->move(125,300);            //移动组件位置

    //2、构造按钮时给定文本内容以及父组件
    QPushButton *btn2 = new QPushButton("退出", this);
    btn2->resize(btn1->size());
    btn2->move(btn1->x()+150, btn1->y());

    //1、构造一个行编辑器,构造时给定父组件
    QLineEdit *edit1 = new QLineEdit(this);
    //edit1->setText("请输入。。。");       //设置编辑器中的文本内容
    edit1->setPlaceholderText("账号");        //设置编辑器的占位文本
    qDebug() << edit1->text();
    edit1->setObjectName("usrwd");
    edit1->resize(200,40);            //设置尺寸
    edit1->move(btn1->x()+50, btn1->y()-125);       //移动位置
    edit1->setEnabled(true);            //设置可用状态
    //2、构造一个行编辑器,构造时给定父组件以及文本内容
    QLineEdit *edit2 = new QLineEdit("", this);
    qDebug() << edit2->text();              //获取行编辑器中文本内容
    edit2->setObjectName("passwd");
    edit2->setPlaceholderText("密码");
    edit2->resize(edit1->size());
    edit2->move(edit1->x(), edit1->y()+50);
    edit2->setEchoMode(QLineEdit::Password);          //设置回显模式
    //1、实例化一个标签
    QLabel *lab1 = new QLabel("账号", this);
    lab1->resize(50,50);
    lab1->move(edit1->x()-50, edit1->y());
    lab1->setScaledContents(true);              //设置内容自适应
    //2、实例化一个标签
    QLabel *lab2 = new QLabel("密码", this);
    lab2->resize(50,50);
    lab2->move(edit2->x()-50, edit2->y());
    lab2->setScaledContents(true);              //设置内容自适应
    //3、实例化一个标签
    QLabel *lab3 = new QLabel("密码", this);
    lab3->resize(500,150);
    lab3->move(0,0);
    //1、实例化一个动图
    QMovie *movie=new QMovie(":/pictrue/zz.gif");
    lab3->setMovie(movie);
    movie->start();
    lab3->setScaledContents(true);              //设置内容自适应
    connect(btn1, &QPushButton::clicked, this, &Widget::on_btn1_clicked);
    connect(btn2, &QPushButton::clicked, this, &Widget::close_btn2);
    //将按钮的信号与跳转信号连接
        //connect(btn1, &QPushButton::clicked, this, &Widget::jump);

}
void Widget::on_btn1_clicked()
{
    // 获取行编辑器中的内容
    QString str1 = findChild<QLineEdit*>("usrwd")->text();
    QString str2 = findChild<QLineEdit*>("passwd")->text();
    if (str1 == str2 ) {
        qDebug() << "登录成功";
        QMessageBox::information(this, "提示", "登录成功");
        this->close();        //关闭当前界面
        // 打开另一个界面的代码
        emit jump();

    } else {
        int ret = QMessageBox::warning(this, "错误", "账号和密码不匹配,是否重新登录?", QMessageBox::Yes | QMessageBox::No);
        if (ret == QMessageBox::Yes) {
            findChild<QLineEdit*>("usrwd")->clear();
            findChild<QLineEdit*>("passwd")->clear();
        } else {
            this->close();
        }
    }
}

void Widget::close_btn2()
{
    int ret = QMessageBox::warning(this, "退出中", "是否退出?", QMessageBox::Yes | QMessageBox::No);
    if (ret == QMessageBox::Yes) {
        this->close();
    }
}
Widget::~Widget()
{
    delete ui;
}

form.cpp

#include "form.h"
#include "ui_form.h"
#include<QtDebug>
#include <QIcon>
Form::Form(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form)
{
    ui->setupUi(this);
    //1、有关QT中的信息调试类的使用
    qDebug("hello %d", 520) ;         //类似于printf的使用
    qDebug() << "hello world" << 520;  //类似于cout

    //2、有关组件尺寸大小的相关内容
    qDebug() << "this->size = "<< this->size();     //800  600
    qDebug()<<"width = "<<this->width()<<"    heigh = "<<this->height()<<endl;   //800   600
    qDebug()<<"width = "<<this->rect().width()<<"    height = "<< this->rect().height();  //800  600

    this->resize(400, 300);        //更改当前界面的尺寸
    this->resize(QSize(1000,800));  //使用类对象进行更改尺寸
    this->setMaximumSize(1000,900);     //设置最大尺寸
    this->setMinimumSize(200,100);         //设置最小尺寸
    this->setFixedSize(500,400);          //设置固定尺寸

    //2、有关组件的名称
    qDebug() << "this.tittle = "<<this->windowTitle();        //获取当前窗口标题
    this->setWindowTitle("My First Window");                  //设置窗口标题

    //3、设置窗体图标
    this->setWindowIcon(QIcon("F:\\QQfile\\pictrue\\pictrue\\logo.png"));
    //this->setWindowFlag(Qt::FramelessWindowHint);      //设置去除头部的窗口
    //this->showMaximized();                       //设置窗口最大化

    //4、设置窗口的样式表
    //this->setStyleSheet("background-color:pink;");
    //this->setWindowOpacity(0.1);                      //设置窗口透明度

    //5、有关窗口的为止
    this->move(200,300);          //相对于整个屏幕左上角进行偏移
    qDebug()<<"this.posion = "<<this->pos();    //当前窗口的为止

}

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

void Form::jump_slot()
{
    this->show();
}

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值