1.继续完善登录框,当登录成功时,关闭登录界面,跳转到新的界面中,来回切换页面;
---mychat.h chatroom.h---两个页面头文件
#ifndef MYCHAT_H
#define MYCHAT_H#include <QWidget>
#include <QDebug> //打印信息
#include <QIcon> //图标
#include <QPushButton> //按钮
#include <QLineEdit> //行编辑器类
#include <QLabel> //标签类class Mychat : public QWidget
{Q_OBJECTpublic:Mychat(QWidget *parent = nullptr);~Mychat();QLabel *lab1; //标签类,设置背景QLabel *lab2; //设置账号图标QLabel *lab3; //设置密码图标QLineEdit *edit1; //行编辑器类,设置明文账号QLineEdit *edit2; //设置密文密码QPushButton *but1; //按钮类,设置登录按钮QPushButton *but2; //设置取消按钮signals:void my_signal();void login_jump(); //登录跳转信号public slots:void login_slot(); //发送跳转信号槽void cancle_slot();void returnjump_slot(); //接收返回信号槽};
#endif // MYCHAT_H
#ifndef CHATROOM_H
#define CHATROOM_H#include <QWidget>namespace Ui {
class Chatroom;
}class Chatroom : public QWidget
{Q_OBJECTpublic:explicit Chatroom(QWidget *parent = nullptr);~Chatroom();signals:void return_jump(); //返回跳转信号public slots:void loginjump_slot(); //接收登录跳转槽private slots:void on_returnjump_clicked(); //发送返回跳转信号槽private:Ui::Chatroom *ui;
};#endif // CHATROOM_H
---mychat.cpp chatroom.cpp---两个页面源文件
#include "mychat.h"Mychat::Mychat(QWidget *parent): QWidget(parent)
{//对组件界面的设置qDebug()<<this->size();this->setFixedSize(this->size()); //固定界面尺寸this->setWindowOpacity(0.9); //透明度//设置窗口this->setWindowTitle(" 轻松一刻的你"); //窗口内容this->setWindowIcon(QIcon(":/qt_icon/qie.png")); //窗口图标//设置界面背景,lab1lab1 = new QLabel;lab1->setParent(this); //将当前组件设置为父组件lab1->resize(700,200); //更改标签大小lab1->setPixmap(QPixmap(":/qt_icon/background.jpg"));lab1->setScaledContents(true); //自动适应空间//设置登录页面//账号图标lab2lab2 = new QLabel;lab2->setParent(this); //将当前组件设置为父组件//lab2->setStyleSheet("background-color:blue;"); //参考lab2->resize(50,40); //设置大小lab2->move(170,250); //位置lab2->setPixmap(QPixmap(":/qt_icon/name.png"));lab2->setScaledContents(true); //自动适应空间//密码图标lab3lab3 = new QLabel(this);//lab3->setStyleSheet("background-color:blue;"); //参考lab3->resize(lab2->size()); //设置大小lab3->move(170,320); //位置lab3->setPixmap(QPixmap(":/qt_icon/pwd.png"));lab3->setScaledContents(true); //自动适应空间//行文本设置//输入账号,edit1edit1 = new QLineEdit(this);edit1->move(260,250); //位置qDebug()<<edit1->size(); //获取大小edit1->resize(220,40); //设置大小edit1->setPlaceholderText("账号"); //占位提示//输入密码,edit2edit2 = new QLineEdit(this);edit2->move(260,320); //位置edit2->resize(edit1->size()); //设置大小edit2->setEchoMode(QLineEdit::Password); //设置密文edit2->setPlaceholderText("密码"); //占位提示//登录按钮but1but1 = new QPushButton(this);qDebug()<<but1->size(); //获取大小//but1->setStyleSheet("background-color:red;"); //参考but1->move(340,400); //位置but1->resize(70,40); //设置大小but1->setText("登录"); //文本but1->setIcon(QIcon(":/qt_icon/login.png"));//取消按钮but2but2 = new QPushButton(this);//but2->setStyleSheet("background-color:red;"); //参考but2->move(440,400); //位置but2->resize(but1->size()); //设置大小but2->setText("取消"); //文本but2->setIcon(QIcon(":/qt_icon/cancle.png"));//取消按钮函数连接my_slot(),qt4// connect(btn3,SIGNAL(clicked()),this,SLOT(my_slot())); //与自定义槽函数相连connect(but2,SIGNAL(clicked()),this,SLOT(cancle_slot()));//登录按钮函数连接my_slot1(),qt5connect(but1,&QPushButton::clicked,this,&Mychat::login_slot);}Mychat::~Mychat()
{
}//取消按钮槽函数
void Mychat::cancle_slot()
{this->close();
}//登录按钮槽函数
void Mychat::login_slot()
{if(edit1->text() == "admin" && edit2->text() == "123456"){qDebug()<<"登陆成功!";emit login_jump();this->close();}else{qDebug()<<"登陆失败,请重新登录!";edit2->clear();}
}//接收返回信号槽
void Mychat::returnjump_slot()
{this->show();
}
#include "chatroom.h"
#include "ui_chatroom.h"Chatroom::Chatroom(QWidget *parent) :QWidget(parent),ui(new Ui::Chatroom)
{ui->setupUi(this);
}Chatroom::~Chatroom()
{delete ui;
}void Chatroom::loginjump_slot() //接收登录信号槽
{this->show();
}void Chatroom::on_returnjump_clicked() //发送疯返回信号槽
{emit return_jump();this->close();
}
---main.cpp---两个页面测试文件
#include "mychat.h"
#include "chatroom.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);Mychat w;w.show();Chatroom c; //实例化chatroom界面QObject::connect(&w,&Mychat::login_jump,&c,&Chatroom::loginjump_slot); //连接登录调转信号和槽QObject::connect(&c,&Chatroom::return_jump,&w,&Mychat::returnjump_slot); //连接返回调转信号和槽return a.exec();
}
2.新建一个工程文件,将默认提供的代码加上注释信息;
工程管理文件---
头文件---
源文件---
主函数---
3.今日份Qt思维导图;