QT DAY2

实现登录框中,当登录成功时,关闭登录界面,并跳转到其他界面

1.mainwindow.h文件

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include 
#include 
#include 
#include 
#include 
#include      //文本转语音类

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    QPushButton *btn1;
    QPushButton *btn2;
    QLineEdit *edit1;
    QLineEdit *edit2;
    ~MainWindow();
signals:
    //登录页面跳转函数
    void jump();


private:
    Ui::MainWindow *ui;
    QTextToSpeech *speecher;      //定义一个播报员
public slots:
    void btn1_login();
    void btn2_cancel();
};

#endif // MAINWINDOW_H

2.第二个界面的头文件 third.h

#ifndef THIRD_H
#define THIRD_H

#include 
#include 
#include 
#include 
#include 
#include 

namespace Ui {
class third;
}

class third : public QWidget
{
    Q_OBJECT

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

public slots:
    void jump_slot();//定义有关信号处理的槽函数

private:
    Ui::third *ui;
};

#endif // THIRD_H

3.main.h

#include "mainwindow.h"
#include "third.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    third t;
    //两界面跳转
    QObject::connect(&w,&MainWindow::jump,&t,&third::jump_slot);

    return a.exec();
}

4.mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

void fun()
{

}

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

    //设置窗口标题、图标
    this->setWindowTitle("Fly_Chat");
//    qDebug()<size();//获取原窗口大小:400 300
    this->setWindowIcon(QIcon(":/icon_2/chat-bolt.png"));
    //设置窗口固定大小
    this->setFixedSize(600,500);

    //设置背景logo
    QLabel *lab1=new QLabel("logo",this);
    //设置大小
    lab1->resize(600,200);
    lab1->setPixmap(QPixmap(":/bg/2_scenery.jpg"));
    //设置内容为自适应大小
    lab1->setScaledContents(true);

    //设置账户密码图标
    QLabel *lab2=new QLabel("Username",this);
    lab2->resize(40,40);
    lab2->setPixmap(QPixmap("D:\\My-software6\\01_Icon\\icon\\userName.jpg"));
    lab2->setScaledContents(true);
    //移动组件
    lab2->move(140,270);

    QLabel *lab3=new QLabel("passwd",this);
    lab3->resize(40,40);
    lab3->setPixmap(QPixmap("D:\\My-software6\\01_Icon\\icon\\passwd.jpg"));
    lab3->setScaledContents(true);
    //移动组件
    lab3->move(140,330);

    //给播报员实例化空间
    speecher = new QTextToSpeech(this);

    //设置账户密码框
    edit1=new QLineEdit(this);
    edit1->resize(250,40);
    edit1->move(200,270);
    edit1->setPlaceholderText("账号/手机号/邮箱");

    edit2=new QLineEdit(this);
    edit2->resize(250,40);
    edit2->move(200,330);
    edit2->setPlaceholderText("密码");
    //把密码设置为密文模式
    edit2->setEchoMode(QLineEdit::Password);


    //设置登录与取消
    btn1=new QPushButton;
    btn1->setParent(this);
    btn1->setText("登录");
    btn1->resize(100,40);
    btn1->move(200,400);
    btn1->setIcon(QIcon("D:\\My-software6\\01_Icon\\icon\\login.png"));

    //btn1连接登录函数(qt5版本)
    connect(btn1, &QPushButton::clicked, this, &MainWindow::btn1_login);


    btn2=new QPushButton;
    btn2->setParent(this);
    btn2->setText("取消");
    btn2->resize(100,40);
    btn2->move(350,400);
    btn2->setIcon(QIcon("D:\\My-software6\\01_Icon\\icon\\cancel.png"));
    //取消按钮关闭界面(qt4版本)
    connect(btn2,SIGNAL(clicked()),this,SLOT(btn2_cancel()));

    //* 信号函数也可以连接信号函数
//    connect(btn1,&QPushButton::clicked,this,&MainWindow::jump);
}

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

void MainWindow::btn1_login()
{
   if((edit1->text()=="admin")&&(edit2->text()=="123456"))
   {
       //语音播报edit1按钮中的文本内容
       //speecher->say("真相只有一个");

       qDebug()<<"登录成功";
       qDebug()<<"准备跳转";
       emit jump();//发射跳转信号
       this->close();//关闭登录页面
   }
   else
   {
      qDebug()<<"登录失败";
   }
}

void MainWindow::btn2_cancel()
{
    qDebug()<<"关闭界面";
    this->close();
}

5.third.cpp

#include "third.h"
#include "ui_third.h"

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

    //设置窗口固定大小
    this->setFixedSize(600,500);

    //设置背景logo
    QLabel *lab1=new QLabel("logo",this);
    //设置大小
    lab1->resize(600,200);
    lab1->setPixmap(QPixmap(":/bg/3_river.jpeg"));
    //设置内容为自适应大小
    lab1->setScaledContents(true);

}

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

void third::jump_slot()
{
    qDebug()<<"跳转成功";
    this->show();
}

登录界面

QT DAY2_第1张图片

 跳转界面

QT DAY2_第2张图片

 

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