widget.h
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include<QFontDialog> //字体对话框类
#include<QFont> //字体类
#include<QMessageBox> //消息对话框类
#include<QColorDialog> //颜色对话框类
#include<QColor> //颜色类
#include<QFileDialog> //文件对话框类
#include<QDebug>
#include<QFile>QT_BEGIN_NAMESPACE
namespace Ui {
class Widget;
}
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();signals:void jump_signal();
private slots:void on_Login_clicked();void on_Cancel_clicked();private:Ui::Widget *ui;
};
#endif // WIDGET_H
success.h
#ifndef SUCCESS_H
#define SUCCESS_H#include <QWidget>namespace Ui {
class Success;
}class Success : public QWidget
{Q_OBJECTpublic:explicit Success(QWidget *parent = nullptr);~Success();
public slots:void success_slot();
private:Ui::Success *ui;
};#endif // SUCCESS_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);
}Widget::~Widget()
{delete ui;
}void Widget::on_Login_clicked()
{QString user=ui->User->text();QString passwd=ui->Passwd->text();if(user == "admin" && passwd == "123456"){QMessageBox msg_ls(QMessageBox::Information," ","登陆成功!",QMessageBox::Yes,this);msg_ls.exec();this->close();emit jump_signal();}else{QMessageBox msg_lf(QMessageBox::Information," ","账号和密码不匹配,是否重新登录",QMessageBox::Yes|QMessageBox::No,this);int ret = msg_lf.exec();if(ret == QMessageBox::Yes){ui->User->clear();ui->Passwd->clear();}else{this->close();}}
}void Widget::on_Cancel_clicked()
{int ret = QMessageBox::question(this, " ","您是否确定要退出登录?", QMessageBox::Yes | QMessageBox::No);if(ret == QMessageBox::Yes){this->close();}
}
success.cpp
#include "success.h"
#include "ui_success.h"Success::Success(QWidget *parent): QWidget(parent), ui(new Ui::Success)
{ui->setupUi(this);
}Success::~Success()
{delete ui;
}void Success::success_slot()
{this->show();
}
main.cpp
#include "widget.h"
#include "success.h"
#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);Widget w;w.show();Success s;QObject::connect(&w,&Widget::jump_signal,&s,&Success::success_slot);return a.exec();
}
结果: