QT生成流水账号

时间:2025-03-09 17:07:26

在做数据库课程的时候,要生成财务表,每条记录应该有一个流水账号。

实现思路:

当前时间+随机一个四位数

上代码

//生成流水号
QString adminRecharge::getNumber()
{
QDateTime time = QDateTime::currentDateTime();//获取系统现在的时间
QString number = time.toString("yyyy-MM-dd-hh-mm-ss"); //设置显示格式
QTime t;
t= QTime::currentTime();
qsrand(t.msec()+t.second()*);
int n = qrand();
return number+QString("-%1").arg(n);//添加一个随机数
}

用连字符连接在一起是因为可以直接通过连字符切割字符串,这样可以还原有用的时间信息