C++ day4

登录界面

second.h

#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 mySlot();
private:
    Ui::Second *ui;
};

#endif // SECOND_H

  widget.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 slots:
    void on_pushButton_clicked();
    void on_closeButton_clicked();

    void on_shrinkButton_clicked();

    void on_cancelButton_clicked();

signals:
    void jumpToSecondWindow();
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;
    w.show();
    Second s;
    QObject::connect(&w, &Widget::jumpToSecondWindow, &s, &Second::mySlot);
    return a.exec();
}

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::mySlot()
{
   show();
}

widget.cpp

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    setWindowFlag(Qt::FramelessWindowHint);

    // 去掉空白
    setAttribute(Qt::WA_TranslucentBackground);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_clicked()
{
    if(ui->actLineEdit->text() == "admin" && ui->pwdLineEdit->text() == "123456")
    {
      //  int ret = QMessageBox::information(this, "Login Success", "登录成功!");
        QMessageBox msg;
        msg.setIcon(QMessageBox::Information);
        msg.setStandardButtons(QMessageBox::Ok);
        msg.setText("登录成功");
        msg.setInformativeText("!!!!!!!!!");
        int ret = msg.exec();
        if(ret == QMessageBox::Ok)
        {
            close();
            emit jumpToSecondWindow();
        }
    }
    else
    {
        int ret = QMessageBox::critical(this, "Login Error", "账号或密码错误!", QMessageBox::Yes | QMessageBox::No);
        if(ret == QMessageBox::Yes)
        {
            // 清空账号和密码输入行
            ui->actLineEdit->clear();
            ui->pwdLineEdit->clear();
        }
        else
        {
            close();
        }

    }
}

void Widget::on_closeButton_clicked()
{
    close();
}

void Widget::on_shrinkButton_clicked()
{
    showMinimized();
}

void Widget::on_cancelButton_clicked()
{
    QMessageBox msg(QMessageBox::Question, "退出确认", "您是否确定要退出登录?", QMessageBox::Yes | QMessageBox::No, this);
    int ret = msg.exec();
    if(ret == QMessageBox::Yes)
    {
        close();
    }
}

闹钟

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTime>
#include <QTextToSpeech>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
    void timerEvent(QTimerEvent *e);
    QString showTime();

private slots:
    void on_pushButton_clicked();

private:
    Ui::Widget *ui;
    QTextToSpeech * speecher;
    QString time;
};
#endif // WIDGET_H

 widget.cpp

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
    , speecher(new QTextToSpeech(this))
{
    ui->setupUi(this);

    showTime();
    startTimer(1000);
    ui->hourLineEdit->setPlaceholderText("时");
    ui->minuteLineEdit->setPlaceholderText("分");
    ui->secondLineEdit->setPlaceholderText("秒");
}

Widget::~Widget()
{
    delete ui;
}

void Widget::timerEvent(QTimerEvent *e)
{
    QString currentTime = showTime();
    if (currentTime == time)
    {
        QString hintMsg = "这个年纪你怎么,睡得着啊!!!!!";
        ui->hintLabel->setText(hintMsg);

        int *count = new int(0);
        connect(speecher, &QTextToSpeech::stateChanged, this, [=]()mutable{
            if (speecher->state() == QTextToSpeech::Ready)
            {
                (*count)++;
                if (*count == 5)
                {
                    ui->hintLabel->clear();
                    delete count;
                    return;
                }
                speecher->say(hintMsg);
            }
        });

        speecher->say(hintMsg); // 开始第一次读
    }
}

QString Widget::showTime()
{
    QTime currentTime = QTime::currentTime();

    // 将QTime时间格式转换为QString类
    QString t = currentTime.toString("hh:mm:ss");

    ui->timeLabel->setText(t);

    return t;
}

void Widget::on_pushButton_clicked()
{
    time = ui->hourLineEdit->text()+":"+ui->minuteLineEdit->text()+":"+ui->secondLineEdit->text();
    ui->timeListLabel->setText(ui->timeListLabel->text()+time+";");
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值