QT 第四天

一、设置一个闹钟

.pro

QT       += core gui texttospeech

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

.h

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include     //标签
#include  //行编辑器
#include //按钮
#include   //文本编辑器
#include   //文本转语音的头文件
#include    //定时器类的头文件
#include     //时间类
#include //定时器时间处理类
#include   //日期时间类

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)override;

private slots:
    void on_btn1_clicked();//启动按钮的槽函数
    void on_btn2_clicked();//结束按钮的槽函数

private:
    Ui::Widget *ui;
    //定义一个播报员指针
    QTextToSpeech *speechr;

    //定义一个定时器指针
    QTimer *time;

    //定义一个定时器标识
    int tid;

    QPushButton *btn1;
    QTextEdit *txt1;
    QPushButton *btn2;
    QLineEdit *edit1;
    QLabel *lab1;
};
#endif // WIDGET_H

.cpp

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

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

    //实例化一个标签,设置为当前时间
    lab1 = new QLabel(" ",this);
    lab1 -> resize(400,150);//设置大小
    lab1->move(60,50);
    lab1->setStyleSheet("background-color:pink");//设置背景颜色
    lab1->setScaledContents(true);//设置内容自适应

    //实例化一个文本编辑器,设置为定的时间
    edit1 = new QLineEdit(this);
    edit1->setText("请输入>>>");    //设置编辑器中的文本内容
    edit1->setPlaceholderText("");   //设置编辑器的占位文本
    edit1->resize(250,75);      //设置尺寸
    edit1->move(lab1->x()+450,50);//移动位置
    edit1->setStyleSheet("background-color:pink");
    edit1->setEnabled(true);   //设置不可用状态4

    //实例化一个按钮,用作启动时间
    btn1 = new QPushButton(this);  //将当前界面设置成父组件
    btn1->setText("启动");
    btn1->resize(120,70);    //设置按钮大小
    btn1->setStyleSheet("background-color:pink");//设置背景颜色
    btn1->move(edit1->x(),edit1->y()+85);
    btn1->setEnabled(true);    //设置是否可用

    //实例化一个按钮,用作结束时间
    btn2 = new QPushButton(this);  //将当前界面设置成父组件
    btn2->setText("停止");
    btn2->resize(120,70);    //设置按钮大小
    btn2->setStyleSheet("background-color:pink");//设置背景颜色
    btn2->move(btn1->x()+130,btn1->y());
    btn2->setEnabled(true);    //设置是否可用

    //实例化一个文本编辑器,播报当定的时间与当前时间相同时的内容
    txt1 = new QTextEdit(this);
    txt1->setText("易烊千玺来叫你起床啦!!!");
    txt1->resize(700,300);
    txt1->setStyleSheet("background-color:pink");//设置背景颜色
    txt1->move(60,lab1->y()+200);

    //给播报员实例化一个空间
    speechr = new QTextToSpeech(this);

    //启动定时器
    tid = this->startTimer(0);  //每隔0秒调用timerEvent函数

    connect(btn1,&QPushButton::clicked,this,&Widget::on_btn1_clicked);   //启动按钮连接槽函数
    connect(btn2,&QPushButton::clicked,this,&Widget::on_btn2_clicked);   //启动按钮连接槽函数
}

Widget::~Widget()
{
    delete ui;
}
//定时器闹钟启动按钮
void Widget::on_btn1_clicked()
{
    //启动按钮禁用
    this->btn1->setEnabled(false);

    //将文本编辑器禁用
    this->txt1->setEnabled(false);

    //关闭按钮开启
    this->btn2->setEnabled(true);
}
//定时器闹钟关闭按钮
void Widget::on_btn2_clicked()
{
    //启动按钮开启
    this->btn2->setEnabled(false);

    //将文本编辑器禁用
    this->txt1->setEnabled(true);

    //关闭按钮关闭
    this->btn1->setEnabled(true);
}
//定时器事件处理函数
void Widget::timerEvent(QTimerEvent *e)
{
    QDateTime sys_time = QDateTime::currentDateTime();  //获取系统当前的日期时间
    this->lab1->setText(sys_time.toString("yyyy-MM-dd-hh:mm:ss"));//将时间展示到lab1界面
    QString t = sys_time.toString("hh:mm:ss");
    this->lab1->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);//设置lab1中的内容居中显示

    if(t == this->edit1->text() && this->btn1->isEnabled()== false)
    {
        speechr->say(this->txt1->toPlainText());
    }
    else if(this->btn1->isEnabled()== true)
    {
        speechr->stop();
    }
}

QT 第四天_第1张图片

二、使用绘制事件完成钟表的实现

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    widget.cpp

HEADERS += \
    widget.h

FORMS += \
    widget.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

.h

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include
#include
#include
#include
#include
#include
#include

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(int width, int height,QWidget *parent = nullptr);
    ~Widget();

    void paintEvent(QPaintEvent *event);

private:
    Ui::Widget *ui;

    QTimer *timer;

    int count = 0;

    int hour;
    int minute ;
    int second ;
};
#endif // WIDGET_H

main.cpp

#include "widget.h"

#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w(860, 640);
    w.show();
    return a.exec();
}

widget.cpp

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

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

    timer = new QTimer;
    timer->start(1000);

    connect(timer, &QTimer::timeout, [&](){
        count++;
        update();
    });

    //获取系统时间
    QString t = QTime::currentTime().toString("h:m:s ap");

    //qDebug()<width()/2,this->height()/2);
    p.drawEllipse(QPoint(0,0), 200,200);

    //使用画家类绘制刻度
    pen.setColor(QColor("black"));
    p.setPen(pen);
    for(int i=0; i<60; i++)
    {
        p.rotate(6);
        p.drawLine(QPoint(200,0), QPoint(195,0));

    }

    pen.setWidth(5);
    p.setPen(pen);
    for(int i=0; i<12; i++)
    {

        p.drawLine(QPoint(200,0), QPoint(190,0));
        p.rotate(30);
        p.drawText(QPoint(0, -170),QString("%1").arg(i+1));


    }
    //制作时针
    pen.setWidth(10);
    pen.setColor(QColor("red"));
    p.setPen(pen);
    p.rotate(hour*30+6*second/60/12+30*minute/60+6*count/60/12);       //6*count/60/12+
    p.drawLine(QPoint(0,-50), QPoint(0, 5));

    //制作分针
    QPainter p1(this);
    p1.translate(this->width()/2,this->height()/2);
    pen.setWidth(6);
    pen.setColor(QColor("blue"));
    p1.setPen(pen);
    p1.rotate(6*count/60+minute*6+6*second/60);
    p1.drawLine(QPoint(0,-80), QPoint(0, 8));

    //制作秒针
    QPainter p2(this);
    p2.translate(this->width()/2,this->height()/2);
    pen.setWidth(3);
    pen.setColor(QColor("green"));
    p2.setPen(pen);
    p2.rotate(6*count+second*6);
    p2.drawLine(QPoint(0,-120), QPoint(0, 12));
}

QT 第四天_第2张图片

你可能感兴趣的:(qt,开发语言)