关于这个
上个学期刚开学的时候写的,当时似乎是为了看 还有几天考六级写的,就想着 有个倒计时程序,简洁 一点, 然后好看一点, 然后可以像snipast那样贴到桌面上。这个程序就诞生了。关于计算 两个日期之间的天数的代码,qt里面一个函数就搞定了(day_num=QDate::currentDate().daysTo(date);
)
今天重新写了一遍,又搞了点美化,加了圆角和可选颜色。(其实本来打算写成 可以存下好几个日期的,但是感觉文件太麻烦了啊,还是大道至简嘛(doge)。
图
左右两个图标,左边是设置 名称 日期 颜色,右边本来想做成可以选择 不同的 事件,但是没弄。
主要代码
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
this->setAttribute(Qt::WA_TranslucentBackground);
this->setWindowFlags(Qt::FramelessWindowHint|Qt::Tool);
this->ui->btn_setting->hide();
this->ui->btn_list->hide();
readFile();
connect(this->ui->btn_setting,QToolButton::clicked,this,[=](){
QString name;
QDate date;
QColor color;
QFile file("c:\\myfile\\timedata.time");
file.open(QIODevice::ReadOnly);
QDataStream in(&file);
in>>name>>date>>color;
this->setting.setName(name);
this->setting.setDate(date);
this->setting.setColor(color);
this->setting.show();
});
connect(&(this->setting),settingForm::changeData,this,[=](){
this->setting.hide();
writeFile(setting.getName(),setting.getDate(),setting.getColor());
readFile();
this->show();
});
this->setStyleSheet("QLabel{ border-radius:12px;background-color:rgba(111, 210, 98, 255);}QToolButton{background: transparent;}");
}
void Widget::changeColor(QColor color)
{
int r=color.red(),g=color.green(),b=color.blue(),alpha=color.alpha();
this->setStyleSheet(QString("QLabel{ border-radius:12px;background-color:rgba(%1, %2, %3, %4);}QToolButton{background: transparent;}")
.arg(r)
.arg(g)
.arg(b)
.arg(alpha));
}
Widget::~Widget()
{
delete ui;
}
void Widget::readFile()
{
QFile file("c:\\myfile\\timedata.time");
if(!file.exists())
{
bool ok;
QString name=QInputDialog::getText(this,"无缓冲信息,请输入:","倒数日 事件名称:",QLineEdit::Normal,"",&ok);
if(!ok) return;
QString str_date=QInputDialog::getText(this,"无缓冲信息,请输入:","倒数日 事件日期(以yyyy-MM-dd形式输入):",QLineEdit::Normal,"",&ok);
if(!ok) return ;
QDate date=QDate::fromString(str_date,"yyyy-MM-dd");
qDebug()<<"fromstring"<<date;
writeFile(name,date,QColor(111, 210, 98, 120));
readFile();
}
else
{
QString name;
QDate date;
QColor color;
int day_num;
file.open(QIODevice::ReadOnly);
QDataStream in(&file);
in>>name>>date>>color;
qDebug()<<date;
day_num=QDate::currentDate().daysTo(date);
file.close();
this->ui->label->setText(QString("距离%1还有%2天")
.arg(name)
.arg(day_num)
);
changeColor(color);
}
}
void Widget::writeFile(QString name, QDate date, QColor color)
{
//如果没有目录 myfile 创建c://myfile
QDir mydir("C://myfile");
if(!mydir.exists()) mydir.mkdir("C://myfile");
QFile file("c:\\myfile\\timedata.time");
file.open(QIODevice::WriteOnly);
QDataStream out(&file);
out<<name<<date<<color;
file.close();
}
void Widget::mouseMoveEvent(QMouseEvent *ev)
{
this->move(ev->globalPos()-point);
}
void Widget::mousePressEvent(QMouseEvent *ev)
{
this->point=ev->pos();
}
void Widget::enterEvent(QEvent *event)
{
this->ui->btn_setting->show();
this->ui->btn_list->show();
}
void Widget::leaveEvent(QEvent *event)
{
this->ui->btn_setting->hide();
this->ui->btn_list->hide();
}