华清 Qt day2 9月18

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "second.h"

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 on_btn2_clicked();

    void on_le1_cursorPositionChanged(int arg1, int arg2);

    void on_le2_cursorPositionChanged(int arg1, int arg2);

signals:
    void jump();

private:
    Ui::Widget *ui;

    Second *s1;
};
#endif // WIDGET_H
#ifndef SECOND_H
#define SECOND_H

#include 

namespace Ui {
class Second;
}

class Second : public QDialog
{
    Q_OBJECT

public:
    explicit Second(QWidget *parent = nullptr);
    ~Second();

public slots:
    void jump_slot();

private:
    Ui::Second *ui;
};

#endif // SECOND_H
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    s1 = new Second;
}


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

//登录按钮
void Widget::on_btn1_clicked()
{
    QString username = ui->le1->text();
    QString password = ui->le2->text();
    //登录成功
    if(username=="admin" && password=="123456")
    {
        QMessageBox::information(this,"登录成功","登录成功");
        //跳转到其他界面的代码
        connect(this,&Widget::jump,s1,&Second::jump_slot);
        emit jump();
        this->hide();
    }
    else  //登录失败
    {
        //静态版
        int ret = QMessageBox::critical(this,"登录失败",
                                        "账号密码不匹配,是否重新登录",
                                        QMessageBox::Ok|QMessageBox::Cancel);
        if(ret==QMessageBox::Ok)
        {
            //清楚密码框内容
            ui->le2->clear();
        }
        else if(ret==QMessageBox::Cancel)
        {
            //关闭界面
            close();
        }
    }
}


//取消按钮
void Widget::on_btn2_clicked()
{
    //对象版
    QMessageBox box(QMessageBox::Question,"退出登录","是否确定退出登录?",
                                    QMessageBox::Yes|QMessageBox::No,
                                                                 this);
    int ret = box.exec();
    if(ret == QMessageBox::Yes)
    {
        //关闭界面
        close();
    }
}


//输入账号
void Widget::on_le1_cursorPositionChanged(int arg1, int arg2)
{
    ui->le1->setPlaceholderText("账号");
}

//输入密码
void Widget::on_le2_cursorPositionChanged(int arg1, int arg2)
{
    ui->le2->setPlaceholderText("密码");
    ui->le2->setEchoMode(QLineEdit::Password);
    ui->le2->setMaxLength(7);
}
#include "second.h"
#include "ui_second.h"

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

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

void Second::jump_slot()
{
    this->show();
}
#include "widget.h"

#include 

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

华清 Qt day2 9月18_第1张图片

 华清 Qt day2 9月18_第2张图片

 华清 Qt day2 9月18_第3张图片

 在线思维导图 - GitMind

你可能感兴趣的:(qt,开发语言)