其他分享
首页 > 其他分享> > Qt 寿命倒计时设计实现

Qt 寿命倒计时设计实现

作者:互联网

Qt 寿命倒计时设计实现

文章目录

概述

本篇文章实现一个用来倒计时人生已经度过时间的计算器,珍惜生活的每一天。

在这里插入图片描述

设计思路

这个小软件逻辑比较简单,花了一两个小时完成总体设计开发,逻辑如下:

  1. 根据出生日期和当前日期的月份差
  2. 根据平均寿命计算总月份
  3. 渲染绘制已经度过的月份和剩余的月份
  4. 保存&加载姓名、出生、平均岁数这几个参数到xml

核心源码

声明

/****************************************************************
 Doc    :   widget.h
 Author :   BingLee
 Date   :   2021-07-14
 Info   :   life counter head file.
            this class for calculate how much life time have past.
 https://blog.csdn.net/Bing_Lee (C)All rights reserved.
******************************************************************/
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QList>

class QDateTime;
class QLabel;
class QGridLayout;

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

private:
    void init();
    void refreshShow();
    void load();
    void save();
    bool readFileToString(const QString &path, QString &documentFullStr);
    bool writeStringToFile(const QString &path, QString &documentFullStr);

    bool readXmlData(QString &documentFullStr);
    bool writeXmlData(QString &documentFullStr);

private slots:
    void slot_calculate();

private:
    Ui::Widget *ui;

    QString m_logPath;
    QString m_name;
    int m_age;
    int m_row;
    int m_colume;
    int m_month;
    int m_squareLength;
    QDateTime *m_birthDate;
    QDateTime *m_todayDate;

    QList<QLabel *> m_labelList;
    QGridLayout *m_gridLayout;
};

#endif // WIDGET_H

实现

void Widget::slot_calculate()   //计算度过的月份和剩余月份. 2021-07-14 https://blog.csdn.net/Bing_Lee
{
    m_name = ui->LE_Name->text();
    m_age = ui->LE_Age->text().toUInt();
    *m_birthDate = QDateTime::fromString(ui->LE_Birth->text(), DATE_STYLE);
    ui->LE_Today->setText(m_todayDate->toString(DATE_STYLE));

    int days = m_birthDate->daysTo(*m_todayDate);
    m_month = days / 30;
    m_squareLength = qSqrt(m_age * 12);
    refreshShow();
    ui->progressBar->setValue(m_month * 100 / m_age / 12 );
    save();
}

bool Widget::readFileToString(const QString &path, QString &documentFullStr)         //读取文件到QString. 2021-07-14 https://blog.csdn.net/Bing_Lee
{
    if (!QFile::exists(path))
    {
        qDebug()<<"file doesn't exist!";
        return false;
    }
    QFile xmlFile(path);
    if (!xmlFile.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        qDebug()<<path<<" open failed!";
        return false;
    }
    QTextStream reader(&xmlFile);
    documentFullStr.clear();
    documentFullStr = reader.readAll();
    return true;
}

bool Widget::writeStringToFile(const QString &path, QString &documentFullStr)        //写入QString到文件. 2021-07-14 https://blog.csdn.net/Bing_Lee
{
    if (!QFile::exists(path))
    {
        qDebug()<<"file doesn't exist!";
    }
    QFile file(path);
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
    {
        return false;
    }
    QTextStream writer(&file);//写入
    writer<<documentFullStr;
    file.close();
    return true;
}

bool Widget::readXmlData(QString &documentFullStr)        //解析xml文档. 2021-07-14 https://blog.csdn.net/Bing_Lee
{
    QXmlStreamReader reader(documentFullStr);
    QString nameStr, birthStr, ageStr;
    QXmlStreamAttributes attributes;
    while (!reader.atEnd())
    {
        if (reader.isStartElement())
        {
            attributes = reader.attributes();
            if (reader.name() == "Name")
            {
                nameStr = attributes.value("value").toString();
            }
            else if (reader.name() == "Birth")
            {
                birthStr = attributes.value("value").toString();
            }
            else if (reader.name() == "Age")
            {
                ageStr = attributes.value("value").toString();
            }
        }
        reader.readNext();
    }
    if (reader.hasError())  //error check, and output error infomation.
    {
        qDebug()<<"ProductXmlProcess" << " read error: "<< reader.errorString();
        return false;
    }
    else
    {
        if (!nameStr.isEmpty() && !birthStr.isEmpty() && !ageStr.isEmpty())
        {
            m_name = nameStr;
            *m_birthDate = QDateTime::fromString(birthStr, DATE_STYLE);
            m_age = ageStr.toInt();
        }
        return true;
    }
}

bool Widget::writeXmlData(QString &documentFullStr)        //保存数据到xml. 2021-07-14 https://blog.csdn.net/Bing_Lee
{
    QXmlStreamWriter writer(&documentFullStr);
    writer.setAutoFormatting(true);
    writer.writeStartDocument();
    writer.writeStartElement("xsl:stylesheet");
    writer.writeAttribute("xmlns:xsl", "http://www.w3.org/1999/XSL/Transform");
    {
        writer.writeStartElement("Name");
        writer.writeAttribute("value", m_name);
        writer.writeEndElement();

        writer.writeStartElement("Birth");
        writer.writeAttribute("value", m_birthDate->toString(DATE_STYLE));
        writer.writeEndElement();

        writer.writeStartElement("Age");
        writer.writeAttribute("value", QString::number(m_age));
        writer.writeEndElement();
    }
    writer.writeEndElement();
    writer.writeEndElement();
    writer.writeEndDocument();
    return true;
}

数据保存加载

保存采用新建或加载log.xml文件实现,如下:

在这里插入图片描述

在这里插入图片描述

结尾

如果喜欢本文,请帮忙给博主点赞、评论、关注三连哦~

源代码下载链接

标签:寿命,Qt,documentFullStr,writer,Widget,value,倒计时,QString,reader
来源: https://blog.csdn.net/Bing_Lee/article/details/118739803