实现登录框中,当登录成功时,关闭登录界面,并跳转到其他界面。
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QIcon>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
this->setWindowIcon(QIcon("D:/QTProj/C++/icon/QQ.png"));
setWindowTitle("Login");
setFixedSize(500,400);
QLabel *labLogo = new QLabel(this);
labLogo->resize(500,150);
labLogo->setPixmap(QPixmap("D:/QTProj/C++/icon/logo.jpg"));
labLogo->setScaledContents(true);
QLabel *labAccount = new QLabel(this);
labAccount->resize(50,50);
labAccount->setPixmap(QPixmap("D:/QTProj/C++/icon/denglu.png"));
labAccount->setScaledContents(true);
labAccount->move(100,200);
QLabel *labPassword = new QLabel(this);
labPassword->resize(50,50);
labPassword->setPixmap(QPixmap("D:/QTProj/C++/icon/denglumima.png"));
labPassword->setScaledContents(true);
labPassword->move(100,280);
QLineEdit *editAccount = new QLineEdit(this);
editAccount->resize(200,50);
editAccount->move(180,200);
editAccount->setPlaceholderText("账号");
QLineEdit *editPassword = new QLineEdit(this);
editPassword->resize(200,50);
editPassword->move(180,280);
editPassword->setPlaceholderText("密码");
editPassword->setEchoMode(QLineEdit::Password);
QPushButton* btnLogin = new QPushButton("登录",this);
btnLogin->setIcon(QIcon("D:/QTProj/C++/icon/denglu_1.png"));
btnLogin->resize(100,50);
btnLogin->move(250,340);
QPushButton* btnCancel = new QPushButton("取消",this);
btnCancel->setIcon(QIcon("D:/QTProj/C++/icon/quxiao.png"));
btnCancel->resize(100,50);
btnCancel->move(380,340);
connect(btnLogin,&QPushButton::clicked,[=](){
if(editAccount->text()=="admin"&&editPassword->text()=="123456"){
emit toMainWindow();
close();
}else {
qDebug()<<"登录失败";
}
});
}
Widget::~Widget()
{
delete ui;
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
namespace Ui {
class MainWindow;
}
class MainWindow : public QWidget
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
public slots:
void mainWindow_slot();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QWidget(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::mainWindow_slot()
{
show();
}
Main.cpp
#include "widget.h"
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
MainWindow mw;
QObject::connect(&w,&Widget::toMainWindow,&mw,&MainWindow::mainWindow_slot);
return a.exec();
}