作业1:
mywidget.h
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>
#include <QLabel>
#include <QMessageBox>
QT_BEGIN_NAMESPACE
namespace Ui { class myWidget; }
QT_END_NAMESPACE
class myWidget : public QWidget
{
Q_OBJECT
public:
myWidget(QWidget *parent = nullptr);
~myWidget();
signals:
void myjump();
public slots:
void play();
private slots:
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
void on_pushButton_4_clicked();
private:
Ui::myWidget *ui;
};
#endif // MYWIDGET_H
main.cpp
#include "mywidget.h"
#include "second.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
myWidget w;
second s;
w.show();
QObject::connect(&w,&myWidget::myjump,&s,&second::myslot);
return a.exec();
}
mywidget.cpp
#include "mywidget.h"
#include "ui_mywidget.h"
myWidget::myWidget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::myWidget)
{
ui->setupUi(this);
this->setWindowFlag(Qt::FramelessWindowHint);
ui->lineEdit->setPlaceholderText("手机号/QQ号");
ui->lineEdit_1->setEchoMode(QLineEdit::Password);
connect(ui->pushButton,&QPushButton::clicked,this,&myWidget::play);
}
//实现点击登录后的相关功能
void myWidget::play()
{
QString text1=ui->lineEdit->text();
QString text2=ui->lineEdit_1->text();
if(text1=="666" && text2=="123456")
{
QMessageBox::information(this,"提示","登录成功",QMessageBox::Ok);
this->close();
emit myjump();
}
else
{
QMessageBox::StandardButton res=QMessageBox::critical(this,"错误","账号和密码不匹配,是否重新登录",QMessageBox::Yes|QMessageBox::No);
if(res==QMessageBox::Yes)
{
ui->lineEdit_1->setText("");
}
else
{
this->close();
}
}
}
myWidget::~myWidget()
{
delete ui;
}
//实现点击取消后的相关功能
void myWidget::on_pushButton_2_clicked()
{
this->showMinimized();
}
void myWidget::on_pushButton_3_clicked()
{
this->close();
}
void myWidget::on_pushButton_4_clicked()
{
QMessageBox::StandardButton res=QMessageBox::question(this,"问题","您是否确定要退出登录?",QMessageBox::Yes|QMessageBox::No);
if(res==QMessageBox::Yes)
{
this->close();
}
}
作业2:
mywidget.h
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>
#include <QTimerEvent>
#include <QTime>
#include <QDebug>
#include <QTextToSpeech>
QT_BEGIN_NAMESPACE
namespace Ui { class myWidget; }
QT_END_NAMESPACE
class myWidget : public QWidget
{
Q_OBJECT
public:
myWidget(QWidget *parent = nullptr);
~myWidget();
void timerEvent(QTimerEvent* E);
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
private:
Ui::myWidget *ui;
int id;
};
#endif // MYWIDGET_H
mywidget.cpp
#include "mywidget.h"
#include "ui_mywidget.h"
myWidget::myWidget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::myWidget)
{
ui->setupUi(this);
this->setWindowTitle("闹钟");
}
myWidget::~myWidget()
{
delete ui;
}
void myWidget::timerEvent(QTimerEvent *E)
{
if(E->timerId()==id)
{
QTime time1=QTime::currentTime();
QString t=time1.toString(("hh:mm:ss"));
ui->label->setText(t);
ui->label->setAlignment(Qt::AlignCenter);
if(ui->label->text()==ui->lineEdit->text())
{
ui->label_3->setText("懒虫起床了! 懒虫起床了!");
QTextToSpeech* speech=new QTextToSpeech(this);
speech->say("懒虫起床了! 懒虫起床了!");
}
}
}
void myWidget::on_pushButton_clicked()
{
QString fel=ui->lineEdit->text();
}
void myWidget::on_pushButton_2_clicked()
{
QString fel=ui->pushButton_2->text();
if(fel=="打开")
{
id=startTimer(1000);
ui->pushButton_2->setText("关闭");
}
else
{
killTimer(id);
ui->pushButton_2->setText("打开");
}
}