form.h
#ifndef FORM_H
#define FORM_H
#include <QWidget>
#include <QDebug>
#include <QIcon>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel>
#include <QMovie>
namespace Ui {
class Form;
}
class Form : public QWidget
{
Q_OBJECT
public:
explicit Form(QWidget *parent = nullptr);
~Form();
QLabel *lab1;
QLabel *lab2;
QLabel *lab3;
QPushButton *btn1;
QPushButton *btn2;
QLineEdit *edit1;
QLineEdit *edit2;
QMovie *movie1;
public slots:
void jump_slot();
private:
Ui::Form *ui;
};
#endif // FORM_H
form.cpp
#include "form.h"
#include "ui_form.h"
Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
ui->setupUi(this);
/*
*窗口设置 biaot tubiao
*/
this->resize(QSize(700,500)); //整个窗口的尺寸
this->setWindowTitle("QQ聊天室"); //窗口标题
this->setWindowIcon(QIcon(":/QT/1/q.png")); //窗口图标
/*
*logo
*/
lab1 = new QLabel(" ",this);
lab1->resize(700,200);
lab1->move(1,0);
lab1->setPixmap(QPixmap(":/QT/1/2.jpg"));
lab1->setScaledContents(true); //自适应
}
Form::~Form()
{
delete ui;
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QDebug>
#include<QPushButton>
#include<QLineEdit>
#include<QLabel>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
QLabel *l1;
QLabel *l2;
QLabel *l3;
QLabel *l4;
QLabel *l5;
QLineEdit *e1;
QLineEdit *e2;
QPushButton *b1;
QPushButton *b2;
signals: //该权限下说明要定义信号函数
// void my_signal(); //定义自定义的信号函数
void jump(); //定义自定义的信号函数
public slots:
void jump_slot(); //自定义的槽函数
void cancel_slot(); //自定义的槽函数
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
/*
*窗口设置 biaot tubiao
*/
this->resize(QSize(700,500)); //整个窗口的尺寸
this->setWindowTitle("QQ聊天室"); //窗口标题
this->setWindowIcon(QIcon(":/QT/1/q.png")); //窗口图标
/*
*logo
*/
l1 = new QLabel(" ",this);
l1->resize(700,200);
l1->move(1,0);
l1->setPixmap(QPixmap(":/QT/1/2.jpg"));
l1->setScaledContents(true); //自适应
/*
*账号密码 图标 尺寸,移动,占位符,密文,图标
*/
l2 = new QLabel("账号:",this);
l2->resize(60,60);
l2->move(190,280);
l2->setStyleSheet("backgroup-color:red");
l3 = new QLabel("密码:",this);
l3->resize(60,60);
l3->move(190,330);
l4 = new QLabel(this);
l4->resize(30,30);
l4->move(150,295);
l4->setPixmap(QPixmap(":/QT/1/wodepeizhenshi.png"));
l4->setScaledContents(true); //自适应
l5 = new QLabel(this);
l5->resize(30,30);
l5->move(150,342);
l5->setPixmap(QPixmap(":/QT/1/denglumima.png"));
l5->setScaledContents(true); //自适应
/*
*行编辑器 尺寸,移动
*/
e1 = new QLineEdit(this); //账号
e1->resize(220,30);
e1->move(250,295);
e1->setPlaceholderText("手机号/邮箱");
e2 = new QLineEdit(this); //密码
e2->resize(220,30);
e2->move(250,342);
e2->setPlaceholderText("密码");
e2->setEchoMode(QLineEdit::Password);
/*
*按钮 尺寸,移动,文本
*/
b1 = new QPushButton("登录",this);
b1->resize(80,35);
b1->move(220,400);
b2 = new QPushButton("取消",this);
b2->resize(80,35);
b2->move(370,400);
//======================================================
//转换宏槽函数里面写自己定义的槽函数,不是系统提供的 QT3
connect(b2,SIGNAL(clicked()),this,SLOT(cancel_slot())); //把b2的信号发射到窗口上
//按钮发射点击信号,窗口接收,响应,窗口收到了之后就开始执行btn1_slo槽函数
connect(b1,&QPushButton::clicked,this,&Widget::jump_slot); //qt5的信号函数与槽不用加()
}
Widget::~Widget()
{
}
//槽函数来发射jump信号
void Widget::jump_slot()
{
if(this->e1->text() == "admin" && this->e2->text() == "123456")
{
qDebug() << "登录成功";
emit jump(); //自定义信号
this->close(); //在widget里的this是窗口
}
else
{
qDebug() << "登录失败";
e2->clear();
}
}
void Widget::cancel_slot()
{
this->close();
}
main.cpp
#include "widget.h"
#include "form.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
Form f;
QObject::connect(&w,&Widget::jump,&f,&Form::show);
//窗口发射jump信号,jump信号写在Widget.h里了
//Form里面自带的show()槽函数
return a.exec();
}