QT DAY 4

时钟:

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

int hour=0;
int min=0;
int sec=0;
int count=0;
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setFixedSize(800,600);
    timer = new QTimer;
    timer->start(1000);

   //int count =0 ;
    connect(timer, &QTimer::timeout,[&](){
        count++;
        update();
    });
 
    //调用QTime的静态成员函数获取当前系统时间
    QTime sys_time = QTime::currentTime();
    //获取时分秒
     hour = sys_time.hour();
     min = sys_time.minute();
    sec=sys_time.second();
    //将时间类对象调用函数转化为字符串
    QString t =sys_time.toString("hh:mm:ss");
}

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

void Widget::paintEvent(QPaintEvent *event)
{
    //定义画家
    QPainter p(this);
    QPen pen(QColor("black"));
    pen.setWidth(3);
    QBrush b("white");
    p.setPen(pen);
    p.setBrush(b);
    p.translate(this->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*sec/60/12+30*min/60+6*count/6/12);
    p.drawLine(QPoint(0,-60),QPoint(0,5));

    //指针分针
    QPainter p1(this);
    p1.translate(this->width()/2,this->height()/2);
    pen.setWidth(8);
    pen.setColor(QColor("green"));
    p1.setPen(pen);
    p1.rotate(6*count/60+min*6+6*sec/60);
    p1.drawLine(QPoint(0,-100),QPoint(0,8));


    //指针秒针
    QPainter p2(this);
    p2.translate(this->width()/2,this->height()/2);
    pen.setWidth(6);
    pen.setColor(QColor("yellow"));
    p2.setPen(pen);
    p2.rotate(6*count+6*sec*6);
    p2.drawLine(QPoint(0,-150),QPoint(0,12));
}

time

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

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    speecher = new QTextToSpeech(this);
    //实例化一个定时器
    timer = new QTimer(this);
    connect(timer,&QTimer::timeout,this,&Widget::timeout_slot);
    timer->start(1000);//1秒
    ui->evenB->setEnabled(false);



}

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


//基于属性的按钮对应的槽函数
void Widget::on_objB_clicked()
{

     ui->evenB->setEnabled(true);
    ui->objB->setEnabled(false);
    ui->evenL->setEnabled(false);
    ui->textEdit->setEnabled(false);

}
//处理timeout信号对应的函数
void Widget::timeout_slot()
{
    //调用QTime的静态成员函数获取当前系统时间
    QTime sys_time = QTime::currentTime();
    //获取时分秒
//    int hour = sys_time.hour();
//    int min = sys_time.minute();
//    int sec=sys_time.second();
    //将时间类对象调用函数转化为字符串
    QString t =sys_time.toString("hh:mm:ss");
    ui->objL->setText(t);
    ui->objL->setAlignment(Qt::AlignCenter);
    ui->objL->setFont(QFont("楷体",20));
    if(!ui->objB->isEnabled())
    {
     if(ui->objL->text()==ui->evenL->text())
     {
        ui->evenL->setText("");
        ui->objB->setEnabled(true);
        ui->evenL->setEnabled(true);
        ui->evenB->setEnabled(false);
        ui->textEdit->setEnabled(true);
        QString text = ui->textEdit->toPlainText();
        speecher->say(text);
      }
    }

}
void Widget::on_evenB_clicked()
{

    ui->objB->setEnabled(true);
     ui->evenL->setEnabled(true);
     ui->evenB->setEnabled(false);
     ui->textEdit->setEnabled(true);
}
//定时器事件处理函数的实现
void Widget::timerEvent(QTimerEvent *event)
{

}
相关推荐
编程版小新几秒前
C++初阶:STL详解(四)——vector迭代器失效问题
开发语言·c++·迭代器·vector·迭代器失效
c4fx20 分钟前
Delphi5利用DLL实现窗体的重用
开发语言·delphi·dll
鸽芷咕43 分钟前
【Python报错已解决】ModuleNotFoundError: No module named ‘paddle‘
开发语言·python·机器学习·bug·paddle
Jhxbdks1 小时前
C语言中的一些小知识(二)
c语言·开发语言·笔记
java6666688881 小时前
如何在Java中实现高效的对象映射:Dozer与MapStruct的比较与优化
java·开发语言
Violet永存1 小时前
源码分析:LinkedList
java·开发语言
代码雕刻家1 小时前
数据结构-3.1.栈的基本概念
c语言·开发语言·数据结构
Fan_web1 小时前
JavaScript高级——闭包应用-自定义js模块
开发语言·前端·javascript·css·html
梦想科研社1 小时前
【无人机设计与控制】四旋翼无人机俯仰姿态保持模糊PID控制(带说明报告)
开发语言·算法·数学建模·matlab·无人机
风等雨归期1 小时前
【python】【绘制小程序】动态爱心绘制
开发语言·python·小程序