1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the core module of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
29
#include "qplatformdefs.h"
39
#if defined(Q_CC_MSVC) && !defined(Q_OS_TEMP)
45
\brief The QFlag class is a helper data type for QFlags.
47
It is equivalent to a plain \c int, except with respect to
48
function overloading and type conversions. You should never need
49
to use it in your applications.
55
\fn QFlag::QFlag(int value)
57
Constructs a QFlag object that stores the given \a value.
61
\fn QFlag::operator int() const
63
Returns the value stored by the QFlag object.
68
\brief The QFlags class provides a type-safe way of storing
69
OR-combinations of enum values.
74
The QFlags<Enum> class is a template class, where Enum is an enum
75
type. QFlags is used throughout Qt for storing combinations of
78
The traditional C++ approach for storing OR-combinations of enum
79
values is to use a \c int or \c uint variable. The inconvenient
80
with that approach is that there's no type checking at all; any
81
enum value can be OR'd with any other enum value and passed on to
82
a function that takes a \c int or \c uint.
84
Qt uses QFlags to provide type safety. For example, the
85
Qt::Alignment type is simply a typedef for
86
QFlags<Qt::AlignmentFlag>. QLabel::setAlignment() takes a
87
Qt::Alignment parameter, which means that any combination of
88
Qt::AlignmentFlag values is legal:
91
label->setAlignment(Qt::AlignLeft | Qt::AlignTop);
94
If you try to pass a value from another enum, the compiler will
97
If you want to use QFlags for your own enum types, you must use
98
the Q_DECLARE_FLAGS() and Q_DECLARE_OPERATORS_FOR_FLAGS().
111
Q_DECLARE_FLAGS(Options, Option)
115
Q_DECLARE_OPERATORS_FOR_FLAGS(MyClass::Options)
118
You can then use the \c MyClass::Options type to store
119
combinations of \c MyClass::Option values.
121
A sensible naming convension for enum types and associated QFlags
122
types is to give a singular name to the enum type (e.g., \c
123
Option) and a plural name to the QFlags type (e.g., \c Options).
124
When a singular name is desired for the QFlags type (e.g., \c
125
Alignment), you can use \c Flag as the suffix for the enum type
126
(e.g., \c AlignmentFlag).
132
\typedef QFlags::enum_type
134
Typedef for the Enum template type.
138
\fn QFlags::QFlags(const QFlags &other)
140
Constructs a copy of \a other.
144
\fn QFlags::QFlags(Enum flag)
146
Constructs a QFlags object storing the given \a flag.
150
\fn QFlags::QFlags(Zero zero)
152
Constructs a QFlags object with no flags set. \a zero must be a
157
\fn QFlags::QFlags(QFlag value)
159
Constructs a QFlags object initialized with the given integer \a
162
The QFlag type is a helper type. By using it here instead of \c
163
int, we effectively ensure that arbitrary enum values cannot be
164
cast to a QFlags, whereas untyped enum values (i.e., \c int
169
\fn QFlags &QFlags::operator=(const QFlags &other)
171
Assigns \a other to this object and returns a reference to this
176
\fn QFlags &QFlags::operator&=(int mask)
178
Performs a bitwise AND operation with \a mask and stores the
179
result in this QFlags object. Returns a reference to this object.
181
\sa operator&(), operator|=(), operator^=()
185
\fn QFlags &QFlags::operator&=(uint mask)
191
\fn QFlags &QFlags::operator|=(QFlags other)
193
Performs a bitwise OR operation with \a other and stores the
194
result in this QFlags object. Returns a reference to this object.
196
\sa operator|(), operator&=(), operator^=()
200
\fn QFlags &QFlags::operator|=(Enum other)
206
\fn QFlags &QFlags::operator^=(QFlags other)
208
Performs a bitwise XOR operation with \a other and stores the
209
result in this QFlags object. Returns a reference to this object.
211
\sa operator^(), operator&=(), operator|=()
215
\fn QFlags &QFlags::operator^=(Enum other)
221
\fn QFlags::operator int() const
223
Returns the value stored in the QFlags object as an integer.
227
\fn QFlags QFlags::operator|(QFlags other) const
229
Returns a QFlags object containing the result of the bitwise OR
230
operation on this object and \a other.
232
\sa operator|=(), operator^(), operator&(), operator~()
236
\fn QFlags QFlags::operator|(Enum other) const
242
\fn QFlags QFlags::operator^(QFlags other) const
244
Returns a QFlags object containing the result of the bitwise XOR
245
operation on this object and \a other.
247
\sa operator^=(), operator&(), operator|(), operator~()
251
\fn QFlags QFlags::operator^(Enum other) const
257
\fn QFlags QFlags::operator&(int mask) const
259
Returns a QFlags object containing the result of the bitwise AND
260
operation on this object and \a mask.
262
\sa operator&=(), operator|(), operator^(), operator~()
266
\fn QFlags QFlags::operator&(uint mask) const
272
\fn QFlags QFlags::operator&(Enum mask) const
278
\fn QFlags QFlags::operator~() const
280
Returns a QFlags object that contains the bitwise negation of
283
\sa operator&(), operator|(), operator^()
287
\fn bool QFlags::operator!() const
289
Returns true if no flag is set (i.e., if the value stored by the
290
QFlags object is 0); otherwise returns false.
294
\macro Q_DECLARE_FLAGS(Flags, Enum)
297
The Q_DECLARE_FLAGS() macro expands to
300
typedef QFlags<Enum> Flags;
303
\a Enum is the name of an existing enum type, whereas \a Flags is
304
the name of the QFlags<\e{Enum}> typedef.
306
See the QFlags documentation for details.
308
\sa Q_DECLARE_OPERATORS_FOR_FLAGS()
312
\macro Q_DECLARE_OPERATORS_FOR_FLAGS(Flags)
315
The Q_DECLARE_OPERATORS_FOR_FLAGS() macro declares global \c
316
operator|() functions for \a Flags, which is of type QFlags<T>.
318
See the QFlags documentation for details.
320
\sa Q_DECLARE_FLAGS()
324
\headerfile <QtGlobal>
325
\title Global Qt Declarations
327
\brief The <QtGlobal> header file provides basic declarations and is included by all other Qt headers.
336
Typedef for \c double.
342
Convenience typedef for \c{unsigned char}.
348
Convenience typedef for \c{unsigned short}.
354
Convenience typedef for \c{unsigned int}.
360
Convenience typedef for \c{unsigned long}.
366
Typedef for \c{signed char}. This type is guaranteed to be 8-bit
367
on all platforms supported by Qt.
373
Typedef for \c{unsigned signed char}. This type is guaranteed to
374
be 8-bit on all platforms supported by Qt.
380
Typedef for \c{signed short}. This type is guaranteed to be
381
16-bit on all platforms supported by Qt.
387
Typedef for \c{unsigned signed short}. This type is guaranteed to
388
be 16-bit on all platforms supported by Qt.
394
Typedef for \c{signed int}. This type is guaranteed to be 32-bit
395
on all platforms supported by Qt.
401
Typedef for \c{unsigned signed int}. This type is guaranteed to
402
be 32-bit on all platforms supported by Qt.
408
Typedef for \c{long long int} (\c __int64 on Windows). This type
409
is guaranteed to be 64-bit on all platforms supported by Qt.
411
Literals of that type can be created using the Q_INT64_C() macro:
414
qint64 value = Q_INT64_C(932838457459459);
417
\sa Q_INT64_C(), quint64
423
Typedef for \c{unsigned long long int} (\c{unsigned __int64} on
424
Windows). This type is guaranteed to be 64-bit on all platforms
427
Literals of that type can be created using the Q_UINT64_C()
431
quint64 value = Q_UINT64_C(932838457459459);
434
\sa Q_UINT64_C(), qint64
437
/*! \macro qint64 Q_INT64_C(literal)
440
Wraps the signed 64-bit integer \a literal in a
441
platform-independent way. For example:
444
qint64 value = Q_INT64_C(932838457459459);
447
\sa qint64, Q_UINT64_C()
450
/*! \macro quint64 Q_UINT64_C(literal)
453
Wraps the unsigned 64-bit integer \a literal in a
454
platform-independent way. For example:
457
quint64 value = Q_UINT64_C(932838457459459);
460
\sa quint64, Q_INT64_C()
463
/*! \typedef qlonglong
466
Typedef for \c{long long int} (\c __int64 on Windows). This is
467
the same as \l qint64.
469
\sa Q_INT64_C(), qulonglong
472
/*! \typedef qulonglong
475
Typedef for \c{unsigned long long int} (\c{unsigned __int64} on
476
Windows). This is the same as \l quint64.
478
\sa Q_UINT64_C(), qlonglong
481
/*! \fn const T &qAbs(const T &value)
484
Returns the absolute value of \a value.
487
/*! \fn int qRound(double value)
490
Rounds \a value up to the nearest integer.
493
/*! \fn qint64 qRound64(double value)
496
Rounds \a value up to the nearest 64-bit integer.
499
/*! \fn const T &qMin(const T &value1, const T &value2)
502
Returns the minimum of \a value1 and \a value2.
507
/*! \fn const T &qMax(const T &value1, const T &value2)
510
Returns the maximum of \a value1 and \a value2.
515
/*! \fn const T &qBound(const T &min, const T &value, const T &max)
518
Returns \a value bounded by \a min and \a max. This is equivalent
519
to qMax(\a min, qMin(\a value, \a max)).
529
Use \l qint8 instead.
537
Use \l quint8 instead.
545
Use \l qint16 instead.
553
Use \l quint16 instead.
561
Use \l qint32 instead.
569
Use \l quint32 instead.
577
Use \l qint64 instead.
585
Use \l quint64 instead.
593
Use \l qint64 instead.
601
Use \l quint64 instead.
609
Use \c{void *} instead.
617
Use \c{void *} instead.
620
/*! \fn bool qSysInfo(int *wordSize, bool *bigEndian)
623
Use QSysInfo::WordSize and QSysInfo::ByteOrder instead.
627
\fn bool qt_winUnicode()
630
Use !(QSysInfo::WindowsVersion & QSysInfo::WV_DOS_based) instead.
636
\fn int qWinVersion()
639
Use QSysInfo::WindowsVersion instead.
645
\fn int qMacVersion()
648
Use QSysInfo::MacintoshVersion instead.
656
Returns the Qt version number as a string, for example, "4.0.1" or
659
The \c QT_VERSION define has the numeric value in the form:
660
0xMMNNPP (MM = major, NN = minor, PP = patch). For example, Qt
661
4.0.1's \c QT_VERSION is 0x040001.
664
const char *qVersion()
666
return QT_VERSION_STR;
678
/*****************************************************************************
679
System detection routines
680
*****************************************************************************/
684
\brief The QSysInfo class provides information about the system.
687
\o \l WordSize specifies the size of a pointer for the platform
688
on which the application is compiled.
689
\o \l ByteOrder specifies whether the platform is big-endian or
691
\o \l WindowsVersion specifies the version of the Windows operating
692
system on which the application is run (Windows only)
693
\o \l MacintoshVersion specifies the version of the Macintosh
694
operating system on which the application is run (Mac only).
697
Some constants are defined only on certain platforms. You can use
698
the preprocessor symbols \c Q_WS_WIN and \c Q_WS_MAC to test that
699
the application is compiled under Windows or Mac.
705
\variable QSysInfo::WordSize
706
\brief the size in bits of a pointer for the platform on which
707
the application is compiled (32 or 64)
711
\variable QSysInfo::WindowsVersion
712
\brief the version of the Windows operating system on which the
713
application is run (Windows only)
717
\variable QSysInfo::MacintoshVersion
718
\brief the version of the Macintosh operating system on which
719
the application is run (Mac only).
723
\enum QSysInfo::Endian
725
\value BigEndian Big-endian byte order (also called Network byte order)
726
\value LittleEndian Little-endian byte order
727
\value ByteOrder Equals BigEndian or LittleEndian, depending on
728
the platform's byte order.
732
\enum QSysInfo::WinVersion
734
This enum provides symbolic names for the various versions of the
735
Windows operating system. On Windows, the
736
QSysInfo::WindowsVersion variable gives the version of the system
737
on which the application is run.
739
MS-DOS-based versions:
741
\value WV_32s Windows 3.1 wth Win 32s
742
\value WV_95 Windows 95
743
\value WV_98 Windows 98
744
\value WV_Me Windows Me
748
\value WV_NT Windows NT
749
\value WV_2000 Windows 2000
750
\value WV_XP Windows XP
751
\value WV_2003 Windows XP 2003
755
\value WV_CE Windows CE
756
\value WV_CENET Windows CE .NET
758
The following masks can be used for testing whether a Windows
759
version is MS-DOS-based, NT-based, or CE-based:
761
\value WV_DOS_based MS-DOS-based version of Windows
762
\value WV_NT_based NT-based version of Windows
763
\value WV_CE_based CE-based version of Windows
769
\enum QSysInfo::MacVersion
771
This enum provides symbolic names for the various versions of the
772
Macintosh operating system. On Mac, the
773
QSysInfo::MacintoshVersion variable gives the version of the
774
system on which the application is run.
776
\value MV_9 MacOS 9 (unsupported)
777
\value MV_10_0 Mac OS X 10.0
778
\value MV_10_1 Mac OS X 10.1
779
\value MV_10_2 Mac OS X 10.2
780
\value MV_10_3 Mac OS X 10.3
781
\value MV_10_4 Mac OS X 10.4
782
\value MV_Unknown Other
787
#if !defined(Q_BYTE_ORDER) && defined(QT_BUILD_QMAKE)
788
// needed to bootstrap qmake
789
static const unsigned int qt_one = 1;
790
const int QSysInfo::ByteOrder = ((*((unsigned char *) &qt_one) == 0) ? BigEndian : LittleEndian);
793
#if !defined(QWS) && defined(Q_OS_MAC)
795
#include <private/qcore_mac_p.h>
796
#include "qnamespace.h"
798
// This function has descended from Apple Source Code (FSpLocationFromFullPath),
799
// but changes have been made. [Creates a minimal alias from the full pathname]
800
Q_CORE_EXPORT OSErr qt_mac_create_fsspec(const QString &file, FSSpec *spec)
803
QByteArray utfs = file.toUtf8();
804
OSErr ret = FSPathMakeRef((const UInt8 *)utfs.data(), &fref, NULL);
806
ret = FSGetCatalogInfo(&fref, kFSCatInfoNone, NULL, NULL, spec, NULL);
810
Q_CORE_EXPORT void qt_mac_to_pascal_string(QString s, Str255 str, TextEncoding encoding=0, int len=-1)
815
UnicodeMapping mapping;
816
mapping.unicodeEncoding = CreateTextEncoding(kTextEncodingUnicodeDefault,
817
kTextEncodingDefaultVariant,
818
kUnicode16BitFormat);
819
mapping.otherEncoding = (encoding ? encoding : );
820
mapping.mappingVersion = kUnicodeUseLatestMapping;
822
UnicodeToTextInfo info;
823
OSStatus err = CreateUnicodeToTextInfo(&mapping, &info);
825
qDebug("Qt: internal: Unable to create pascal string '%s'::%d [%ld]",
826
s.left(len).latin1(), (int)encoding, err);
829
const int unilen = len * 2;
830
const UniChar *unibuf = (UniChar *)s.unicode();
831
ConvertFromUnicodeToPString(info, unilen, unibuf, str);
832
DisposeUnicodeToTextInfo(&info);
835
CFStringGetPascalString(QCFString(s), str, 256, CFStringGetSystemEncoding());
839
Q_CORE_EXPORT QString qt_mac_from_pascal_string(const Str255 pstr) {
840
return QCFString(CFStringCreateWithPascalString(0, pstr, CFStringGetSystemEncoding()));
843
static QSysInfo::MacVersion macVersion()
845
long gestalt_version;
846
if (Gestalt(gestaltSystemVersion, &gestalt_version) == noErr) {
847
if (gestalt_version >= 0x1040 && gestalt_version < 0x1050)
848
return QSysInfo::MV_10_4;
849
else if (gestalt_version >= 0x1030 && gestalt_version < 0x1040)
850
return QSysInfo::MV_10_3;
851
else if (gestalt_version >= 0x1020 && gestalt_version < 0x1030)
852
return QSysInfo::MV_10_2;
853
else if (gestalt_version >= 0x1010 && gestalt_version < 0x1020)
854
return QSysInfo::MV_10_1;
855
else if (gestalt_version >= 0x1000 && gestalt_version < 0x1010)
856
return QSysInfo::MV_10_0;
858
return QSysInfo::MV_Unknown;
860
const QSysInfo::MacVersion QSysInfo::MacintoshVersion = macVersion();
861
#elif defined(Q_OS_WIN32) || defined(Q_OS_CYGWIN) || defined(Q_OS_TEMP)
863
#include "qt_windows.h"
865
static QSysInfo::WinVersion winVersion()
867
#ifndef VER_PLATFORM_WIN32s
868
#define VER_PLATFORM_WIN32s 0
870
#ifndef VER_PLATFORM_WIN32_WINDOWS
871
#define VER_PLATFORM_WIN32_WINDOWS 1
873
#ifndef VER_PLATFORM_WIN32_NT
874
#define VER_PLATFORM_WIN32_NT 2
876
#ifndef VER_PLATFORM_WIN32_CE
877
#define VER_PLATFORM_WIN32_CE 3
880
static QSysInfo::WinVersion winver = QSysInfo::WV_NT;
882
OSVERSIONINFOA osver;
883
osver.dwOSVersionInfoSize = sizeof(osver);
884
GetVersionExA(&osver);
887
OSVERSIONINFOW osver;
888
osver.dwOSVersionInfoSize = sizeof(osver);
889
GetVersionEx(&osver);
890
qt_cever = osver.dwMajorVersion * 100;
891
qt_cever += osver.dwMinorVersion * 10;
893
switch (osver.dwPlatformId) {
894
case VER_PLATFORM_WIN32s:
895
winver = QSysInfo::WV_32s;
897
case VER_PLATFORM_WIN32_WINDOWS:
898
// We treat Windows Me (minor 90) the same as Windows 98
899
if (osver.dwMinorVersion == 90)
900
winver = QSysInfo::WV_Me;
901
else if (osver.dwMinorVersion == 10)
902
winver = QSysInfo::WV_98;
904
winver = QSysInfo::WV_95;
907
case VER_PLATFORM_WIN32_CE:
910
winver = QSysInfo::WV_CENET;
913
winver = QSysInfo::WV_CE;
916
default: // VER_PLATFORM_WIN32_NT
917
if (osver.dwMajorVersion < 5) {
918
winver = QSysInfo::WV_NT;
919
} else if (osver.dwMinorVersion == 0) {
920
winver = QSysInfo::WV_2000;
921
} else if (osver.dwMinorVersion == 1) {
922
winver = QSysInfo::WV_XP;
923
} else if (osver.dwMinorVersion == 2) {
924
winver = QSysInfo::WV_2003;
926
qWarning("Untested Windows version detected!");
927
winver = QSysInfo::WV_NT_based;
934
const QSysInfo::WinVersion QSysInfo::WindowsVersion = winVersion();
940
\macro void Q_ASSERT(bool test)
943
Prints a warning message containing the source code file name and
944
line number if \a test is false.
946
Q_ASSERT() is useful for testing pre- and post-conditions
947
during development. It does nothing if \c QT_NO_DEBUG was defined
958
int divide(int a, int b)
965
If \c b is zero, the Q_ASSERT statement will output the following
966
message using the qFatal() function:
969
ASSERT: "b == 0" in file div.cpp, line 9
972
\sa Q_ASSERT_X(), qFatal(), {Debugging Techniques}
976
\macro void Q_ASSERT_X(bool test, const char *where, const char *what)
979
Prints the message \a what together with the location \a where,
980
the source file name and line number if \a test is false.
982
Q_ASSERT_X is useful for testing pre- and post-conditions during
983
development. It does nothing if \c QT_NO_DEBUG was defined during
994
int divide(int a, int b)
996
Q_ASSERT_X(b != 0, "divide", "division by zero");
1001
If \c b is zero, the Q_ASSERT_X statement will output the following
1002
message using the qFatal() function:
1005
ASSERT failure in divide: "division by zero", file div.cpp, line 9
1008
\sa Q_ASSERT(), qFatal(), {Debugging Techniques}
1012
\macro void Q_CHECK_PTR(void *p)
1015
If \a p is 0, prints a warning message containing the source code file
1016
name and line number, saying that the program ran out of memory.
1018
Q_CHECK_PTR does nothing if \c QT_NO_DEBUG was defined during
1025
Q_CHECK_PTR(a = new int[80]); // WRONG!
1027
a = new (nothrow) int[80]; // Right
1031
\sa qWarning(), {Debugging Techniques}
1036
The Q_CHECK_PTR macro calls this function if an allocation check
1039
void qt_check_pointer(const char *n, int l)
1041
qWarning("In file %s, line %d: Out of memory", n, l);
1045
The Q_ASSERT macro calls this this function when the test fails.
1047
void qt_assert(const char *assertion, const char *file, int line)
1049
qFatal("ASSERT: \"%s\" in file %s, line %d", assertion, file, line);
1053
The Q_ASSERT_X macro calls this this function when the test fails.
1055
void qt_assert_x(const char *where, const char *what, const char *file, int line)
1057
qFatal("ASSERT failure in %s: \"%s\", file %s, line %d", where, what, file, line);
1062
Dijkstra's bisection algorithm to find the square root of an integer.
1063
Deliberately not exported as part of the Qt API, but used in both
1064
qsimplerichtext.cpp and qgfxraster_qws.cpp
1066
Q_CORE_EXPORT unsigned int qt_int_sqrt(unsigned int n)
1068
// n must be in the range 0...UINT_MAX/2-1
1069
if (n >= (UINT_MAX>>2)) {
1070
unsigned int r = 2 * qt_int_sqrt(n / 4);
1071
unsigned int r2 = r + 1;
1072
return (n >= r2 * r2) ? r2 : r;
1074
uint h, p= 0, q= 1, r= n;
1089
#if defined(qMemCopy)
1092
#if defined(qMemSet)
1096
void *qMalloc(size_t size) { return ::malloc(size); }
1097
void qFree(void *ptr) { ::free(ptr); }
1098
void *qRealloc(void *ptr, size_t size) { return ::realloc(ptr, size); }
1099
void *qMemCopy(void *dest, const void *src, size_t n) { return memcpy(dest, src, n); }
1100
void *qMemSet(void *dest, int c, size_t n) { return memset(dest, c, n); }
1103
static QtMsgHandler handler = 0; // pointer to debug handler
1104
static const int QT_BUFFER_LENGTH = 8192; // internal buffer length
1107
#include <CoreServices/CoreServices.h>
1108
extern bool qt_is_gui_used;
1109
static void mac_default_handler(const char *msg)
1111
if (qt_is_gui_used) {
1113
qt_mac_to_pascal_string(msg, pmsg);
1116
fprintf(stderr, msg);
1119
#endif // Q_CC_MWERKS
1122
QString qt_error_string(int errorCode)
1126
if (errorCode == -1) {
1127
#if defined(Q_OS_WIN32)
1128
errorCode = GetLastError();
1133
switch (errorCode) {
1137
s = QT_TRANSLATE_NOOP("QIODevice", "Permission denied");
1140
s = QT_TRANSLATE_NOOP("QIODevice", "Too many open files");
1143
s = QT_TRANSLATE_NOOP("QIODevice", "No such file or directory");
1146
s = QT_TRANSLATE_NOOP("QIODevice", "No space left on device");
1151
unsigned short *string = 0;
1152
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
1155
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
1159
ret = QString::fromUtf16(string);
1160
LocalFree((HLOCAL)string);
1163
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
1166
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
1170
ret = QString::fromLocal8Bit(string);
1171
LocalFree((HLOCAL)string);
1174
ret = QString::fromLocal8Bit(strerror(errorCode));
1179
// ######## this breaks moc build currently
1180
// ret = QCoreApplication::translate("QIODevice", s);
1181
ret = QString::fromLatin1(s);
1182
return ret.trimmed();
1188
Installs a Qt message handler \a h. Returns a pointer to the
1189
message handler previously defined.
1191
The message handler is a function that prints out debug messages,
1192
warnings and fatal error messages. The Qt library (debug version)
1193
contains hundreds of warning messages that are printed when
1194
internal errors (usually invalid function arguments) occur. If you
1195
implement your own message handler, you get total control of these
1198
The default message handler prints the message to the standard
1199
output under X11 or to the debugger under Windows. If it is a
1200
fatal message, the application aborts immediately.
1202
Only one message handler can be defined, since this is usually
1203
done on an application-wide basis to control debug output.
1205
To restore the message handler, call \c qInstallMsgHandler(0).
1209
#include <qapplication.h>
1213
void myMessageOutput(QtMsgType type, const char *msg)
1217
fprintf(stderr, "Debug: %s\n", msg);
1220
fprintf(stderr, "Warning: %s\n", msg);
1223
fprintf(stderr, "Critical: %s\n", msg);
1226
fprintf(stderr, "Fatal: %s\n", msg);
1227
abort(); // deliberately core dump
1231
int main(int argc, char **argv)
1233
qInstallMsgHandler(myMessageOutput);
1234
QApplication a(argc, argv);
1240
\sa qDebug(), qWarning(), qFatal(), {Debugging Techniques}
1242
QtMsgHandler qInstallMsgHandler(QtMsgHandler h)
1244
QtMsgHandler old = handler;
1250
void qt_message_output(QtMsgType msgType, const char *buf)
1253
(*handler)(msgType, buf);
1255
#if defined(Q_CC_MWERKS)
1256
mac_default_handler(buf);
1257
#elif defined(Q_OS_TEMP)
1259
OutputDebugString((fstr + "\n").utf16());
1261
fprintf(stderr, "%s\n", buf);
1265
if (msgType == QtFatalMsg
1266
|| (msgType == QtWarningMsg
1267
&& (!qgetenv("QT_FATAL_WARNINGS").isNull())) ) {
1269
#if defined(Q_CC_MSVC) && defined(QT_DEBUG) && defined(_DEBUG) && defined(_CRT_ERROR)
1270
// get the current report mode
1271
int reportMode = _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_WNDW);
1272
_CrtSetReportMode(_CRT_ERROR, reportMode);
1273
int ret = _CrtDbgReport(_CRT_ERROR, __FILE__, __LINE__, QT_VERSION_STR, buf);
1274
if (ret == 0 && reportMode & _CRTDBG_MODE_WNDW)
1280
#if defined(Q_OS_UNIX) && defined(QT_DEBUG)
1281
abort(); // trap; generates core dump
1283
exit(1); // goodbye cruel world
1292
Calls the message handler with the debug message \a msg. If no
1293
message handler has been installed, the message is printed to
1294
stderr. Under Windows, the message is sent to the debugger. This
1295
function does nothing if \c QT_NO_DEBUG_OUTPUT was defined during
1298
If you pass the function a format string and a list of arguments,
1299
it works in similar way to the C printf() function.
1303
qDebug("my window handle = %x", myWidget->id());
1306
A more convenient syntax is also available:
1308
qDebug() << "Brush:" << myQBrush << "Other value:" << i;
1310
This syntax automatically puts a single space between each item,
1311
and outputs a newline at the end. It supports many C++ and Qt
1314
\warning The internal buffer is limited to 8192 bytes, including
1315
the '\0'-terminator.
1317
\warning Passing (const char *)0 as argument to qDebug might lead
1318
to crashes on certain platforms due to the platform's printf() implementation.
1320
\sa qWarning(), qCritical(), qFatal(), qInstallMsgHandler(),
1321
{Debugging Techniques}
1323
void qDebug(const char *msg, ...)
1325
char buf[QT_BUFFER_LENGTH];
1326
buf[QT_BUFFER_LENGTH - 1] = '\0';
1328
va_start(ap, msg); // use variable arg list
1329
qvsnprintf(buf, QT_BUFFER_LENGTH - 1, msg, ap);
1332
qt_message_output(QtDebugMsg, buf);
1339
Calls the message handler with the warning message \a msg. If no
1340
message handler has been installed, the message is printed to
1341
stderr. Under Windows, the message is sent to the debugger. This
1342
function does nothing if \c QT_NO_WARNING_OUTPUT was defined
1343
during compilation; it exits if the environment variable \c
1344
QT_FATAL_WARNINGS is defined.
1346
This function takes a format string and a list of arguments,
1347
similar to the C printf() function.
1354
qWarning("f: bad argument, c == %d", c);
1358
\warning The internal buffer is limited to 8192 bytes, including
1359
the '\0'-terminator.
1361
\warning Passing (const char *)0 as argument to qWarning might lead
1362
to crashes on certain platforms due to the platforms printf implementation.
1364
\sa qDebug(), qCritical(), qFatal(), qInstallMsgHandler(),
1365
{Debugging Techniques}
1367
void qWarning(const char *msg, ...)
1369
char buf[QT_BUFFER_LENGTH];
1370
buf[QT_BUFFER_LENGTH - 1] = '\0';
1372
va_start(ap, msg); // use variable arg list
1373
qvsnprintf(buf, QT_BUFFER_LENGTH - 1, msg, ap);
1376
qt_message_output(QtWarningMsg, buf);
1382
Calls the message handler with the critical message \a msg. If no
1383
message handler has been installed, the message is printed to
1384
stderr. Under Windows, the message is sent to the debugger.
1386
This function takes a format string and a list of arguments, similar
1387
to the C printf() function.
1391
void load(const QString &filename)
1393
QFile file(filename);
1395
qCritical("file '%s' does not exist!", filename.local8bit());
1399
\warning The internal buffer is limited to 8192 bytes, including
1400
the '\0'-terminator.
1402
\warning Passing (const char *)0 as argument to qCritical might
1403
lead to crashes on certain platforms due to the platforms printf
1406
\sa qDebug(), qWarning(), qFatal(), qInstallMsgHandler(),
1407
{Debugging Techniques}
1409
void qCritical(const char *msg, ...)
1411
char buf[QT_BUFFER_LENGTH];
1412
buf[QT_BUFFER_LENGTH - 1] = '\0';
1414
va_start(ap, msg); // use variable arg list
1415
qvsnprintf(buf, QT_BUFFER_LENGTH - 1, msg, ap);
1418
qt_message_output(QtCriticalMsg, buf);
1421
void qSystemWarning(const char *msg, int code)
1422
{ qCritical("%s (%s)", msg, qt_error_string(code).toLocal8Bit().constData()); }
1423
#endif // QT3_SUPPORT
1425
void qErrnoWarning(const char *msg, ...)
1427
char buf[QT_BUFFER_LENGTH];
1428
buf[QT_BUFFER_LENGTH - 1] = '\0';
1431
qvsnprintf(buf, QT_BUFFER_LENGTH - 1, msg, ap);
1434
qCritical("%s (%s)", buf, qt_error_string(-1).toLocal8Bit().constData());
1437
void qErrnoWarning(int code, const char *msg, ...)
1439
char buf[QT_BUFFER_LENGTH];
1440
buf[QT_BUFFER_LENGTH - 1] = '\0';
1443
qvsnprintf(buf, QT_BUFFER_LENGTH - 1, msg, ap);
1446
qCritical("%s (%s)", buf, qt_error_string(code).toLocal8Bit().constData());
1452
Calls the message handler with the fatal message \a msg. If no
1453
message handler has been installed, the message is printed to
1454
stderr. Under Windows, the message is sent to the debugger.
1456
For a release library this function will exit the application
1457
with return value 1. For the debug version this function will
1458
abort on Unix systems to create a core dump, and report a
1459
_CRT_ERROR on Windows allowing to connect a debugger to the
1462
This function takes a format string and a list of arguments,
1463
similar to the C printf() function.
1467
int divide(int a, int b)
1469
if (b == 0) // program error
1470
qFatal("divide: cannot divide by zero");
1475
\warning The internal buffer is limited to 8192 bytes, including
1476
the '\0'-terminator.
1478
\warning Passing (const char *)0 as argument to qFatal might lead
1479
to crashes on certain platforms due to the platforms printf implementation.
1481
\sa qDebug(), qCritical(), qWarning(), qInstallMsgHandler(),
1482
{Debugging Techniques}
1484
void qFatal(const char *msg, ...)
1486
char buf[QT_BUFFER_LENGTH];
1487
buf[QT_BUFFER_LENGTH - 1] = '\0';
1489
va_start(ap, msg); // use variable arg list
1490
qvsnprintf(buf, QT_BUFFER_LENGTH - 1, msg, ap);
1493
qt_message_output(QtFatalMsg, buf);
1496
// getenv is declared as deprecated in VS2005. This function
1497
// makes use of the new secure getenv function.
1498
QByteArray qgetenv(const char *varName)
1500
#if defined(_MSC_VER) && _MSC_VER >= 1400
1501
size_t requiredSize;
1503
getenv_s(&requiredSize, 0, 0, varName);
1504
if (requiredSize == 0)
1506
buffer.resize(requiredSize);
1507
getenv_s(&requiredSize, buffer.data(), requiredSize, varName);
1510
return QByteArray(::getenv(varName));
1515
\macro foreach(variable, container)
1518
This macro is used to implement Qt's \c foreach loop. \a variable
1519
is a variable name or variable definition; \a container is a Qt
1520
container whose value type corresponds to the type of the
1521
variable. See \l{The foreach Keyword} for details.
1523
If you're worried about namespace pollution, you can disable this
1524
macro by adding the following line to your \c .pro file:
1527
CONFIG += no_keywords
1534
\macro Q_FOREACH(variable, container)
1537
Same as foreach(\a variable, \a container).
1541
\macro const char *QT_TR_NOOP(const char *sourceText)
1544
Marks the string literal \a sourceText for translation in the
1545
current context. Expands to \a sourceText.
1550
QString FriendlyConversation::greeting(int type)
1552
static const char *greeting_strings[] = {
1553
QT_TR_NOOP("Hello"),
1554
QT_TR_NOOP("Goodbye")
1556
return tr(greeting_strings[type]);
1560
\sa QT_TRANSLATE_NOOP(), {Internationalization with Qt}
1564
\macro const char *QT_TRANSLATE_NOOP(const char *context, const char *sourceText)
1567
Marks the string literal \a sourceText for translation in the
1568
given \a context. Expands to \a sourceText.
1573
static const char *greeting_strings[] = {
1574
QT_TRANSLATE_NOOP("FriendlyConversation", "Hello"),
1575
QT_TRANSLATE_NOOP("FriendlyConversation", "Goodbye")
1578
QString FriendlyConversation::greeting(int type)
1580
return tr(greeting_strings[type]);
1583
QString global_greeting(int type)
1585
return qApp->translate("FriendlyConversation",
1586
greeting_strings[type]);
1590
\sa QT_TR_NOOP(), {Internationalization with Qt}
1594
#include <qlibraryinfo.h>
1595
static const char *qInstallLocation(QLibraryInfo::LibraryLocation loc)
1597
static QByteArray ret;
1598
ret = QLibraryInfo::location(loc).toLatin1();
1599
return ret.constData();
1601
const char *qInstallPath()
1603
return qInstallLocation(QLibraryInfo::PrefixPath);
1605
const char *qInstallPathDocs()
1607
return qInstallLocation(QLibraryInfo::DocumentationPath);
1609
const char *qInstallPathHeaders()
1611
return qInstallLocation(QLibraryInfo::HeadersPath);
1613
const char *qInstallPathLibs()
1615
return qInstallLocation(QLibraryInfo::LibrariesPath);
1617
const char *qInstallPathBins()
1619
return qInstallLocation(QLibraryInfo::BinariesPath);
1621
const char *qInstallPathPlugins()
1623
return qInstallLocation(QLibraryInfo::PluginsPath);
1625
const char *qInstallPathData()
1627
return qInstallLocation(QLibraryInfo::DataPath);
1629
const char *qInstallPathTranslations()
1631
return qInstallLocation(QLibraryInfo::TranslationsPath);
1633
const char *qInstallPathSysconf()
1635
return qInstallLocation(QLibraryInfo::SettingsPath);