Qt day4

制作一个时钟

源文件

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

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    t_id=this->startTimer(1000);
    this->setWindowIcon(QIcon("C:/Users/WS/Pictures/1.png"));//改变左上角图标的位
    this->setWindowTitle("时钟");//窗口的名字
    this->setStyleSheet("background-color:pink");//背景


}
//定义绘制事件处理函数
void Widget::paintEvent(QPaintEvent *event)
{
    //实例化一个画家类
    QPainter p1(this);
    //实例化一个画笔使用无参构造
    QPen pen;
    //设置画笔颜色
    pen.setColor(QColor("brown"));
    //设置线性
    pen.setStyle(Qt::SolidLine);
    //设置画笔粗细
    pen.setWidth(5);

    //画笔给画家
    p1.setPen(pen);

    //设置画家坐标起点
    p1.translate(this->width()/2,this->height()/2);
    //设置画刷
    p1.setBrush(QBrush(QColor("pink")));
    //画一个圆圈
    p1.drawEllipse(QPointF(0,0),200,200);
    //颜色
    p1.setPen(QColor("brown"));
    //刻度
    for(int i=0;i<60;i++)
    {
        p1.drawLine(200,0,190,0);
        //旋转
        p1.rotate(6);
    }
    //绘画家设置字体
    p1.setFont(QFont("隶书",20));
    //1-12小时
    for(int i=1;i<=12;i++)
    {
        //旋转
        p1.rotate(30);
        p1.drawLine(0,-200,0,-180);
        p1.drawText(-10,-155,QString("%1").arg(i));
    }
    //画家2
    QPainter p2(this);
    p2.setPen(QColor("brown"));
    p2.translate(this->width()/2, this->height()/2);
    p2.rotate(-90);
    //画笔2
    QPen pen2;
    pen2.setWidth(3);
    pen2.setColor(QColor("red"));
    p2.setPen(pen2);
    //秒针
    p2.rotate(ss*6);
    p2.drawLine(0,0,150,0);
    //分针
    pen2.setColor(QColor("brown"));
    pen2.setWidth(5);
    p2.setPen(pen2);
    p2.rotate(-ss*6);
    p2.rotate(mm*6+ss*6/60);
    p2.drawLine(0,0, 150,0);
    //时针
    p2.rotate(-mm*6-ss*6/60);
    p2.rotate(hh*30+mm*6/12+ss*6/60/12);
    p2.drawLine(0,0, 100,0);


}
void Widget::timerEvent(QTimerEvent *event)
{

    if(event->timerId()==t_id)
    {
        //数字显示当前时间
        QDateTime sysDateTime=QDateTime::currentDateTime();
        //讲QTime类对象转化为字符串
        QString t=sysDateTime.toString("hh:mm:ss");
        //展示到ui界面
        ui->nowtime->setText(t);
        //获取时分秒
        QStringList list =t.split(":");
        hh = list[0].toUInt();
        mm = list[1].toUInt();
        ss = list[2].toUInt();
        update();
    }
}
Widget::~Widget()
{
    delete ui;
}

头文件

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include<QPaintEvent>           //绘制事件类
#include<QDebug>               //信息调试类
#include<QPainter>              //画家类
#include<QTimerEvent>
#include<QDateTime>
#include<QDateTime>
#include <QTimer>
#include <QIcon>
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 *event) override;
    void paintEvent(QPaintEvent *event) override;//画


private:
    Ui::Widget *ui;
    int t_id;
    QTimer *timer;//显示屏显示时间
    int hh=0,mm=0,ss=0;


};
#endif // WIDGET_H

pro

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值