Qt 寿命倒计时设计实现
2021/7/14 23:36:12
本文主要是介绍Qt 寿命倒计时设计实现,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Qt 寿命倒计时设计实现
文章目录
- 概述
- 设计思路
- 核心源码
- 声明
- 实现
- 数据保存加载
- 结尾
概述
本篇文章实现一个用来倒计时人生已经度过时间的计算器,珍惜生活的每一天。
设计思路
这个小软件逻辑比较简单,花了一两个小时完成总体设计开发,逻辑如下:
- 根据出生日期和当前日期的月份差
- 根据平均寿命计算总月份
- 渲染绘制已经度过的月份和剩余的月份
- 保存&加载姓名、出生、平均岁数这几个参数到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 寿命倒计时设计实现的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-26结对编程到底难不难?答案在这里
- 2024-06-19《2023版Java工程师》课程升级公告
- 2024-06-15matplotlib作图不显示3D图,怎么办?
- 2024-06-1503-Loki 日志监控
- 2024-06-1504-让LLM理解知识 -Prompt
- 2024-06-05做软件测试需要懂代码吗?
- 2024-06-0514-ShardingSphere的分布式主键实现
- 2024-06-03为什么以及如何要进行架构设计权衡?
- 2024-05-31全网首发第二弹!软考2024年5月《软件设计师》真题+解析+答案!(11-20题)
- 2024-05-31全网首发!软考2024年5月《软件设计师》真题+解析+答案!(21-30题)