作业要求:
主函数:
int main(int argc, char *argv[])
{QApplication a(argc, argv);Widget w;w.show();Form1 f;//连接窗口1的信号函数和窗口2打开的lambda函数Widget::connect(&w,&Widget::login,[&](){f.show();});return a.exec();
}
窗口1函数:
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_but1_clicked()
{if(ui->lineEdit->text()=="admin"&&ui->lineEdit_2->text()=="123456"){QMessageBox msg(QMessageBox::Information,"登录","登陆成功",QMessageBox::Ok);int res=msg.exec();if(res==QMessageBox::Ok){//发射登陆成功信号,在main连接到窗口2emit login();this->close();}}else{QMessageBox msg2(QMessageBox::Warning,"登录","账号和密码不匹配",QMessageBox::Yes|QMessageBox::No);int res2=msg2.exec();if(res2==QMessageBox::No){this->close();}else{this->ui->lineEdit->clear();this->ui->lineEdit_2->clear();}}
}void Widget::on_but2_clicked()
{QMessageBox msg(QMessageBox::Question,"取消","您是否确认要退出登录?",QMessageBox::Yes|QMessageBox::No);int res=msg.exec();if(res==QMessageBox::Yes){this->close();}
}
窗口1头文件
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QMessageBox>QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();
signals:void login();private slots:void on_but1_clicked();void on_but2_clicked();private:Ui::Widget *ui;};
#endif // WIDGET_H
窗口2函数
#include "form1.h"
#include "ui_form1.h"Form1::Form1(QWidget *parent) :QWidget(parent),ui(new Ui::Form1),speecher(new QTextToSpeech(this))
{ui->setupUi(this);this->setWindowFlag(Qt::FramelessWindowHint);this->setAttribute(Qt::WA_TranslucentBackground);
}Form1::~Form1()
{delete ui;
}void Form1::on_pushButton_4_clicked()
{this->close();
}void Form1::on_pushButton_clicked()
{speecher->say(ui->pushButton->text());
}void Form1::on_pushButton_2_clicked()
{speecher->say(ui->pushButton_2->text());
}void Form1::on_pushButton_3_clicked()
{speecher->say(ui->pushButton_3->text());
}
窗口2头文件
#ifndef FORM1_H
#define FORM1_H#include <QWidget>
#include <QTextToSpeech>
namespace Ui {
class Form1;
}class Form1 : public QWidget
{Q_OBJECTpublic:explicit Form1(QWidget *parent = nullptr);~Form1();private slots:void on_pushButton_4_clicked();void on_pushButton_clicked();void on_pushButton_2_clicked();void on_pushButton_3_clicked();private:Ui::Form1 *ui;QTextToSpeech *speecher;
};#endif // FORM1_H
现象
窗口1:
窗口2: