mainwindow.h
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();signals:void jump(); //自定义跳转信号函数private slots:void on_pushButton_clicked();void on_pushButton_2_clicked();private:Ui::MainWindow *ui;Second *s1; //定义另一个界面的指针
};
#endif // MAINWINDOW_H
second.h
#ifndef SECOND_H
#define SECOND_H#include <QDialog>namespace Ui {
class Second;
}class Second : public QDialog
{Q_OBJECTpublic:explicit Second(QWidget *parent = nullptr);~Second();public slots:void jump_slot(); //接收跳转信号的槽函数private:Ui::Second *ui;
};#endif // SECOND_H
mianwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);ui->label->setPixmap(QPixmap("D:\\QTtupian\\jimoaqing.gif"));ui->label->setScaledContents(true);ui->label_2->setPixmap(QPixmap("D:\\QTtupian\\zhanghao.jpg"));ui->label_2->setScaledContents(true);ui->label_3->setPixmap(QPixmap("D:\\QTtupian\\mima.jpg"));ui->label_3->setScaledContents(true);s1 = new Second; //给另一个界面实例化空间//将当前界面的信号,与s1界面的槽函数进行连接connect(this,&MainWindow::jump, s1, &Second::jump_slot);
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::on_pushButton_clicked()
{if(ui->lineEdit->text()=="admin"&&ui->lineEdit_2->text()=="123456"){//信息对话框QMessageBox box(QMessageBox::Question,"信息对话框","登录成功",QMessageBox::Ok,this);box.setDefaultButton(QMessageBox::Ok); //默认到ok上int ret = box.exec(); //运行对话框if(ret == QMessageBox::Ok){emit jump(); //发送跳转信息this->hide(); //将当前界面隐藏}}else{//错误对话框QMessageBox box(QMessageBox::Question,"错误对话框","账号密码不匹配,是否重新登录",QMessageBox::Ok|QMessageBox::Cancel,this);box.setDefaultButton(QMessageBox::Ok); //默认到ok上int ret = box.exec(); //运行对话框switch(ret){case QMessageBox::Ok:ui->lineEdit->clear(); //清楚账号密码内容ui->lineEdit_2->clear();break;case QMessageBox::Cancel://ret=box.close();break;}}}void MainWindow::on_pushButton_2_clicked()
{//问题对话框int ret = QMessageBox::question(this,"问题对话框","是否确定要退出登录",QMessageBox::Yes|QMessageBox::No,QMessageBox::No);switch(ret){case QMessageBox::Yes:this->close(); //关闭窗口break;case QMessageBox::No:break;}
}
second.cpp
#include "second.h"
#include "ui_second.h"Second::Second(QWidget *parent) :QDialog(parent),ui(new Ui::Second)
{ui->setupUi(this);
}Second::~Second()
{delete ui;
}//接收跳转信号对应的槽函数
void Second::jump_slot()
{this->show(); //将自己界面进行展示
}
main.cpp
#include "mainwindow.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);MainWindow w;w.show();return a.exec();
}