作业要求:
1.思维导图
2.完善对话框,点击登录对话框,如果账号和密码匹配,则弹出信息对话框,给出提示”登录成功“,提供一个Ok按钮,用户点击Ok后,关闭登录界面,跳转到其他界面
如果账号和密码不匹配,弹出错误对话框,给出信息”账号和密码不匹配,是否重新登录“,并提供两个按钮Yes|No,用户点击Yes后,清除密码框中的内容,继续让用户进行登录,如果用户点击No按钮,则直接关闭登录界面
如果用户点击取消按钮,则弹出一个问题对话框,给出信息”您是否确定要退出登录?“,并给出两个按钮Yes|No,用户点击Yes后,关闭登录界面,用户点击No后,关闭对话框,继续执行登录功能
要求:(基于属性版、静态成员函数都使用)实现对话框的弹出
3.完成闹钟界面
作业1:没做完
作业2:
运行代码:
widget.cpp
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);
}Widget::~Widget()
{delete ui;
}//登录按钮槽函数
void Widget::on_btn_login_clicked()
{//登录成功if(("123456"==ui->lineEdit1->text()) && ("123456"==ui->lineEdit2->text())){QMessageBox msg(QMessageBox::Information,"登录","登陆成功",QMessageBox::Ok,this);//实例化对象msg.exec();//弹出消息对话框emit my_jump();//触发信号jumpthis->close();//关闭登录界面//登录失败}else{QMessageBox msg(QMessageBox::Warning,"登录","账号或密码错误,是否重新登陆",QMessageBox::Yes|QMessageBox::No,this);//实例化对象int ret = msg.exec();//弹出消息对话框if(ret == QMessageBox::Yes){//选择yes//清空数据ui->lineEdit1->clear();ui->lineEdit2->clear();}else if(ret == QMessageBox::No){//选择No//关闭登录界面this->close();}}
}
//取消按钮槽函数
void Widget::on_btn_login_2_clicked()
{int ret = QMessageBox::question(this,"question","是否退出登录",QMessageBox::Yes|QMessageBox::No);if(ret == QMessageBox::Yes){this->close();}
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QLineEdit>
#include <QMessageBox>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();
signals:void my_jump();
private slots:void on_btn_login_clicked();void on_btn_login_2_clicked();private:Ui::Widget *ui;
};
#endif // WIDGET_H
second.cpp
#include "second.h"
#include "ui_second.h"Second::Second(QWidget *parent) :QWidget(parent),ui(new Ui::Second)
{ui->setupUi(this);
}Second::~Second()
{delete ui;
}
void Second::jump_slot(){this->show();
}
second.h
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QLineEdit>
#include <QMessageBox>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();
signals:void my_jump();
private slots:void on_btn_login_clicked();void on_btn_login_2_clicked();private:Ui::Widget *ui;
};
#endif // WIDGET_H
main.cpp
#include "widget.h"
#include "second.h"
#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);Widget w;Second s;w.show();QObject::connect(&w,&Widget::my_jump,&s,&Second::jump_slot);return a.exec();
}
运行界面:
作业3:
运行代码:
widget.cpp
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget),num(0)
{ui->setupUi(this);id = startTimer(1000);}Widget::~Widget()
{delete ui;
}
void Widget::timerEvent(QTimerEvent *e){//系统时间设置if(e->timerId()==id){QTime sys_time = QTime::currentTime();time = sys_time.toString("hh:mm:ss");ui->lab_sys_time->setText(time);}//检测闹钟是否到if(ui->start_btn->text()=="关闭"){if(time == alarm){num=1;}}//开始循环if(num>0){ui->lab_context->setText(ui->lab_context->text()+"起床了\n");num++;}if(num>5){num=0;}
}void Widget::on_start_btn_clicked()
{if(ui->start_btn->text()=="启动"){ui->start_btn->setText("关闭");alarm = ui->lineEdit_alarm->text();}else if(ui->start_btn->text()=="关闭"){ui->start_btn->setText("启动");}
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QLineEdit>
#include <QLabel>
#include <QTimerEvent>
#include <QTime>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();private slots:void on_start_btn_clicked();void timerEvent(QTimerEvent *e);private:Ui::Widget *ui;int id;QString alarm;QString time;//系统时间int num;//循环次数
};
#endif // WIDGET_H