思维导图:
第一个界面头文件:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <qmessagebox.h>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
signals:
void my_jump();
private slots:
void on_pushButton_clicked();
void on_cancelButton_clicked();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
第一个界面源文件:
#include "widget.h"
#include "ui_widget.h"
static QString rightAccount = "admin";
static QString rightPasswd = "123456";
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_clicked()
{
QString account = ui->lineEdit->text();
QString passwd = ui->lineEdit_2->text();
if(account.compare(rightAccount) == 0 && passwd.compare(rightPasswd) == 0){
//this->close();
//emit my_jump();
QMessageBox::information(this,
"",
"登陆成功!",
QMessageBox::Ok
);
this->close();
emit my_jump();
}else{
// ui->lineEdit->clear();
// ui->lineEdit_2->clear();
int ret = QMessageBox::critical(this,
"",
"账号或密码输入错误,是否重新输入?",
QMessageBox::Yes | QMessageBox::No
);
if(ret == QMessageBox::No){
this->close();
}else{
ui->lineEdit->clear();
ui->lineEdit_2->clear();
}
}
}
void Widget::on_cancelButton_clicked()
{
QMessageBox quesMsg(QMessageBox::Question,
"",
"确定要退出吗?",
QMessageBox::Yes | QMessageBox::No,
this);
int res = quesMsg.exec();
if(res == QMessageBox::Yes){
this->close();
}
}
第二个界面头文件:
#ifndef SECOND_H
#define SECOND_H
#include <QWidget>
namespace Ui {
class second;
}
class second : public QWidget
{
Q_OBJECT
public:
explicit second(QWidget *parent = nullptr);
~second();
public slots:
void jump_solt();
private:
Ui::second *ui;
};
#endif // SECOND_H
第二个界面源文件:
#include "second.h"
#include "ui_second.h"
second::second(QWidget *parent) :
QWidget(parent),
ui(new Ui::second)
{
ui->setupUi(this);
this->setWindowTitle("second");
this->setWindowFlag(Qt::FramelessWindowHint);
this->setAttribute(Qt::WA_TranslucentBackground);
}
void second::jump_solt(){
this->show();
}
second::~second()
{
delete ui;
}
作业二:
头文件:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTime>
#include <QTimerEvent>
#include <QTextToSpeech>
#include <QMessageBox>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
void timeEvent(QTimerEvent *e);
private slots:
void on_startButton_clicked();
private:
Ui::Widget *ui;
int id = startTimer(1000);
int id1 = startTimer(1000);
QTextToSpeech *speecher;
};
#endif // WIDGET_H
源文件:
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
, speecher(new QTextToSpeech(this))
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::timeEvent(QTimerEvent *e){
QTime sys_time = QTime::currentTime();
QString t = sys_time.toString("hh-mm-ss");
if(e->timerId() == id){
ui->timeLabel->setText(t);
ui->timeLabel->setAlignment(Qt::AlignCenter);
}else if(e->timerId() == id1){
if(t == ui->timeEdit->text()){
speecher->say(ui->label_4->text());
}
}
}
void Widget::on_startButton_clicked()
{
if(ui->startButton->text() == "启动"){
ui->startButton->setText("关闭");
}else{
killTimer(id);
killTimer(id1);
ui->startButton->setText("启动");
}
}