diff options
Diffstat (limited to 'src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp')
-rw-r--r-- | src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp | 69 |
1 files changed, 12 insertions, 57 deletions
diff --git a/src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp b/src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp index 096e36dc72..25d3abf67e 100644 --- a/src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp @@ -32,17 +32,6 @@ Q_FLAG(Options) typedef QFlags<Enum> Flags; //! [2] - -//! [3] -int myValue = 10; -int minValue = 2; -int maxValue = 6; - -int boundedValue = qBound(minValue, myValue, maxValue); -// boundedValue == 6 -//! [3] - - //! [4] if (!driver()->isOpen() || driver()->isOpenError()) { qWarning("QSqlQuery::exec: database not open"); @@ -61,11 +50,6 @@ quint64 value = Q_UINT64_C(932838457459459); //! [6] -//! [7] -void myMsgHandler(QtMsgType, const char *); -//! [7] - - //! [8] qint64 value = Q_INT64_C(932838457459459); //! [8] @@ -226,37 +210,26 @@ const TInputType &myMin(const TInputType &value1, const TInputType &value2) //! [23] -#include <qapplication.h> +#include <QApplication> #include <stdio.h> #include <stdlib.h> -void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg) +QtMessageHandler originalHandler = nullptr; + +void logToFile(QtMsgType type, const QMessageLogContext &context, const QString &msg) { - QByteArray localMsg = msg.toLocal8Bit(); - const char *file = context.file ? context.file : ""; - const char *function = context.function ? context.function : ""; - switch (type) { - case QtDebugMsg: - fprintf(stderr, "Debug: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function); - break; - case QtInfoMsg: - fprintf(stderr, "Info: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function); - break; - case QtWarningMsg: - fprintf(stderr, "Warning: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function); - break; - case QtCriticalMsg: - fprintf(stderr, "Critical: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function); - break; - case QtFatalMsg: - fprintf(stderr, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function); - break; - } + QString message = qFormatLogMessage(type, context, msg); + static FILE *f = fopen("log.txt", "a"); + fprintf(f, "%s\n", qPrintable(message)); + fflush(f); + + if (originalHandler) + *originalHandler(type, context, msg); } int main(int argc, char **argv) { - qInstallMessageHandler(myMessageOutput); + originalHandler = qInstallMessageHandler(logToFile); QApplication app(argc, argv); ... return app.exec(); @@ -579,11 +552,6 @@ qFuzzyCompare(0.0, 1.0e-200); // This will return false qFuzzyCompare(1 + 0.0, 1 + 1.0e-200); // This will return true //! [46] -//! [47] -CApaApplication *myApplicationFactory(); -//! [47] - - //! [49] void myMessageHandler(QtMsgType, const QMessageLogContext &, const QString &); //! [49] @@ -667,19 +635,6 @@ bool readConfiguration(const QFile &file) } //! [qunreachable-switch] -//! [qunreachable-return] - switch (shape) { - case Rectangle: - return rectangle(); - case Triangle: - return triangle(); - case Circle: - return circle(); - case NumShapes: - Q_UNREACHABLE_RETURN(nullptr); - } -//! [qunreachable-return] - //! [qt-version-check] #include <QtGlobal> |