Qt-如何生成GUID

相关资料:

https://blog.csdn.net/hellokandy/article/details/119753115

 

在Windows环境下,生成GUID的代码基本是下面这样的:

 1 const char* MakeGuid()
 2 {
 3     CoInitialize(NULL);
 4     static char buf[64] = { 0 };
 5     GUID guid;
 6     if (S_OK == ::CoCreateGuid(&guid))
 7     {
 8         _snprintf_s(buf, sizeof(buf),
 9             "{%08X-%04X-%04x-%02X%02X-%02X%02X%02X%02X%02X%02X}",
10             guid.Data1,
11             guid.Data2,
12             guid.Data3,
13             guid.Data4[0],
14             guid.Data4[1],
15             guid.Data4[2],
16             guid.Data4[3],
17             guid.Data4[4],
18             guid.Data4[5],
19             guid.Data4[6],
20             guid.Data4[7]);
21     }
22     CoUninitialize();
23     return (const char*)buf;
24 }
View Code

但是通过QT,代码可以简单很多:

 1 #include <QtCore/QCoreApplication>
 2 #include <QUuid>
 3 #include <QDebug>
 4 
 5 int main(int argc, char *argv[])
 6 {
 7     QCoreApplication a(argc, argv);
 8 
 9     QUuid id = QUuid::createUuid();
10     QString strGuid = id.toString();//QUuid::WithBraces
11     qDebug() << "strGuid=" << strGuid;
12     //strGuid= "{7c6d16ac-1517-45cd-86d2-f6aa4a25a459}"
13 
14     strGuid = id.toString(QUuid::WithoutBraces);
15     qDebug() << "strGuid(WithoutBraces)=" << strGuid;
16     //strGuid(WithoutBraces)= "7c6d16ac-1517-45cd-86d2-f6aa4a25a459"
17 
18     strGuid = id.toString(QUuid::Id128);
19     qDebug() << "strGuid(Id128)=" << strGuid;
20     //strGuid(Id128)= "7c6d16ac151745cd86d2f6aa4a25a459"
21 
22     return a.exec();
23 }/119753115
View Code

 

posted on 2022-08-11 14:12  疯狂delphi  阅读(194)  评论(0编辑  收藏  举报

导航