练习:
完善对话框,点击登录对话框,如果账号和密码匹配,则弹出信息对话框,给出提示”登录成功“,提供一个Ok按钮,用户点击Ok后,关闭登录界面,跳转到其他界面
如果账号和密码不匹配,弹出错误对话框,给出信息”账号和密码不匹配,是否重新登录“,并提供两个按钮Yes|No,用户点击Yes后,清除密码框中的内容,继续让用户进行登录,如果用户点击No按钮,则直接关闭登录界面
如果用户点击取消按钮,则弹出一个问题对话框,给出信息”您是否确定要退出登录?“,并给出两个按钮Yes|No,用户点击Yes后,关闭登录界面,用户点击No后,关闭对话框,继续执行登录功能
要求:(基于属性版、静态成员函数都使用)实现对话框的弹出
3> 使用定时器事件 实现闹钟
main.c:
#include "widget.h"
#include "Seon.h"
#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);Widget w;w.show();Seon s;QObject::connect(&w,&Widget::signal,&s,&Seon::my_slots);return a.exec();
}
seco.h:
#ifndef SEON_H
#define SEON_H#include <QWidget>
#include<QTimerEvent> //定时器事件类
#include<QTime> //时间类
#include <QTextToSpeech>
#include <QDebug>
namespace Ui {
class Seon;
}class Seon : public QWidget
{Q_OBJECTpublic:explicit Seon(QWidget *parent = nullptr);~Seon();void timerEvent(QTimerEvent *e);
signals:void signal_time();
public slots:void my_slots();void my_slots_time();
private slots:void on_pushButton_clicked();private:Ui::Seon *ui;QTextToSpeech speech;int sid;int haha;
};#endif // SEON_H
Widget.h:
#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();private slots:void on_pushButton_2_clicked();void on_pushButton_3_clicked();void on_pushButton_clicked();void on_pushButton_4_clicked();signals:void signal();
private:Ui::Widget *ui;
};
#endif // WIDGET_H
seco.c:
#include "seon.h"
#include "ui_seon.h"Seon::Seon(QWidget *parent) :QWidget(parent),ui(new Ui::Seon)
{ui->setupUi(this);connect(this,&Seon::signal_time,this,&Seon::my_slots_time);
}Seon::~Seon()
{delete ui;
}void Seon::timerEvent(QTimerEvent *e){if(e->timerId()==sid){QTime sys_time = QTime::currentTime();QString t = sys_time.toString("hh:mm:ss");ui->label_2->setText(t);ui->label_2->setAlignment(Qt::AlignCenter);qDebug() << t << ui->lineEdit->text();if(t==ui->lineEdit->text() && haha==1){emit signal_time();}}}
void Seon::my_slots()
{this->show();sid=startTimer(1000);
}void Seon::my_slots_time()
{QString textToSpeak = ui->label->text();qDebug() << textToSpeak;if (!textToSpeak.isEmpty()) {for (int a = 0; a < 5; ++a) {speech.say(textToSpeak);}} else {speech.say("No text to read");}
}void Seon::on_pushButton_clicked()
{if(ui->pushButton->text()=="启动"){haha=1;ui->pushButton->setText("关闭");}else{haha=0;ui->pushButton->setText("启动");}
}
widget.c:
#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);
}Widget::~Widget()
{delete ui;
}void Widget::on_pushButton_2_clicked()
{this->close();
}void Widget::on_pushButton_3_clicked()
{this->showMinimized();
}void Widget::on_pushButton_clicked()
{//判断密码是否正确if(ui->lineEdit->text()=="admin" && ui->lineEdit_2->text()=="123456"){//弹出对话框int ret = QMessageBox::information(this,"","登陆成功");//按下ok后操作if(ret==QMessageBox::Ok){this->close();emit Widget::signal();}}else{int ret = QMessageBox::information(this,"Error","密码或账户名错误,是否重新登录",QMessageBox::Yes | QMessageBox::No);if(ret==QMessageBox::Yes){ui->lineEdit_2->clear();}else{this->close();}}
}void Widget::on_pushButton_4_clicked()
{//弹出问题框int ret = QMessageBox::question(this,"","您确定要退出登录?",QMessageBox::Yes | QMessageBox::No);if(ret==QMessageBox::Yes){this->close();}else{QMessageBox::Close;}
}
思维导图: