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 }
但是通过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
作者:疯狂Delphi
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
欢迎关注我,一起进步!扫描下方二维码即可加我