思路:
1.登录需要判断账号与密码是否想等,相等才可跳转新页面,匹配失败输入框提示”账号密码不匹配”。
2.注册不需要判断,直接跳转新页面即可。
widget.cpp文件
#include "widget.h"
#include "ui_widget.h"
#include <QPushButton>
#include <QString>
#include <QDebug>
#include "login.h"
#include "zhuce.h"
Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget)
{ui->setupUi(this);this->setWindowTitle("千峰");
// connect(btn,QPushButton::clicked,this,QWidget::login);connect(ui->btn1,SIGNAL(clicked(bool)),this,SLOT(logins()));connect(ui->btn2,SIGNAL(clicked(bool)),this,SLOT(registers()));connect(ui->lab1,SIGNAL(textChanged(QString)),this,SLOT(mesg()));connect(ui->lab2,SIGNAL(textChanged(QString)),this,SLOT(mesg()));}Widget::~Widget()
{delete ui;
}void Widget::logins()
{QString user = ui->lab1->text();QString pass = ui->lab2->text();if(user == "admin" && pass == "027027"){qDebug() << "登录成功" << endl;login* log = new login;log->show();this->close();}else{ui->lab1->setText("账号密码不匹配");qDebug() << endl << "账号密码不匹配" << endl;}}void Widget::mesg(){qDebug() << "你好" << endl;
}void Widget::registers()
{zhuce* reg = new zhuce;reg->show();this->close();
}
widget.h文件
#ifndef WIDGET_H
#define WIDGET_H#include "login.h"#include <QWidget>namespace Ui {
class Widget;
}class Widget : public QWidget
{Q_OBJECTpublic:explicit Widget(QWidget *parent = 0);~Widget();private:Ui::Widget *ui;
public slots:void logins();void mesg();void registers();
};#endif // WIDGET_H
login.cpp文件
#include "login.h"
#include "ui_login.h"
#include <QLabel>
#include <QIcon>
login::login(QWidget *parent) :QWidget(parent),ui(new Ui::login)
{ui->setupUi(this);QLabel *label = new QLabel(this);
}login::~login()
{delete ui;
}
login.h文件
#ifndef LOGIN_H
#define LOGIN_H#include <QWidget>namespace Ui {
class login;
}class login : public QWidget
{Q_OBJECTpublic:explicit login(QWidget *parent = 0);~login();private:Ui::login *ui;
};#endif // LOGIN_H
zhuce.cpp
#include "zhuce.h"
#include "ui_zhuce.h"
#include <QLabel>
#include <QIcon>
zhuce::zhuce(QWidget *parent) :QWidget(parent),ui(new Ui::zhuce)
{ui->setupUi(this);QLabel *label = new QLabel(this);
}zhuce::~zhuce()
{delete ui;
}
zhuce.h文件
#ifndef ZHUCE_H
#define ZHUCE_H#include <QWidget>namespace Ui {
class zhuce;
}class zhuce : public QWidget
{Q_OBJECTpublic:explicit zhuce(QWidget *parent = 0);~zhuce();private:Ui::zhuce *ui;
};#endif // ZHUCE_H
注意:
1.main.cpp文件不用动
2.zhuce.ui文件和login.ui文件是注册与登录所对应要跳转的新页面