一、pro代码
RC_ICONS =countdown.ico
二、mainwindow.cpp代码
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDateTime>
#include <QMessageBox>
#include <QSettings>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowTitle("倒计时");
setWindowIcon(QIcon("countdown.ico"));
InitDateInfor();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::InitDateInfor()
{
// 读取INI文件中的日期
QSettings settings("base.ini", QSettings::IniFormat);
int year = settings.value("date/year").toInt();
int month = settings.value("date/month").toInt();
int day = settings.value("date/day").toInt();
QDate targetDate(year, month, day);
QDate currentDate = QDate::currentDate();
// 计算剩余天数
int daysLeft = targetDate.daysTo(currentDate);
// 构造日期字符串
QString date_str = QString("%1-%2-%3").arg(year).arg(month, 2, 10, QChar('0')).arg(day, 2, 10, QChar('0'));
// 读取INI文件中的消息
QString message1 = settings.value("message/message1").toString();
QString last_message1=message1+QString(" ")+date_str;
QString message2 = settings.value("message/message2").toString();
QString last_message2 = QString("%1%4 天").arg(message2).arg(-daysLeft);
ui->label_nowTime->setText(last_message1);
ui->label_lastTime->setText(last_message2);
}
三、base.ini文件
[date]
year=2025
month=2
day=5
[message]
message1="软件正式上线"
message2="剩余天数为:"