第一个界面头文件:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPushButton>
#include <QDebug>
#include <QString>
#include <QMessageBox>
#include <QLineEdit>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
public slots: //表示该函数下的都是槽函数
void my_slot(); //自定义一个槽函数,完整函数,既要声明,也要定义
void my_slot1(); //登录按钮
signals:
void my_signals();
private slots:
void on_pushButton_clicked();
void on_login_clicked();
void on_cancel_clicked();
private:
Ui::Widget *ui;
QPushButton *cancel;
QPushButton *login;
};
#endif // WIDGET_H
第一个界面源文件:
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
, cancel(new QPushButton("取消", this)) //实例化一个按钮
, login(new QPushButton("登录", this))
{
ui->setupUi(this);
this->resize(760, 1030);
ui->title_label->setPixmap(QPixmap(":/pictrue/wyy.png"));
ui->title_label->setScaledContents(true); //使图片自适应标签
ui->user_label->setPixmap(QPixmap(":/pictrue/userName.jpg"));
//图片自动适应
ui->user_label->setScaledContents(1);
//设置标签图片
ui->passwd_label->setPixmap(QPixmap(":/pictrue/passwd.jpg"));
//图片自动适应
ui->passwd_label->setScaledContents(1);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_login_clicked()
{
QString user,passwd;
user = ui->user_Edit->text();
passwd = ui->passwd_Edit->text();
if(user == "admin" && passwd == "12345678")
{
//弹出一个消息对话框QMessageBox 基于属性版本
QMessageBox msg(QMessageBox::Information,
"登录",
"登录成功",
QMessageBox::Ok,
this
);
int ret = msg.exec();
if(ret == QMessageBox::Ok)
{
emit my_signals();
this->close();
}
}
else
{
//弹出一个警告对话框QMessageBox 基于属性版本
QMessageBox msg(QMessageBox::Warning,
"登录失败",
"账号和密码不匹配,是否重新登录",
QMessageBox::Yes | QMessageBox::No,
this
);
int ret = msg.exec();
if(ret == QMessageBox::Yes)
{
ui->user_Edit->clear();
ui->passwd_Edit->clear();
}
else
{
this->close();
}
}
}
void Widget::on_cancel_clicked()
{
//弹出一个问题对话框QMessageBox 基于属性版本
QMessageBox msg(QMessageBox::Question,
"确认",
"您是否确定要退出登录?",
QMessageBox::Yes | QMessageBox::No,
this
);
int ret = msg.exec();
if(ret == QMessageBox::Yes)
{
this->close();
}
else
{
msg.close();
}
}
第二个界面头文件:
#ifndef SECOND_H
#define SECOND_H
#include <QWidget>
namespace Ui {
class Second;
}
class Second : public QWidget
{
Q_OBJECT
public:
explicit Second(QWidget *parent = nullptr);
~Second();
private:
Ui::Second *ui;
public slots:
void my_slots();
};
#endif // SECOND_H
第二个界面的源文件:
#include "second.h"
#include "ui_second.h"
Second::Second(QWidget *parent) :
QWidget(parent),
ui(new Ui::Second)
{
ui->setupUi(this);
//去掉头部
this->setWindowFlag(Qt::FramelessWindowHint);
//去掉窗口的空白部分
this->setAttribute(Qt::WA_TranslucentBackground);
}
Second::~Second()
{
delete ui;
}
void Second::my_slots()
{
this->show();
}
主函数:
#include "widget.h"
#include "second.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
Second s;
QObject::connect(&w,&Widget::my_signals,&s,&Second::my_slots);
w.show();
return a.exec();
}