完善对话框,点击登录对话框,如果账号和密码匹配,则弹出信息对话框,给出提示”登录成功“,提供一个Ok按钮,用户点击Ok后,关闭登录界面,跳转到其他界面
如果账号和密码不匹配,弹出错误对话框,给出信息”账号和密码不匹配,是否重新登录“,并提供两个按钮Yes|No,用户点击Yes后,清除密码框中的内容,继续让用户进行登录,如果用户点击No按钮,则直接关闭登录界面(账号123 密码123)
如果用户点击取消按钮,则弹出一个问题对话框,给出信息”您是否确定要退出登录?“,并给出两个按钮Yes|No,用户点击Yes后,关闭登录界面,用户点击No后,关闭对话框,继续执行登录功能
要求:(基于属性版、静态成员函数都使用)实现对话框的弹出
3> 使用定时器事件 实现闹钟
页面1.h
#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(); private: Ui::Widget *ui; signals: void login_signals(); public slots: void logout_slots(); private slots: void on_loginBtn_clicked(); void on_cancelBtn_clicked(); }; #endif // WIDGET_H
页面2.h
#ifndef CONTENT_H #define CONTENT_H #include <QWidget> #include <QTimerEvent> #include <QTime> #include <QtDebug> #include <QTextToSpeech> namespace Ui { class Content; } class Content : public QWidget { Q_OBJECT public: explicit Content(QWidget *parent = nullptr); ~Content(); void timerEvent(QTimerEvent *event); private: Ui::Content *ui; int time_id; QString alarmStr; QTextToSpeech *speecher; bool isSaying; void startSay(); signals: void logout_signals(); public slots: void login_slots(); void voice_stop_slots(QTextToSpeech::State state); private slots: void on_pushButton_clicked(); void on_pushButton_2_clicked(); }; #endif // CONTENT_H
mian.cpp
#include "widget.h" /* * 完善对话框,点击登录对话框,如果账号和密码匹配,则弹出信息对话框,给出提示”登录成功“, * 提供一个Ok按钮,用户点击Ok后,关闭登录界面,跳转到其他界面 如果账号和密码不匹配,弹出错误对话框,给出信息”账号和密码不匹配,是否重新登录“, 并提供两个按钮Yes|No,用户点击Yes后,清除密码框中的内容,继续让用户进行登录, 如果用户点击No按钮,则直接关闭登录界面 如果用户点击取消按钮,则弹出一个问题对话框,给出信息”您是否确定要退出登录? “,并给出两个按钮Yes|No,用户点击Yes后,关闭登录界面,用户点击No后,关闭对话框,继续执行登录功能 要求:(基于属性版、静态成员函数都使用)实现对话框的弹出*/ #include <QApplication> #include "content.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); Widget w; w.show(); Content con; QObject::connect(&w, &Widget::login_signals, &con, &Content::login_slots); QObject::connect(&con, &Content::logout_signals, &w, &Widget::logout_slots); return a.exec(); }
页面1.cpp
#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::logout_slots() { this->show(); } void Widget::on_loginBtn_clicked() { if (ui->userTE->text() == "123" && ui->pswTE->text() == "123"){ QMessageBox msg(QMessageBox::Information, "提示信息", "账号密码正确,登录成功!", QMessageBox::Ok, this); int ret = msg.exec(); switch (ret) { case QMessageBox::Ok: { this->close(); emit login_signals();} break; } }else{ int ret = QMessageBox::warning(this,"提示信息","账号和密码不匹配,是否重新登录",QMessageBox::Yes |QMessageBox::No); if (ret == QMessageBox::Yes) { ui->pswTE->clear(); ui->pswTE->setFocus(); }else if(ret == QMessageBox::No) { this->close(); } } } void Widget::on_cancelBtn_clicked() { int ret = QMessageBox::warning(this,"提示信息","是否确定要退出登录?",QMessageBox::Yes |QMessageBox::No); if (ret == QMessageBox::Yes) { this->close(); } }
页面2.cpp
#include "content.h" #include "ui_content.h" Content::Content(QWidget *parent) : QWidget(parent), ui(new Ui::Content) { ui->setupUi(this); this->setWindowFlag(Qt::FramelessWindowHint); this->setAttribute(Qt::WA_TranslucentBackground); time_id = startTimer(1000); speecher = new QTextToSpeech; isSaying = false; // 这里监测 语音播报状态 connect(speecher, &QTextToSpeech::stateChanged, this, &Content::voice_stop_slots); } Content::~Content() { killTimer(time_id); delete ui; } void Content::timerEvent(QTimerEvent *event) { if(event->timerId() == time_id){ QTime times = QTime::currentTime(); ui->label_2->setText(times.toString("hh:mm:ss")); if(times.toString("hh:mm") == alarmStr && !isSaying){ startSay(); } } } void Content::startSay(){ for (int i =0; i < 3; i++) { speecher->say(ui->label_4->text()); } } void Content::voice_stop_slots(QTextToSpeech::State state) { if(state == QTextToSpeech::Speaking){ isSaying = true; }else{ // 播放完成一定要重置当前闹钟时间 alarmStr = ""; isSaying = false; } } void Content::login_slots() { this->show(); } void Content::on_pushButton_clicked() { this->close(); emit logout_signals(); } void Content::on_pushButton_2_clicked() { alarmStr = ui->timeEdit->text(); }