I'm using QtCreator and execute a sample code named "Calendar"
I'm new to Qt and C++.
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QDate>
#include <QMainWindow>
QT_BEGIN_NAMESPACE
class QTextBrowser;
QT_END_NAMESPACE
Why does this example use class QTextBrowser; prototype statement, instead of #include ?
I executed this code by #include and delete this macro function. This had no problem.
This is from source code
\macro QT_BEGIN_NAMESPACE \internal This macro expands to \snippet code/src_corelib_global_qglobal.cpp begin namespace macro if \c QT_NAMESPACE is defined and nothing otherwise. If should always appear in the file-level scope and be followed by \c QT_END_NAMESPACE at the same logical level with respect to preprocessor conditionals in the same file. As a rule of thumb, \c QT_BEGIN_NAMESPACE should appear in all Qt header and Qt source files after the last \c{#include} line and before the first declaration. If that rule can't be followed because, e.g., \c{#include} lines and declarations are wildly mixed, place \c QT_BEGIN_NAMESPACE before the first declaration and wrap the \c{#include} lines in \c QT_BEGIN_INCLUDE_NAMESPACE and \c QT_END_INCLUDE_NAMESPACE. When using the \c QT_NAMESPACE feature in user code (e.g., when building plugins statically linked to Qt) where the user code is not intended to go into the \c QT_NAMESPACE namespace, all forward declarations of Qt classes need to be wrapped in \c QT_BEGIN_NAMESPACE and \c QT_END_NAMESPACE. After that, a \c QT_USE_NAMESPACE should follow. No further changes should be needed.
and,
\macro QT_NAMESPACE \internal If this macro is defined to \c ns all Qt classes are put in a namespace called \c ns. Also, moc will output code putting metaobjects etc. into namespace \c ns. \sa QT_BEGIN_NAMESPACE, QT_END_NAMESPACE, QT_PREPEND_NAMESPACE, QT_USE_NAMESPACE, QT_BEGIN_INCLUDE_NAMESPACE, QT_END_INCLUDE_NAMESPACE, QT_BEGIN_MOC_NAMESPACE, QT_END_MOC_NAMESPACE,
I think this comments state that QT_BEGIN_NAMESPACE
is executed if QT_NAMESPACE
is defined.
As far as this sample code, QT_NAMESPACE
is not defined.
And I understand this macro put all Qt classes into a namespace.
But in this sample code, only class QTextBrowser
is packed by the two macros.
I tried to do this by force,
namespace QT_NAMESPACE {
class QTextBrowser;
}
because these are the texts of two macros.
I came to need to write QT_NAMESPACE::QTextBrowser *editor;
in class definition.
This can delete newly occured warning in header file.
But other warnings occur in source file.
`editor = new QT_NAMESPACE::QTextBrowser;`
Allocation of imcomplete type QT_NAMESPACE::QTextBrowser
I want to ask,
why only this class is packed by QT_BEGIN_NAMESPACE and QT_END_NAMESPACE? Are all Qt classes not targeted?
If these macros are executed without problems, new errors and warnings occur in the end. What did the editor think and write this example code?