QT【day2】

完善登录框:

//main头文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include<QDebug>  //信息调试类,用于打印输出
#include<QIcon>   //图标头文件
#include<QPushButton> //按钮类头文件
#include<QLineEdit>  //行编辑器头文件
#include<QLabel>  //标签类头文件
#include <QMovie>
#include <QMessageBox>

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

    QPushButton *btn2 = new QPushButton("取消", this);
    QPushButton *btn1 = new QPushButton("登录", this);
    QLineEdit *edit2 = new QLineEdit("密码",this);
    QLineEdit *edit1 = new QLineEdit("账户",this);
    QLabel *lab3 = new QLabel("密码",this);
    QLabel *lab2 = new QLabel("账号",this);
    QLabel *lab1 = new QLabel(this);

signals:
    //跳转信号
    void jump();

public slots:
    //自定义槽函数
    void my_slot();
    //自定义按钮一的参函数申明
    void btn1_slot();


};
#endif // MAINWINDOW_H

//界面二头文件
#ifndef SECOND_H
#define SECOND_H

#include <QWidget>
#include<QIcon>   //图标头文件

namespace Ui {
class Second;
}

class Second : public QWidget
{
    Q_OBJECT

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

public slots:
    void jump_slot();

private:
    Ui::Second *ui;
};

#endif // SECOND_H

//main.cpp
#include "mainwindow.h"
#include "second.h"

#include <QApplication>

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

//mainwindoe.cpp
#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    //窗口设置
    this->setFixedSize(600,600);   //设置大小
    this->setWindowTitle("MUMU");  //文本内容
    this->setWindowOpacity(0.9);  //透明度
    this->setWindowIcon(QIcon(":/ikun/jaioyou.png"));  //图标

    //QLabel
    lab1->resize(600,230);  //设置大小
    lab1->setPixmap(QPixmap(":/ikun/tou.png")); //插入图片
    lab1->setScaledContents(true); //设置文本自适应

    //账户图标
    lab2->resize(60,40);    //设置大小
    lab2->move(70,280);     //移动位置
    lab2->setAlignment(Qt::AlignCenter);   //居中对齐
    lab2->setPixmap(QPixmap(":/ikun/jiaoyou2.png"));  //插入图片
    lab2->setScaledContents(true); //设置文本自适应

    //密码图标
    lab3->resize(60,40);  //设置大小
    lab3->move(70,340);   //移动位置
    lab3->setAlignment(Qt::AlignCenter);    //居中对齐
    lab3->setPixmap(QPixmap(":/ikun/pas.png"));     //插入图片
    lab3->setScaledContents(true); //设置文本自适应

    //QLineEdit
    //账户行编辑器
    edit1->resize(230,40);      //设置大小
    edit1->move(150,280);       //移动位置

    //密码行编辑器
    edit2->resize(230,40);      //设置大小
    edit2->move(150,340);       //移动位置
    edit2->setEchoMode(QLineEdit::Password);    //设置密文模式

    //QPushbutton
    //登录按钮
    btn1->resize(80,30);    //设置大小
    btn1->move(350,450);    //移动位置
    btn1->setIcon(QIcon(":/ikun/jiaoyou3.png"));    //设置图标

    btn2->resize(btn1->size());     //设置大小
    btn2->move(480,450);        //移动位置
    btn2->setIcon(QIcon(":/ikun/on.png"));      //设置图标

    //手动将取消按钮使用qt4版本连接到自定义的槽函数
    connect(btn2, SIGNAL(clicked()), this, SLOT(my_slot()));;

    //使用qt5版本的连接到自定义槽函数中
    connect(btn1, &QPushButton::clicked, this, &MainWindow::btn1_slot);


}

MainWindow::~MainWindow()
{
}

//自定义槽函数
void MainWindow::my_slot(){
    this->close();
}

//自定义按钮1槽函数
void MainWindow::btn1_slot(){
    if(edit1->text() == "admin" && edit2->text() == "123456"){
        qDebug() << "登录成功";
        //发射跳转信号
        emit jump();
        this->close();
    }else{
        qDebug() << "登录失败";
        QMessageBox::information(this, "提示", "账号或密码错误!");
        edit2->clear();
    }
}



//second.cpp
#include "second.h"
#include "ui_second.h"

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

    this->setFixedSize(600,600);   //大小
    this->setWindowTitle("MUWORLD");  //文本内容
    this->setWindowOpacity(0.9);  //透明度
    this->setWindowIcon(QIcon(":/ikun/jaioyou3.png"));  //图标


    ui->label->setPixmap(QPixmap(":/ikun/R-C.jpg"));
    ui->label->setScaledContents(true); //设置文本自适应
}

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

//跳转函数对应槽函数的定义
void Second::jump_slot()
{
    this->show();
}



默认代码加注释:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
思维导图:
请添加图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值