QT生成流水账号

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

实现思路:

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

上代码

//生成流水号
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()*1000);
    int n = qrand();
    return number+QString("-%1").arg(n);//添加一个随机数
}

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

原文地址:https://www.cnblogs.com/superxuezhazha/p/6233655.html