1.
main.cpp
cpp
#include "widget.h"
#include "form_2.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
Form_2 m;
QObject::connect(&w,&Widget::my_signal,&m,&Form_2::form2_slots);
w.show();
return a.exec();
}
widget.h
cpp
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QMessageBox>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_ton_2_clicked();
void on_ton_clicked();
signals:
void my_signal();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
form_2.h
cpp
#ifndef FORM_2_H
#define FORM_2_H
#include <QWidget>
namespace Ui {
class Form_2;
}
class Form_2 : public QWidget
{
Q_OBJECT
public:
explicit Form_2(QWidget *parent = nullptr);
~Form_2();
public slots:
void form2_slots();
private slots:
void on_label_linkActivated(const QString &link);
private:
Ui::Form_2 *ui;
};
#endif // FORM_2_H
form_2.cpp
cpp
#include "form_2.h"
#include "ui_form_2.h"
Form_2::Form_2(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form_2)
{
ui->setupUi(this);
}
Form_2::~Form_2()
{
delete ui;
}
void Form_2::form2_slots()
{
this->show();
}
void Form_2::on_label_linkActivated(const QString &link)
{
this->close();
}
widget.cpp
cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
this->setWindowFlag(Qt::FramelessWindowHint);
this->setAttribute(Qt::WA_TranslucentBackground);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_ton_2_clicked()
{
//弹出一个问题对话框
int ret=QMessageBox::information(this,
"问题信息",
"您是否确定要退出",
QMessageBox::Yes|QMessageBox::No
);
if(ret==QMessageBox::Yes)
{
this->close();
}
}
void Widget::on_ton_clicked()
{
if(ui->nameedit->text()=="admin" && ui->passwdedit->text()=="123456")
{
//弹出一个信息对话框
//使用静态成员函数实现
int ret = QMessageBox::information(this, //父组件
"登录信息", //标题
"登录成功", //文本
QMessageBox::Ok
);
//根据函数的返回值,判断选中的按钮
if(ret == QMessageBox::Ok)
{
this->close();
emit my_signal();
}
}
else
{
//弹出错误对话框
//1.使用QMessageBox 实例化一个对象 有参构造
QMessageBox msg(QMessageBox::Critical, //图标
"登录错误", //标题
"账号和密码不匹配,是否重新登录", //文本
QMessageBox::Yes | QMessageBox::No, //按钮
this //指定父组件
);
//2.调用exec()弹出对话框
int ret = msg.exec();
//3.根据用户的选择,执行不同的功能
if(ret == QMessageBox::Yes)
{
ui->nameedit->clear();
ui->passwdedit->clear();
}
else
{
this->close();
}
}
}