Qt 每月收支计算,针对每月有支出(房贷、车贷、花呗、借呗)的情况,计算收支明细,直观看到未来的个人经济情况,培养良好的消费习惯,进行理性的财富支配,量入为出。
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDate>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QFont font;
font.setPixelSize(16);
setFont(font);
setWindowTitle(QStringLiteral("每月收支计算"));
ui->lineEdit_shouru->setMaximumWidth(70);
ui->lineEdit_shouru->setMinimumWidth(70);
ui->lineEdit_huan->setMaximumWidth(70);
ui->lineEdit_huan->setMinimumWidth(70);
ui->lineEdit_curent_money->setMaximumWidth(70);
ui->lineEdit_curent_money->setMinimumWidth(70);
ui->dateEdit_shouru->setCalendarPopup(true);
ui->dateEdit_shouru->setDate(QDate::currentDate());
ui->dateEdit_huan->setCalendarPopup(true);
ui->dateEdit_huan->setDate(QDate::currentDate());
ui->dateEdit_huanqing->setCalendarPopup(true);
ui->dateEdit_huanqing->setDate(QDate::currentDate());
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_add_clicked()
{
int huan = ui->lineEdit_huan->text().toInt();
QDate huanDay = ui->dateEdit_huan->date();
QDate huanqingDay = ui->dateEdit_huanqing->date();
QMap<QDate, int> tmpMap;
QDate tmp = huanDay;
while (tmp <= huanqingDay)
{
tmpMap.insert(tmp, huan);
tmp = tmp.addMonths(1);
}
foreach (QDate date, tmpMap.keys())
{
ui->textBrowser_huan->append(QStringLiteral("%1 %2").arg(date.toString("yyyy-MM-dd")).arg(tmpMap.value(date)));
if (huanMap.contains(date))
{
huanMap.insert(date, huanMap.value(date) + tmpMap.value(date));
}
else
{
huanMap.insert(date, tmpMap.value(date));
}
}
ui->textBrowser_huan->append("\n");
}
void MainWindow::on_pushButton_calc_clicked()
{
if (huanMap.isEmpty())
{
return;
}
ui->textBrowser_total->clear();
QStringList monthLst;
QList<QDate> huanDateLst = huanMap.keys();
foreach (QDate date, huanDateLst)
{
QString key = date.toString("yyyy-MM");
if (!monthLst.contains(key))
{
monthLst.append(key);
}
}
int shouru = ui->lineEdit_shouru->text().toInt();
int shouruDay = ui->dateEdit_shouru->date().day() - 1;
QMap<QDate, int> shouruMap;
foreach (QString key, monthLst)
{
QDate date = QDate::fromString(key, "yyyy-MM").addDays(shouruDay);
if (!huanDateLst.contains(date))
{
huanDateLst.append(date);
}
shouruMap.insert(date, shouru);
}
qSort(huanDateLst.begin(), huanDateLst.end());
int shouruTotal = 0;
int huanTotal = 0;
int currentMoney = ui->lineEdit_curent_money->text().toInt();
foreach (QString month, monthLst)
{
int total = 0;
foreach (QDate date, huanDateLst)
{
QString dateStr = date.toString("yyyy-MM-dd");
if (dateStr.startsWith(month))
{
if (shouruMap.contains(date))
{
currentMoney += shouruMap.value(date);
ui->textBrowser_total->append(QStringLiteral("%1 +%2 余%3").arg(dateStr).arg(shouruMap.value(date)).arg(currentMoney));
shouruTotal += shouruMap.value(date);
}
if (huanMap.contains(date))
{
currentMoney -= huanMap.value(date);
ui->textBrowser_total->append(QStringLiteral("%1 -%2 余%3").arg(dateStr).arg(huanMap.value(date)).arg(currentMoney));
total += huanMap.value(date);
}
}
}
ui->textBrowser_total->append(QStringLiteral("%1 共还%2\n").arg(month).arg(total));
huanTotal += total;
}
ui->textBrowser_total->append(QStringLiteral("总收入:%1 总支出:%2 净:%3 余:%4\n").arg(shouruTotal).arg(huanTotal).arg(shouruTotal + ui->lineEdit_curent_money->text().toInt() - huanTotal).arg(currentMoney));
}
void MainWindow::on_pushButton_clear_clicked()
{
if (informationMessageBox(QStringLiteral("提示"), QStringLiteral("确定清空数据"), false))
{
huanMap.clear();
ui->lineEdit_shouru->clear();
ui->lineEdit_huan->clear();
ui->lineEdit_curent_money->clear();
ui->dateEdit_shouru->setDate(QDate::currentDate());
ui->dateEdit_huan->setDate(QDate::currentDate());
ui->dateEdit_huanqing->setDate(QDate::currentDate());
ui->textBrowser_huan->clear();
ui->textBrowser_total->clear();
}
}
bool MainWindow::informationMessageBox(const QString &title, const QString &text, bool isOnlyOk)
{
QMessageBox msgBox(this);
msgBox.setFont(this->font());
msgBox.setIcon(QMessageBox::Information);
msgBox.setWindowTitle(title);
msgBox.setText(text);
if (isOnlyOk)
{
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setButtonText(QMessageBox::Ok, QStringLiteral("确定"));
}
else
{
msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
msgBox.setButtonText(QMessageBox::Ok, QStringLiteral("确定"));
msgBox.setButtonText(QMessageBox::Cancel, QStringLiteral("取消"));
}
return (msgBox.exec() == QMessageBox::Ok);
}