📄 qglobal.cpp
字号:
void myMessageOutput(QtMsgType type, const char *msg) { switch (type) { case QtDebugMsg: fprintf(stderr, "Debug: %s\n", msg); 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;}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);#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 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. \warning Passing (const char *)0 as argument to qDebug might lead to crashes on certain platforms due to the platform's printf() implementation. \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 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 \warning The internal buffer is limited to 8192 bytes, including the '\0'-terminator. \warning Passing (const char *)0 as argument to qWarning might lead to crashes on certain platforms due to the platforms printf implementation. \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 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 \warning The internal buffer is limited to 8192 bytes, including the '\0'-terminator. \warning Passing (const char *)0 as argument to qCritical might lead to crashes on certain platforms due to the platforms printf implementation. \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 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); 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); 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. \warning Passing (const char *)0 as argument to qFatal might lead to crashes on certain platforms due to the platforms printf implementation. \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 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}/*! \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 \relates <QtGlobal> Expands to the size of a pointer in bytes (4 or 8). This is equivalent to \c sizeof(void *) but can be used in a preprocessor directive.*//*! \macro TRUE \relates <QtGlobal> \obsolete Synonym for \c true. \sa FALSE*//*! \macro FALSE \relates <QtGlobal> \obsolete Synonym for \c false. \sa TRUE*//*! \macro QABS(n) \relates <QtGlobal> \obsolete Use qAbs(\a n) instead. \sa QMIN(), QMAX()*//*! \macro QMIN(x, y) \relates <QtGlobal> \obsolete Use qMin(\a x, \a y) instead. \sa QMAX(), QABS()*//*! \macro QMAX(x, y) \relates <QtGlobal> \obsolete Use qMax(\a x, \a y) instead. \sa QMIN(), QABS()*//*! \macro const char *qPrintable(const QString &str) \relates <QtGlobal> Returns \a str as a \c{const char *}. This is equivalent to \a{str}.toLocal8bit().constData(). Example: \code qWarning("%s: %s", qPrintable(key), qPrintable(value)); \endcode \sa qDebug(), qWarning(), qCritical(), qFatal()*//*! \macro Q_DECLARE_TYPEINFO(Type, Flags) \relates <QtGlobal> You can use this macro to specify information about a custom type \a Type. With accurate type information, Qt's \l{generic containers} can choose appropriate storage methods and algorithms. \a Flags can be one of the following: \list \o \c Q_PRIMITIVE_TYPE specifies that \a Type is a POD (plain old data) type with no constructor or destructor. \o \c Q_MOVABLE_TYPE specifies that \a Type has a constructor and/or a destructor but can be moved in memory using \c memcpy(). \o \c Q_COMPLEX_TYPE (the default) specifies that \a Type has constructors and/or a destructor and that it may not be moved in memory. \endlist Example of a "primitive" type: \code struct Point2D { int x; int y; }; Q_DECLARE_TYPEINFO(Point2D, Q_PRIMITIVE_TYPE); \endcode Example of a movable type: \code class Point2D { public: Point2D() { data = new int[2]; } Point2D(const Point2D &other) { ... } ~Point2D() { delete[] data; } Point2D &operator=(const Point2D &other) { ... } int x() const { return data[0]; } int y() const { return data[1]; } private: int *data; }; Q_DECLARE_TYPEINFO(Point2D, Q_MOVABLE_TYPE); \endcode*//*! \macro Q_UNUSED(name) \relates <QtGlobal> Indicates to the compiler that the parameter with the specified \a name is not used in the body of a function. This can be used to suppress compiler warnings while allowing functions to be defined with meaningful parameter names in their signatures.*/#if defined(QT3_SUPPORT) && !defined(QT_NO_SETTINGS)#include <qlibraryinfo.h>static const char *qInstallLocation(QLibraryInfo::LibraryLocation loc){ static QByteArray ret; ret = QLibraryInfo::location(loc).toLatin1(); return ret.constData();}const char *qInstallPath(){ return qInstallLocation(QLibraryInfo::PrefixPath);}const char *qInstallPathDocs(){ return qInstallLocation(QLibraryInfo::DocumentationPath);}const char *qInstallPathHeaders(){ return qInstallLocation(QLibraryInfo::HeadersPath);}const char *qInstallPathLibs(){ return qInstallLocation(QLibraryInfo::LibrariesPath);}const char *qInstallPathBins(){ return qInstallLocation(QLibraryInfo::BinariesPath);}const char *qInstallPathPlugins(){ return qInstallLocation(QLibraryInfo::PluginsPath);}const char *qInstallPathData(){ return qInstallLocation(QLibraryInfo::DataPath);}const char *qInstallPathTranslations(){ return qInstallLocation(QLibraryInfo::TranslationsPath);}const char *qInstallPathSysconf(){ return qInstallLocation(QLibraryInfo::SettingsPath);}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -