QString 轉換爲 const char *
qPrintable
#ifndef qPrintable
# define qPrintable(string) QString(string).toLocal8Bit().constData()
#endif
QString 轉換爲 const wchar_t*
qUtf16Printable
#ifndef qUtf16Printable
# define qUtf16Printable(string) \
static_cast<const wchar_t*>(static_cast<const void*>(QString(string).utf16()))
#endif
消除變量定義未使用的警告
Q_UNUSED
#define Q_UNUSED(x) (void)x;
程序啓動時執行
Q_CONSTRUCTOR_FUNCTION
void constructor()
{
qDebug("constructor");
}
Q_CONSTRUCTOR_FUNCTION(constructor)
程序啓動時輸出
constructor
宏定義
#ifndef Q_CONSTRUCTOR_FUNCTION
# define Q_CONSTRUCTOR_FUNCTION0(AFUNC) \
namespace { \
static const struct AFUNC ## _ctor_class_ { \
inline AFUNC ## _ctor_class_() { AFUNC(); } \
} AFUNC ## _ctor_instance_; \
}
# define Q_CONSTRUCTOR_FUNCTION(AFUNC) Q_CONSTRUCTOR_FUNCTION0(AFUNC)
#endif
展開宏
namespace {
static const struct AFUNC_ctor_class_
{
inline AFUNC_ctor_class_() { AFUNC(); }
}
AFUNC_ctor_instance_;
}
程序退出前執行
Q_DESTRUCTOR_FUNCTION
void destructor()
{
qDebug("destructor");
}
Q_DESTRUCTOR_FUNCTION(destructor)
程序啓動時輸出
destructor
宏定義
#ifndef Q_DESTRUCTOR_FUNCTION
# define Q_DESTRUCTOR_FUNCTION0(AFUNC) \
namespace { \
static const struct AFUNC ## _dtor_class_ { \
inline AFUNC ## _dtor_class_() { } \
inline ~ AFUNC ## _dtor_class_() { AFUNC(); } \
} AFUNC ## _dtor_instance_; \
}
# define Q_DESTRUCTOR_FUNCTION(AFUNC) Q_DESTRUCTOR_FUNCTION0(AFUNC)
#endif
展開宏
namespace {
static const struct AFUNC_dtor_class_ {
inline AFUNC_dtor_class_() { }
inline ~ AFUNC_dtor_class_()
{
AFUNC();
}
} AFUNC_dtor_instance_;
}