今日作业:
1. 完善对话框,点击登录对话框,如果账号和密码匹配,则弹出信息对话框,给出提示”登录成功“,提供一个Ok按钮,用户点击Ok后,关闭登录界面,跳转到其他界面
如果账号和密码不匹配,弹出错误对话框,给出信息”账号和密码不匹配,是否重新登录“,并提供两个按钮Yes|No,用户点击Yes后,清除密码框中的内容,继续让用户进行登录,如果用户点击No按钮,则直接关闭登录界面
如果用户点击取消按钮,则弹出一个问题对话框,给出信息”您是否确定要退出登录?“,并给出两个按钮Yes|No,用户点击Yes后,关闭登录界面,用户点击No后,关闭对话框,继续执行登录功能
要求:(基于属性版、静态成员函数都使用)实现对话框的弹出
//day3_04.h
#ifndef DAY3_04_H
#define DAY3_04_H#include <QWidget>
#include <QMessageBox>
#include <QDebug>QT_BEGIN_NAMESPACE
namespace Ui { class day3_04; }
QT_END_NAMESPACEclass day3_04 : public QWidget
{Q_OBJECTpublic:day3_04(QWidget *parent = nullptr);~day3_04();signals:void my_jump(); //第一个界面的信号private slots:void on_pushButton_2_clicked();void on_pushButton_clicked();void on_pushButton_6_clicked();void on_pushButton_3_clicked();private:Ui::day3_04 *ui;
};
#endif // DAY3_04_H
//day3_04.cpp
#include "day3_04.h"
#include "ui_day3_04.h"day3_04::day3_04(QWidget *parent): QWidget(parent), ui(new Ui::day3_04)
{ui->setupUi(this);//去掉头部this->setWindowFlag(Qt::FramelessWindowHint);//去掉空白部分this->setAttribute(Qt::WA_TranslucentBackground);
}day3_04::~day3_04()
{delete ui;
}void day3_04::on_pushButton_2_clicked()
{this->close();
}void day3_04::on_pushButton_clicked()
{//获取输入的文本QString user1 = ui->user->text();QString line1 = ui->line->text();if(user1.isEmpty() || line1.isEmpty()){QMessageBox::warning(this, "Warning", "请输入账号或密码");}else{//判断账号和密码正确性if(user1 == "admin" && line1 == "123456"){QMessageBox::information(this, "Access Granted", "登陆成功");//关闭窗口this->close();//触发信号emit my_jump();}else{int ret = QMessageBox::warning(this, "Access Denied","账号和密码不匹配,是否重新登录",QMessageBox::Yes | QMessageBox::No);//根据函数的返回值,判断选中的按钮if(ret == QMessageBox::Yes){ui->user->clear();ui->line->clear();}else{//关闭窗口this->close();}ui->user->clear();ui->line->clear();}}
}void day3_04::on_pushButton_6_clicked()
{this->showMinimized();
}void day3_04::on_pushButton_3_clicked()
{//弹出警告对话框//使用QMessageBox 实例化一个对象 有参构造QMessageBox msg(QMessageBox::Warning, //图标"警告", //标题"您是否确定要退出登录?", //文本QMessageBox::Yes | QMessageBox::No, //按钮this //指定父组件);//2.调用exec()弹出对话框int ret = msg.exec();//3.根据用户的选择,执行不同的功能if(ret == QMessageBox::Yes){//关闭窗口this->close();}else{QMessageBox::close;}
}
//main.cpp
#include "day3_04.h"
#include "second.h" //包含第二个头文件#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);//实例化第一个界面day3_04 w;w.show();//实例化第二个界面Second s;//连接QObject::connect(&w, &day3_04::my_jump, &s, &Second::jump_slot);return a.exec();
}
2. 使用定时器事件 实现闹钟
//day4_04_zuoye.h
#ifndef DAY4_04_ZUOYE_H
#define DAY4_04_ZUOYE_H#include <QWidget>
#include <QDebug>
#include <QTime>
#include <QTextToSpeech>
QT_BEGIN_NAMESPACE
namespace Ui { class day4_04_zuoye; }
QT_END_NAMESPACEclass day4_04_zuoye : public QWidget
{Q_OBJECTpublic:day4_04_zuoye(QWidget *parent = nullptr);~day4_04_zuoye();void timerEvent(QTimerEvent *e);
signals:void signal_time();private slots:void on_pushButton_clicked();void my_slots_time();private:Ui::day4_04_zuoye *ui;int hh;int sid;//定义一个语音播报者QTextToSpeech speech;};
#endif // DAY4_04_ZUOYE_H
//day4_04_zuoye.cpp
#include "day4_04_zuoye.h"
#include "ui_day4_04_zuoye.h"day4_04_zuoye::day4_04_zuoye(QWidget *parent): QWidget(parent), ui(new Ui::day4_04_zuoye)
{ui->setupUi(this);sid = startTimer(1000);connect(this,&day4_04_zuoye::signal_time,this,&day4_04_zuoye::my_slots_time);
}day4_04_zuoye::~day4_04_zuoye()
{delete ui;
}void day4_04_zuoye::on_pushButton_clicked()
{if(ui->pushButton->text() == "启动"){hh = 1;ui->pushButton->setText("关闭");}else{hh = 0;ui->pushButton->setText("启动");}
}void day4_04_zuoye::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() && hh == 1){emit signal_time();}}
}void day4_04_zuoye::my_slots_time()
{QString textToSpeak = ui->label_4->text();qDebug() << textToSpeak;if(!textToSpeak.isEmpty()){for(int a=0; a<5; ++a){speech.say(textToSpeak);}}
}
//main.cpp
#include "day4_04_zuoye.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);day4_04_zuoye w;w.show();return a.exec();
}