创建一个qt登录界面,密码账号正确转到窗口2,否则弹出对话框提示账号密码错误,窗口2有四个按键,三个按键可以朗读按键文本,第四个退出。

作业要求:

主函数:

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连接到窗口2
            emit 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_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    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_OBJECT

public:
    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:

最近更新

  1. TCP协议是安全的吗?

    2024-04-10 09:20:05       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-10 09:20:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-10 09:20:05       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-10 09:20:05       20 阅读

热门阅读

  1. easyui 使用记录

    2024-04-10 09:20:05       13 阅读
  2. 第四十七章 为 Web 应用程序实现 HTTP 身份验证

    2024-04-10 09:20:05       12 阅读
  3. hbase的基础搭建

    2024-04-10 09:20:05       12 阅读
  4. mysql create procedure

    2024-04-10 09:20:05       12 阅读
  5. HBase详解(3)

    2024-04-10 09:20:05       9 阅读
  6. 封装Element-Plus表单组件

    2024-04-10 09:20:05       15 阅读
  7. textcnn做多分类

    2024-04-10 09:20:05       10 阅读
  8. Android ViewStub

    2024-04-10 09:20:05       10 阅读
  9. 自动驾驶中的传感器融合算法

    2024-04-10 09:20:05       50 阅读