PDA

View Full Version : QT_BEGIN_NAMESPACE question



veraS
3rd July 2010, 13:39
Hi,
What is the diference in using ...

QT_BEGIN_NAMESPACE
class QLabel;
class QPushButton;
class QUdpSocket;
QT_END_NAMESPACE

or

#include <QLabel>
#include <QPushButton>
#include <QUdpSocket>

... in my dialogs?
Thanks

SixDegrees
3rd July 2010, 15:48
The first method can be faster; the compiler doesn't need to process the contents of the header files as it does in the second method. This only affects compilation time, and on the systems I've used there is no perceptible difference in compilation speed. On large projects, or where you have a large number of include files, it may make more of a difference.

veraS
3rd July 2010, 16:40
In the broadcastreceiver example i have:


#ifndef RECEIVER_H
#define RECEIVER_H

#include <QDialog>

QT_BEGIN_NAMESPACE
class QLabel;
class QPushButton;
class QUdpSocket;
QT_END_NAMESPACE

class Receiver : public QDialog
{
Q_OBJECT

public:
Receiver(QWidget *parent = 0);

private slots:
void processPendingDatagrams();

private:
QLabel *statusLabel;
QPushButton *quitButton;
QUdpSocket *udpSocket;
};

#endif


Why use #include for the QDialog class and QT_BEGIN_NAMESPACE...QT_END_NAMESPACE for QLabel, QPushButton and QUdpSocket?

SixDegrees
3rd July 2010, 16:44
Because Receiver inherits from QDialog; the compiler needs the actual class definition provided in the header file in order to resolve the internal structure of the class. The other classes are only referenced through pointers, so a simple forward declaration is sufficient for the compiler in those cases.