Util. function for class name

This commit is contained in:
Klaus Basan
2020-04-23 20:15:57 +02:00
committed by Mat Sutcliffe
parent 5987e58043
commit c1622951b3
2 changed files with 31 additions and 2 deletions

View File

@@ -9,8 +9,9 @@
#include "metadatautils.h"
#include <QMetaType>
#include <QTextStream>
#include <QMetaObject>
#include <QTextStream>
#include <QObject>
namespace BlackMisc
{
@@ -43,6 +44,27 @@ namespace BlackMisc
return meta;
}
QString className(const QObject *object)
{
if (!object) { return "nullptr"; }
return object->metaObject()->className();
}
QString classNameShort(const QObject *object)
{
if (!object) { return "nullptr"; }
const QString fn = object->metaObject()->className();
if (fn.contains("::"))
{
const int index = fn.lastIndexOf("::");
if (fn.length() > index + 3)
{
return fn.mid(index + 2);
}
}
return fn;
}
#ifdef Q_CC_MSVC
#include <crtdbg.h>

View File

@@ -13,11 +13,12 @@
#include "blackmisc/blackmiscexport.h"
#include <stddef.h>
#include <QString>
#include <stddef.h>
class QMetaType;
class QTextStream;
class QObject;
struct QMetaObject;
//! Metadata util functions
@@ -36,6 +37,12 @@ namespace BlackMisc
//! Real heap size of an object
BLACKMISC_EXPORT size_t heapSizeOf(const QMetaObject &objectType);
//! Class name as from QMetaObject::className with namespace
BLACKMISC_EXPORT QString className(const QObject *object);
//! Class name as from QMetaObject::className without namespace
BLACKMISC_EXPORT QString classNameShort(const QObject *object);
} // ns
#endif // guard