QT作业2

1>

代码:

头文件

cpp 复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include<iostream>
#include<QDebug>
#include<QIcon>
#include<QPushButton>
#include<QLabel>
#include<QMovie>
#include<QLineEdit>
#include<QMessageBox>
#include<QTimer>
#include<QTime>
#include<QString>
#include<QTimerEvent>
#include<QPushButton>
#include<QDateTime>
#include<QTextToSpeech>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

public slots:
    void timeout_slot();        //自定义槽函数的声明
    void my_start();
    void my_cancel();

private:
    Ui::Widget *ui;

    QTimer t1;
    QLabel *lab1;
    QLabel *lab2;
    QLineEdit *edit1;
    QPushButton *btn1;
    QPushButton *btn2;
    QTextToSpeech *s1;
};
#endif // WIDGET_H

源文件

cpp 复制代码
#include "widget.h"
#include "ui_widget.h"

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

    this->setFixedSize(1000,800);        //设置固定尺寸

    this->setWindowIcon(QIcon(":/picture/1.png"));

    //标签1:显示系统时间
    this->lab1 = new QLabel("时间",this);
    lab1->resize(500,150);
    lab1->move(50,50);
    lab1->setStyleSheet("background-color:yellow;");
    this->lab1->setAlignment(Qt::AlignCenter);  //居中
    connect(&t1, &QTimer::timeout, this, &Widget::timeout_slot);  //连接定时器
    t1.start(1000);

    //文本框1:写闹钟时间
    this->edit1 = new QLineEdit(this);
    edit1->resize(350,75);
    edit1->move(600,50);
    edit1->setPlaceholderText("闹钟");

    //按钮1:启动
    this->btn1 = new QPushButton("启动",this);
    btn1->resize(100,50);
    btn1->move(650,150);
    connect(this->btn1, &QPushButton::clicked, this, &Widget::my_start);

    //按钮2:取消
    this->btn2 = new QPushButton("取消",this);
    btn2->resize(100,50);
    btn2->move(800,150);
    btn2->setEnabled(false);
    connect(this->btn2, &QPushButton::clicked, this, &Widget::my_cancel);

    //标签2
    this->lab2 = new QLabel(this);
    lab2->resize(1000,550);
    lab2->move(0,250);
    lab2->setStyleSheet("background-color:pink;");
    lab2->setPixmap(QPixmap(":/picture/study.png"));  //贴图
    lab2->setScaledContents(true);

    this->s1 = new QTextToSpeech(this);

}

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

void Widget::timeout_slot()
{
    //获取系统时间
    QTime sysTime = QTime::currentTime();
    QString tm = sysTime.toString("hh:mm:ss");
    lab1->setText(tm);
    lab1->setAlignment(Qt::AlignCenter); //居中
    QFont f;                            //设置字体大小
    f.setPointSize(50);
    lab1->setFont(f);
    if(lab1->text() == edit1->text())
    {
        btn1->setEnabled(true);
        btn2->setEnabled(false);
        edit1->setEnabled(true);
        edit1->clear();
        s1->say("劝学\
                三更灯火五更鸡,正是男儿读书时。\
                黑发不知勤学早,白首方悔读书迟。");
    }
}

void Widget::my_start()
{
    btn1->setEnabled(false);
    btn2->setEnabled(true);
    edit1->setEnabled(false);
}

void Widget::my_cancel()
{
    btn1->setEnabled(true);
    btn2->setEnabled(false);
    edit1->setEnabled(true);
    edit1->clear();
}

运行结果:

2>思维导图

相关推荐
思考的笛卡尔10 分钟前
Go语言实战:高并发服务器设计与实现
服务器·开发语言·golang
努力努力再努力wz21 分钟前
【C++进阶系列】:万字详解智能指针(附模拟实现的源码)
java·linux·c语言·开发语言·数据结构·c++·python
凤年徐24 分钟前
【C++】string的模拟实现
c语言·开发语言·c++
敲代码的嘎仔37 分钟前
JavaWeb零基础学习Day2——JS & Vue
java·开发语言·前端·javascript·数据结构·学习·算法
吃鱼吃鱼吃不动了37 分钟前
什么是负载均衡?
开发语言·php
小蕾Java1 小时前
Python详细安装教程(附PyCharm使用)
开发语言·python·pycharm
weixin_307779131 小时前
AWS云上ClickHouse数据仓库部署方案详解
开发语言·clickhouse·自动化·云计算·aws
weixin_307779131 小时前
使用AWS IAM和Python自动化权限策略分析与导出
开发语言·python·自动化·云计算·aws
惜月_treasure1 小时前
从零构建私域知识库问答机器人:Python 全栈实战(附完整源码)
开发语言·python·机器人
咯哦哦哦哦1 小时前
关于QT 打印中文 乱码问题
java·数据库·qt