Qt day4

制作一个时钟

Qt day4_第1张图片

源文件

#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 
#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 *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

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