登录窗口头文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QToolBar>
#include <QMenuBar>
#include <QPushButton>
#include <QStatusBar>
#include <QLabel>
#include <QDockWidget>//浮动窗口类
#include <QTextEdit>
#include <QIcon>
#include <QMovie>
#include <QString>
#include <cstring>
#include <QDebug>
#include <QMessageBox>QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();
signals:void jump();public slots:void login_slot();private slots:void on_cancel_btn_clicked();private:Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
登录窗口主文件
#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);//窗口名this->setWindowTitle("QQ");this->setWindowIcon(QIcon(":/01login/icon/kun.webp"));this->setFixedSize(480,400);QMovie *kk=new QMovie(":/01login/icon/dance.gif");ui->logo_label->setMovie(kk);kk->start();ui->logo_label->setScaledContents(true);//账号密码图标ui->host_label->setPixmap(QPixmap(":/01login/icon/zhanghu.png"));ui->host_label->setScaledContents(true);ui->passwd_label->setPixmap(QPixmap(":/01login/icon/passwd.jpg"));ui->passwd_label->setScaledContents(true);//账号密码文本ui->host_edit->setPlaceholderText("账号");ui->passwd_edit->setPlaceholderText("密码");//连接登录跳转信号和槽函数connect(ui->login_btn,&QPushButton::clicked,this,&MainWindow::login_slot);
}
void MainWindow::login_slot()
{QString usrname = ui->host_edit->text();QString pwd = ui->passwd_edit->text();if(usrname=="admin"&&pwd=="123456")//若账号密码正确{qDebug()<< "login success";//消息对话框QMessageBox msg(QMessageBox::Information,"通知","登陆成功",QMessageBox::Ok,this);int ret = msg.exec();if(ret==QMessageBox::Ok){this -> close();}//跳转到新页面emit jump();}else//若密码错误{qDebug()<< "login defeat";//基于属性弹出消息对话框QMessageBox msg(QMessageBox::Critical,"登录失败","账号和密码不匹配,是否重新登录",QMessageBox::Yes|QMessageBox::No,this);int ret = msg.exec();if(ret==QMessageBox::Yes)//若yes则清空密码{ui -> passwd_edit->clear();}else//若no则直接关闭登录界面{this->close();}}
}
MainWindow::~MainWindow()
{delete ui;}void MainWindow::on_cancel_btn_clicked()//取消
{//基于静态成员函数消息对话框的弹出int ret = QMessageBox::question(this,"再次确认","您是否确定要退出登录",QMessageBox::Yes|QMessageBox::No);if(ret==QMessageBox::Yes)//若yes则关闭登录界面{this->close();}else//若no则关闭对话框继续执行登录界面{}
}
跳转页面头文件
#ifndef SECOND_H
#define SECOND_H#include <QWidget>namespace Ui {
class second;
}class second : public QWidget
{Q_OBJECTpublic:explicit second(QWidget *parent = nullptr);~second();public slots:void jumpslot();void sureslot();private:Ui::second *ui;
};#endif // SECOND_H
跳转页面主文件
#include "second.h"
#include "ui_second.h"second::second(QWidget *parent) :QWidget(parent),ui(new Ui::second)
{ui->setupUi(this);connect(ui->sure_btn,&QPushButton::clicked,this,&second::sureslot);
}second::~second()
{delete ui;
}void second::jumpslot()
{this->show();
}
void second::sureslot()
{this->close();
}
主文件
#include "mainwindow.h"
#include "second.h"
#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);MainWindow w;w.show();second s;QObject::connect(&w,&MainWindow::jump,&s,&second::jumpslot);return a.exec();
}