ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

【Qt Quick】实现轮盘时钟

2021-05-05 14:57:35  阅读:306  来源: 互联网

标签:return Qt parent color text 轮盘 vText Quick font


效果展示

在这里插入图片描述

代码部分

clock.h

#ifndef CLOCK_H
#define CLOCK_H

#include <QObject>
#include <map>


class Clock : public QObject
{
    Q_OBJECT
public:
    explicit Clock(QObject *parent = nullptr);

    Q_INVOKABLE QString mmANDss(int _index);
    Q_INVOKABLE QString hh(int _index);
public:
    std::map<int,QString> map_Hours;
signals:

};

#endif // CLOCK_H

clock.cpp

#include "clock.h"
#include <QStringList>
#include <QDebug>

//QStringList strList = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
QStringList strList = {"零","一","二","三","四","五","六","七","八","九"};
Clock::Clock(QObject *parent) : QObject(parent)
{

}

QString Clock::hh(int _index)
{
    map_Hours.clear();
    for(int i = 1; i < 13; ++i)
    {
        if(i < 10)
        {
            map_Hours[i] = strList.at(i);
        }
        else
        {
            QString tempStr = strList.at(i/10);
            tempStr += strList.at(i - (i/10)*10);
            map_Hours[i] = tempStr;
        }
    }

    return  map_Hours[_index];
}

QString Clock::mmANDss(int _index)
{
    map_Hours.clear();
    for(int i = 1; i <= 60; ++i)
    {
        if(i < 10)
        {
            map_Hours[i] = strList.at(i);
        }
        else if(60 == i)
        {
            map_Hours[i] = "零零";
        }
        else
        {
            QString tempStr = strList.at(i/10);
            tempStr += strList.at(i - (i/10)*10);
            map_Hours[i] = tempStr;
        }
    }

    return  map_Hours[_index];
}

MyText.qml

import QtQuick 2.0

Text {
    id:text_Rotation
    text: ""
    color: "#252525"
    font.bold: true
    font.pointSize: 12
    anchors.centerIn: parent
}

Time.qml

import QtQuick 2.0
import QtQuick.Controls 2.5
import QtQuick.Layouts 1.3

Page {
    id:time_ROOT

    background: null
    property var textColor: "white"

    //获取时分秒
    function currentHour(){
        return Qt.formatDateTime(new Date(),"hh");
    }
    function currentMinu(){
        return Qt.formatDateTime(new Date(),"mm");
    }
    function currentSeco(){
        return Qt.formatDateTime(new Date(),"ss");
    }
    //获取日期
    function currentDate(){
        return Qt.formatDateTime(new Date(),"yyyy年MM月dd日");
    }
    //获取星期几
    function currentWeek(){
        return Qt.formatDateTime(new Date(),"ddd");
    }

    Component.onCompleted: {
        timer.start()

        for(var i = 1; i <= 60; ++i){
            loadTextMinute((i-1)*6,$CL.mmANDss(i))
        }

        for(var j = 1; j <= 60; ++j){
            loadTextSecond((j-1)*6,$CL.mmANDss(j))
        }

        for(var k = 1; k < 13; ++k){
            loadTextHour((k-1)*30,$CL.hh(k))
        }
    }


    Rectangle {id:rect_Hour;color: "#00000000";anchors.fill: parent}
    Rectangle {id:rect_Minu;color: "#00000000";anchors.fill: parent}
    Rectangle {id:rect_Seco;color: "#00000000";anchors.fill: parent}
    //时
    function loadTextHour(_angle,_text){
        var component = Qt.createComponent("MyText.qml")
        if(component.status === Component.Ready){
            var vText = component.createObject(rect_Hour);
            vText.text = "\t\t\t" + _text
            //vText.color = "red"

            vText.rotation = _angle

            return true
        }
        return false
    }

    //分
    function loadTextMinute(_angle,_text){
        var component = Qt.createComponent("MyText.qml")
        if(component.status === Component.Ready){
            var vText = component.createObject(rect_Minu);
            vText.text = "\t\t\t\t\t" + _text
            //vText.color = "#252525"

            vText.rotation = _angle

            return true
        }
        return false
    }

    //秒
    function loadTextSecond(_angle,_text){
        var component = Qt.createComponent("MyText.qml")

        if(component.status === Component.Ready){
            var vText = component.createObject(rect_Seco);
            vText.text = "\t\t\t\t\t\t\t" + _text
            //vText.color = "red"

            vText.rotation = _angle

            return true
        }
        return false
    }


    Text {
        id:text_Second
        text: "\t\t\t\t\t\t\t\t秒"
        color: textColor
        font.bold: true
        font.pointSize: 12
        anchors.centerIn: parent
    }
    Text {
        id:text_Minute
        text: "\t\t\t\t\t\t分"
        color: textColor
        font.bold: true
        font.pointSize: 12
        anchors.centerIn: parent
    }
    Text {
        id:text_Hour
        text: "\t\t\t\t时"
        color: textColor
        font.bold: true
        font.pointSize: 12
        anchors.centerIn: parent
    }

    Column{
        spacing: 5
        anchors.centerIn: parent
        Text {
            id: text_Week
            text: ""
            color: textColor
            font.bold: true
            font.pointSize: 12
            anchors.horizontalCenter: parent.horizontalCenter
        }
        Text {
            id: text_Date
            text: ""
            color: textColor
            font.bold: true
            font.pointSize: 12
            anchors.horizontalCenter: parent.horizontalCenter
        }
    }


    Timer{
        id:timer
        interval: 1;
        repeat: true
        onTriggered: {
            text_Week.text = currentWeek()
            text_Date.text = currentDate()
            rect_Hour.rotation = (currentHour() - 1) * (-30)
            rect_Minu.rotation = (currentMinu() - 1) * (-6)
            rect_Seco.rotation = (currentSeco() - 1) * (-6)
        }
    }

}

标签:return,Qt,parent,color,text,轮盘,vText,Quick,font
来源: https://blog.csdn.net/weixin_44697562/article/details/116425140

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有