作业:
第一个界面头文件:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QString>
#include <QDebug>
#include <QMessageBox>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
signals:
void jump_signal();
private slots:
void on_btn2_clicked();
void my_slot();
void bt1_slot();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
第一个界面源文件:
#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);
//连接按钮2的信号和槽函数 基于qt4版本
connect(ui->btn2,SIGNAL(clicked()),this,SLOT(my_slot()));
//连接信号和自定义槽函数,基于qt5版本的连接
connect(ui->btn1,&QPushButton::clicked,this,&Widget::bt1_slot);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_btn2_clicked()
{
}
void Widget::my_slot()
{
QMessageBox msg(QMessageBox::Warning,"警告","确定要取消登录吗?",QMessageBox::Yes|QMessageBox::No);
int ret = msg.exec();
if(ret == QMessageBox::Yes)
{
this->close();
}
else
{
}
}
void Widget::bt1_slot()
{
QString str1 = "admin";
QString str2 = ui->usenameEdit->text();
QString passwd = "123456";
QString passwd2 = ui->passwdEdit->text();
if(str1 == str2 && passwd == passwd2)
{
QMessageBox::information(this,"","登录成功!");
this->close();
emit jump_signal();
}
else
{
int ret = QMessageBox::question(this,"","用户名或密码错误",QMessageBox::Yes|QMessageBox::No);
if(ret == QMessageBox::Yes)
{
ui->passwdEdit->clear();
}
else
{
this->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();
public slots:
void jump_slot();
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);
}
Second::~Second()
{
delete ui;
}
void Second::jump_slot()
{
this->show();
}
主函数:
#include "widget.h"
#include "second.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
Second s;
QObject::connect(&w, &Widget::jump_signal, &s, &Second::jump_slot);
return a.exec();
}
思维导图