创建一个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:

  • 15
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值