思维导图
练习
实现登录框中,当登录成功时,关闭登录界面,并跳转到其他界面
second.h
#ifndef SECOND_H
#define SECOND_H
#include <QWidget>
namespace Ui {
class Second;
}
class Second : public QWidget
{
Q_OBJECT
public:
explicit Second(QWidget *parent = 0);
~Second();
public slots:
void jump_slot(); //定义有关处理跳转信号的槽函数
private:
Ui::Second *ui;
};
#endif // SECOND_H
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QDebug> //信息调试类
#include <QIcon> //图标类
#include <QPushButton> //按钮类
#include <QLabel> //标签类
#include <QLineEdit>
#include <QCheckBox>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public slots:
void slot_login();
void slot_cancel();
signals:
void jump();
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
QLineEdit *edit1;
QLineEdit *edit2;
QPushButton *btn_login;
QPushButton *btn_cancel;
};
#endif // WIDGET_H
main.cpp
#include "widget.h"
#include "second.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w; //第一个界面
w.show(); //展示第一个界面
Second s; //第二个界面
//将第一个界面的信号与第二个界面的槽函数进行连接
QObject::connect(&w,&Widget::jump,&s,&Second::jump_slot);
return a.exec();
}
second.cpp
#include "second.h"
#include "ui_second.h"
Second::Second(QWidget *parent) :
QWidget(parent),
ui(new Ui::Second)
{
ui->setupUi(this);
}
Second::~Second()
{
delete ui;
}
//处理跳转信号函数对应的槽函数
void Second::jump_slot()
{
this->show();
}
widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
//设置固定尺寸
this->setFixedSize(540,410);
//设置窗口标题
this->setWindowTitle("HQYJ");
//设置窗口图标
this->setWindowIcon(QIcon("D:/software/QT/Tools/QtCreator/bin/C++/day1/homework/icon/login.png"));
//调用标签的构造函数,构造时顺便给定文本内容以及父组件
QLabel *lab1 = new QLabel(this) ;
lab1->resize(540,160);
//更改标签中的内容
lab1->setPixmap(QPixmap("D:/software/QT/Tools/QtCreator/bin/C++/day1/homework/icon/logo.png"));
//设置内容为自适应
lab1->setScaledContents(true);
//调用标签的构造函数,构造时顺便给定文本内容以及父组件
QLabel *lab3 = new QLabel(this) ;
lab3->resize(40,40);
//更改标签中的内容
lab3->setPixmap(QPixmap("D:/software/QT/Tools/QtCreator/bin/C++/day1/homework/icon/userName.jpg"));
//设置内容为自适应
lab3->setScaledContents(true);
//移动组件
lab3->move(100,180);
//调用行编辑器的构造函数
edit1 = new QLineEdit("QQ号/微信/邮箱",this);
//重新设置大小
edit1->resize(300,40);
//移动
edit1->move(150,180);
//调用标签的构造函数,构造时顺便给定文本内容以及父组件
QLabel *lab4 = new QLabel(this) ;
lab4->resize(40,40);
//更改标签中的内容
lab4->setPixmap(QPixmap("D:/software/QT/Tools/QtCreator/bin/C++/day1/homework/icon/passwd.jpg"));
//设置内容为自适应
lab4->setScaledContents(true);
//移动组件
lab4->move(100,240);
//调用行编辑器的构造函数
edit2 = new QLineEdit("密码",this);
//重新设置大小
edit2->resize(edit1->size());
//移动
edit2->move(150,240);
//将文本内容设置密文模式
edit2->setEchoMode(QLineEdit::Password);
//调用按钮构造函数,在构造按钮的同时顺便加上文本内容以及父组件
QPushButton *btn1 = new QPushButton("找回密码",this);
btn1->resize(70,30);
btn1->move(380,290);
//btn1->setStyleSheet("border-style:none;");
QPushButton *btn2 = new QPushButton("注册账号",this);
btn2->resize(70,30);
btn2->move(15,370);
//btn2->setStyleSheet("border-style:none;");
QPushButton *btn3 = new QPushButton(this);
btn3->resize(30,30);
btn3->setIcon(QIcon("D:/software/QT/Tools/QtCreator/bin/C++/day1/homework/icon/QRcode.png"));
btn3->move(500,370);
btn_login = new QPushButton("登录",this);
btn_login->resize(150,40);
btn_login->move(100,330);
btn_cancel = new QPushButton("取消",this);
btn_cancel->resize(150,40);
btn_cancel->move(300,330);
QCheckBox *cbox1 = new QCheckBox("自动登录", this);
cbox1->resize(90,30);
cbox1->move(100,290);
QCheckBox *cbox2 = new QCheckBox("记住密码", this);
cbox2->resize(90,30);
cbox2->move(240,290);
QObject::connect(btn_cancel, SIGNAL(clicked()),this , SLOT(slot_cancel()));
QObject::connect(btn_login, &QPushButton::clicked, this, &Widget::slot_login);
}
Widget::~Widget()
{
delete ui;
}
//处理登录按钮的槽函数
void Widget::slot_login()
{
//QString userName = ui->userNameEdit->text(); //获取账户
//QString pwd= ui->pwdEdit->text( );
QString userName = edit1->text(); //获取账户
QString pwd= edit2->text();
if(userName == "admin" && pwd == "123456")
{
qDebug() << "登录成功";
//发射跳转型号
emit Widget::jump();
//关闭当前界面
//this->close();
}
else
qDebug() << "登录失败";
}
//处理取消按钮的槽函数
void Widget::slot_cancel()
{
this->close();
}