QT的信号和槽页面的应用
完善对话框,点击登录弹出对话框,如果账号和密码匹配,则弹出信息对话框,给出提示”登录成功“,提供一个Ok按钮,用户点击Ok后,关闭登录界面,跳转到其他界面
如果账号和密码不匹配,弹出错误对话框,给出信息”账号和密码不匹配,是否重新登录“,并提供两个按钮Yes|No,用户点击Yes后,清除密码框中的内容,继续让用户进行登录,如果用户点击No按钮,则直接关闭登录界面
如果用户点击取消按钮,则弹出一个问题对话框,给出信息”您是否确定要退出登录?“,并给出两个按钮Yes|No,用户迪纳基Yes后,关闭登录界面,用户点击No后,关闭对话框,继续执行登录功能
要求:基于属性版实现、基于静态成员函数实现 两个种方式都要有
#include "widget.h"
#include "ui_widget.h"
#include "QDebug"
Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);this->setWindowFlag(Qt::FramelessWindowHint);this->setAttribute(Qt::WA_TranslucentBackground);QObject::connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(btn_closed()));QObject::connect(ui->pushButton_3,SIGNAL(clicked()),this,SLOT(btn_hide()));
}Widget::~Widget()
{delete ui;
}void Widget::btn_closed()
{//基于函数成员的版本int ret=QMessageBox::information(this,"提示","您是否确定要退出登录?",QMessageBox::Yes | QMessageBox::No,QMessageBox::No);//根据用户的选择,执行不同的功能if(ret==QMessageBox::Yes){this->close();}
}
void Widget::btn_hide()
{this->showMinimized();
}
void Widget::on_pushButton_clicked()
{if(ui->lineEdit->text() == "admin" && ui->lineEdit_2->text() == "12345"){ui->label_3->setStyleSheet(QString("background-color:blue;color:white"));ui->label_3->setText("登陆成功");//基于属性版本QMessageBox msg(QMessageBox::Information,"提示","登录成功",QMessageBox::Ok,this);//调用exrc()弹出对话框int ret=msg.exec();//根据用户的选择 执行不同的功能if(ret==QMessageBox::Ok){qDebug() << "登录成功" ;}else{qDebug() << "登录失败";}// 使用 QTimer 延时关闭QTimer::singleShot(600, this, [this](){emit log_sig();ui->label_3->setStyleSheet(QString("background:transparent;"));this->close();});}else{ui->label_3->setText("登陆失败");ui->label_3->setStyleSheet(QString("background-color:red;color:black"));//基于静态成员函数版本int ret=QMessageBox::critical(this,"错误","账号密码不正确,是否重新登录",QMessageBox::Yes | QMessageBox::No,QMessageBox::No);//根据用户的选择,执行不同的功能if(ret==QMessageBox::Yes){ui->lineEdit_2->clear();}else{this->close();}// 使用 QTimer 延时清除文本QTimer::singleShot(600, this, [this](){ui->label_3->setStyleSheet(QString("background:transparent;"));ui->label_3->setText("");ui->lineEdit_2->setText("");});}
}
ui部分:
效果图: