📄 qglobal.cpp
字号:
break; case QtWarningMsg: fprintf(stderr, "Warning: %s\n", msg); break; case QtCriticalMsg: fprintf(stderr, "Critical: %s\n", msg); break; case QtFatalMsg: fprintf(stderr, "Fatal: %s\n", msg); abort(); } } int main(int argc, char **argv) { qInstallMsgHandler(myMessageOutput); QApplication app(argc, argv); ... return app.exec(); } \endcode \sa qDebug(), qWarning(), qCritical(), qFatal(), QtMsgType, {Debugging Techniques}*/QtMsgHandler qInstallMsgHandler(QtMsgHandler h){ QtMsgHandler old = handler; handler = h; return old;}/*! \internal*/void qt_message_output(QtMsgType msgType, const char *buf){ if (handler) { (*handler)(msgType, buf); } else {#if defined(Q_CC_MWERKS) mac_default_handler(buf);#elif defined(Q_OS_TEMP) QString fstr(buf); OutputDebugString((fstr + "\n").utf16());#else fprintf(stderr, "%s\n", buf); fflush(stderr);#endif } if (msgType == QtFatalMsg || (msgType == QtWarningMsg && (!qgetenv("QT_FATAL_WARNINGS").isNull())) ) {#if defined(Q_CC_MSVC) && defined(QT_DEBUG) && defined(_DEBUG) && defined(_CRT_ERROR) // get the current report mode int reportMode = _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_WNDW); _CrtSetReportMode(_CRT_ERROR, reportMode); int ret = _CrtDbgReport(_CRT_ERROR, __FILE__, __LINE__, QT_VERSION_STR, buf); if (ret == 0 && reportMode & _CRTDBG_MODE_WNDW) return; // ignore else if (ret == 1) _CrtDbgBreak();#endif#if defined(Q_OS_UNIX) && defined(QT_DEBUG) abort(); // trap; generates core dump#else exit(1); // goodbye cruel world#endif }}#undef qDebug/*! \relates <QtGlobal> Calls the message handler with the debug message \a msg. If no message handler has been installed, the message is printed to stderr. Under Windows, the message is sent to the console, if it is a console application; otherwise, it is sent to the debugger. This function does nothing if \c QT_NO_DEBUG_OUTPUT was defined during compilation. If you pass the function a format string and a list of arguments, it works in similar way to the C printf() function. Example: \code qDebug("Items in list: %d", myList.size()); \endcode If you include \c <QtDebug>, a more convenient syntax is also available: \code qDebug() << "Brush:" << myQBrush << "Other value:" << i; \endcode This syntax automatically puts a single space between each item, and outputs a newline at the end. It supports many C++ and Qt types. \warning The internal buffer is limited to 8192 bytes, including the '\0'-terminator. \sa qWarning(), qCritical(), qFatal(), qInstallMsgHandler(), {Debugging Techniques}*/void qDebug(const char *msg, ...){ char buf[QT_BUFFER_LENGTH]; buf[QT_BUFFER_LENGTH - 1] = '\0'; va_list ap; va_start(ap, msg); // use variable arg list if (msg) qvsnprintf(buf, QT_BUFFER_LENGTH - 1, msg, ap); va_end(ap); qt_message_output(QtDebugMsg, buf);}#undef qWarning/*! \relates <QtGlobal> Calls the message handler with the warning message \a msg. If no message handler has been installed, the message is printed to stderr. Under Windows, the message is sent to the debugger. This function does nothing if \c QT_NO_WARNING_OUTPUT was defined during compilation; it exits if the environment variable \c QT_FATAL_WARNINGS is defined. This function takes a format string and a list of arguments, similar to the C printf() function. Example: \code void f(int c) { if (c > 200) qWarning("f: bad argument, c == %d", c); } \endcode If you include <QtDebug>, a more convenient syntax is also available: \code qWarning() << "Brush:" << myQBrush << "Other value:" << i; \endcode This syntax inserts a space between each item, and appends a newline at the end. \warning The internal buffer is limited to 8192 bytes, including the '\0'-terminator. \sa qDebug(), qCritical(), qFatal(), qInstallMsgHandler(), {Debugging Techniques}*/void qWarning(const char *msg, ...){ char buf[QT_BUFFER_LENGTH]; buf[QT_BUFFER_LENGTH - 1] = '\0'; va_list ap; va_start(ap, msg); // use variable arg list if (msg) qvsnprintf(buf, QT_BUFFER_LENGTH - 1, msg, ap); va_end(ap); qt_message_output(QtWarningMsg, buf);}/*! \relates <QtGlobal> Calls the message handler with the critical message \a msg. If no message handler has been installed, the message is printed to stderr. Under Windows, the message is sent to the debugger. This function takes a format string and a list of arguments, similar to the C printf() function. Example: \code void load(const QString &fileName) { QFile file(fileName); if (!file.exists()) qCritical("File '%s' does not exist!", qPrintable(fileName)); } \endcode If you include <QtDebug>, a more convenient syntax is also available: \code qCritical() << "Brush:" << myQBrush << "Other value:" << i; \endcode A space is inserted between the items, and a newline is appended at the end. \warning The internal buffer is limited to 8192 bytes, including the '\0'-terminator. \sa qDebug(), qWarning(), qFatal(), qInstallMsgHandler(), {Debugging Techniques}*/void qCritical(const char *msg, ...){ char buf[QT_BUFFER_LENGTH]; buf[QT_BUFFER_LENGTH - 1] = '\0'; va_list ap; va_start(ap, msg); // use variable arg list if (msg) qvsnprintf(buf, QT_BUFFER_LENGTH - 1, msg, ap); va_end(ap); qt_message_output(QtCriticalMsg, buf);}#ifdef QT3_SUPPORTvoid qSystemWarning(const char *msg, int code) { qCritical("%s (%s)", msg, qt_error_string(code).toLocal8Bit().constData()); }#endif // QT3_SUPPORTvoid qErrnoWarning(const char *msg, ...){ char buf[QT_BUFFER_LENGTH]; buf[QT_BUFFER_LENGTH - 1] = '\0'; va_list ap; va_start(ap, msg); if (msg) qvsnprintf(buf, QT_BUFFER_LENGTH - 1, msg, ap); va_end(ap); qCritical("%s (%s)", buf, qt_error_string(-1).toLocal8Bit().constData());}void qErrnoWarning(int code, const char *msg, ...){ char buf[QT_BUFFER_LENGTH]; buf[QT_BUFFER_LENGTH - 1] = '\0'; va_list ap; va_start(ap, msg); if (msg) qvsnprintf(buf, QT_BUFFER_LENGTH - 1, msg, ap); va_end(ap); qCritical("%s (%s)", buf, qt_error_string(code).toLocal8Bit().constData());}/*! \relates <QtGlobal> Calls the message handler with the fatal message \a msg. If no message handler has been installed, the message is printed to stderr. Under Windows, the message is sent to the debugger. For a release library this function will exit the application with return value 1. For the debug version this function will abort on Unix systems to create a core dump, and report a _CRT_ERROR on Windows allowing to connect a debugger to the application. This function takes a format string and a list of arguments, similar to the C printf() function. Example: \code int divide(int a, int b) { if (b == 0) // program error qFatal("divide: cannot divide by zero"); return a / b; } \endcode \warning The internal buffer is limited to 8192 bytes, including the '\0'-terminator. \sa qDebug(), qCritical(), qWarning(), qInstallMsgHandler(), {Debugging Techniques}*/void qFatal(const char *msg, ...){ char buf[QT_BUFFER_LENGTH]; buf[QT_BUFFER_LENGTH - 1] = '\0'; va_list ap; va_start(ap, msg); // use variable arg list if (msg) qvsnprintf(buf, QT_BUFFER_LENGTH - 1, msg, ap); va_end(ap); qt_message_output(QtFatalMsg, buf);}// getenv is declared as deprecated in VS2005. This function// makes use of the new secure getenv function.QByteArray qgetenv(const char *varName){#if defined(_MSC_VER) && _MSC_VER >= 1400 size_t requiredSize; QByteArray buffer; getenv_s(&requiredSize, 0, 0, varName); if (requiredSize == 0) return buffer; buffer.resize(int(requiredSize)); getenv_s(&requiredSize, buffer.data(), requiredSize, varName); return buffer;#else return QByteArray(::getenv(varName));#endif}#if defined(Q_OS_UNIX) && !defined(QT_NO_THREAD)# if defined(Q_OS_INTEGRITY)typedef long SeedStorageType;# elsetypedef uint SeedStorageType;# endiftypedef QThreadStorage<SeedStorageType *> SeedStorage;Q_GLOBAL_STATIC(SeedStorage, randTLS) // Thread Local Storage for seed value#endif/*! \relates <QtGlobal> \since 4.2 Thread-safe version of the standard C++ \c srand() function. Sets the argument \a seed to be used to generate a new random number sequence of pseudo random integers to be returned by qrand(). If no seed value is provided, qrand() is automatically seeded with a value of 1. The sequence of random numbers generated is deterministic per thread. For example, if two threads call qsrand(1) and subsequently calls qrand(), the threads will get the same random number sequence. \sa qrand()*/void qsrand(uint seed){#if defined(Q_OS_UNIX) && !defined(QT_NO_THREAD) if (!randTLS()->hasLocalData()) randTLS()->setLocalData(new SeedStorageType); *randTLS()->localData() = seed;#else // On Windows srand() and rand() already use Thread-Local-Storage // to store the seed between calls srand(seed);#endif}/*! \relates <QtGlobal> \since 4.2 Thread-safe version of the standard C++ \c rand() function. Returns a value between 0 and \c RAND_MAX (defined in \c <cstdlib> and \c <stdlib.h>), the next number in the current sequence of pseudo-random integers. Use \c qsrand() to initialize the pseudo-random number generator with a seed value. \sa qsrand()*/int qrand(){#if defined(Q_OS_UNIX) && !defined(QT_NO_THREAD) if (!randTLS()->hasLocalData()) { randTLS()->setLocalData(new SeedStorageType); *randTLS()->localData() = 1; } return rand_r(randTLS()->localData());#else // On Windows srand() and rand() already use Thread-Local-Storage // to store the seed between calls return rand();#endif}/*! \macro forever \relates <QtGlobal> This macro is provided for convenience for writing infinite loops. Example: \code forever { ... } \endcode It is equivalent to \c{for (;;)}. If you're worried about namespace pollution, you can disable this macro by adding the following line to your \c .pro file: \code CONFIG += no_keywords \endcode \sa Q_FOREVER*//*! \macro Q_FOREVER \relates <QtGlobal> Same as \l{forever}. This macro is available even when \c no_keywords is specified using the \c .pro file's \c CONFIG variable. \sa foreach()*//*! \macro foreach(variable, container) \relates <QtGlobal> This macro is used to implement Qt's \c foreach loop. The \a variable parameter is a variable name or variable definition; the \a container parameter is a Qt container whose value type corresponds to the type of the variable. See \l{The foreach Keyword} for details. If you're worried about namespace pollution, you can disable this macro by adding the following line to your \c .pro file: \code CONFIG += no_keywords \endcode \sa Q_FOREACH()*//*! \macro Q_FOREACH(variable, container) \relates <QtGlobal> Same as foreach(\a variable, \a container). This macro is available even when \c no_keywords is specified using the \c .pro file's \c CONFIG variable. \sa foreach()*//*! \macro const char *QT_TR_NOOP(const char *sourceText) \relates <QtGlobal> Marks the string literal \a sourceText for dynamic translation in the current context (class), i.e the stored \a sourceText will not be altered. For example: \code QString FriendlyConversation::greeting(int type) { static const char *greeting_strings[] = { QT_TR_NOOP("Hello"), QT_TR_NOOP("Goodbye") }; return tr(greeting_strings[type]); } \endcode The macro expands to \a sourceText. \sa QT_TRANSLATE_NOOP(), {Internationalization with Qt}*//*! \macro const char *QT_TRANSLATE_NOOP(const char *context, const char *sourceText) \relates <QtGlobal> Marks the string literal \a sourceText for dynamic translation in the given \a context, i.e the stored \a sourceText will not be altered. The \a context is typically a class. For example: \code static const char *greeting_strings[] = { QT_TRANSLATE_NOOP("FriendlyConversation", "Hello"), QT_TRANSLATE_NOOP("FriendlyConversation", "Goodbye") }; QString FriendlyConversation::greeting(int type) { return tr(greeting_strings[type]); } QString global_greeting(int type) { return qApp->translate("FriendlyConversation", greeting_strings[type]); } \endcode The macro expands to \a sourceText. \sa QT_TR_NOOP(), {Internationalization with Qt}*//*! \macro QT_POINTER_SIZE
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -