PDA

View Full Version : Doubt regarding 'Find Files' example - No forward declaration of class?



el33t
4th August 2010, 20:00
Hi people,

I'm a beginner in C++ and recently started learning Qt. I was just going through the QT 'Find Files' example when I got this doubt. It is regarding some elements of the 'window.h' header file which is part of the 'Find Files' example.

First let me post the source of the window.h file. Then I'll ask my doubt.

window.h

#ifndef WINDOW_H
#define WINDOW_H

#include <QDialog>

class QComboBox;
class QDir;
class QLabel;
class QPushButton;
class QTableWidget;

class Window : public QDialog
{
Q_OBJECT

public:
Window(QWidget *parent = 0);

private slots:
void browse();
void find();

private:
QStringList findFiles(const QDir &directory, const QStringList &files,
const QString &text);
void showFiles(const QDir &directory, const QStringList &files);
QPushButton *createButton(const QString &text, const char *member);
QComboBox *createComboBox(const QString &text = QString());
void createFilesTable();

QComboBox *fileComboBox;
QComboBox *textComboBox;
QComboBox *directoryComboBox;
QLabel *fileLabel;
QLabel *textLabel;
QLabel *directoryLabel;
QLabel *filesFoundLabel;
QPushButton *browseButton;
QPushButton *findButton;
QTableWidget *filesTable;
};

#endif

Doubt Questions:

As you can see above, all the classes of objects that have been used in the header file have been forward declared (like QLabel, QPushbutton, QDir etc. etc.). But in line no. 24, an object of class QStringlist has been declared without any forward declaration of the class QStringlist. A similar case can be found in line no. 17 where the function takes a argument of the type QWidget.

Can anyone explain why these two classes were not forward declared?

Regards,

Lykurg
4th August 2010, 20:09
Ok, the widget thing is easy. QDialog depends on QWidget, so inside qdialog.h qwidget.h is included, thus you can use QWidget. QStringList puzzles me too.

Lykurg
4th August 2010, 20:14
ok, in qwidget.h they include qfont.h. And in qfont.h they have included qstring.h. And somewhere in there they surely have included qlist.h

el33t
5th August 2010, 01:21
Ok, thanks for the reply. I got the QWidget part ( According to Qt Assistant, QDialog inherits QWidget, so the latter is automatically available when QDialog is included)

**Damn! Why didn't I check Qt Assistant before asking?? Sorry about that.**

But I'm still confused about the QStringlist part. You said that QWidget inherits QFont( which further inherits QString and QStringlist), but nothing such is mentioned in the reference document of QWidget. In fact, I checked the reference document of QStringlist and to my surprise, this class is not inherited by any other class.

So what now mate?

norobro
5th August 2010, 03:09
The version of window.h in the 4.6 docs (here (http://doc.trolltech.com/4.6/dialogs-findfiles-window-h.html)) looks like this:
#ifndef WINDOW_H
#define WINDOW_H

#include <QDialog>
#include <QDir>

class QComboBox;
. . .

And in qdir.h you have:
#ifndef QDIR_H
#define QDIR_H

#include <QtCore/qstring.h>
#include <QtCore/qfileinfo.h>
#include <QtCore/qstringlist.h>
That example won't compile on my machine with QDir forward declared.

el33t
5th August 2010, 03:52
@norobro : Thanks for the reply. Problem solved. So, it seems the class QDir has QStringlist 'included' in it rather than inheriting it.

Also, the window.h in your post belongs to version 4.6. Mine is of version 4.2. You can find it here (http://doc.trolltech.com/4.2/dialogs-findfiles-window-h.html). But I guess the 4.2 version will compile fine because if you notice, no new object of class QDir is being declared in the version I quoted. It will give error only if any non-pointer declaration takes place in the code(as in the version you quoted). But I'm not sure about this.

norobro
5th August 2010, 04:23
The Google C++ Style Guide (link (http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml?showone=Header_File_Dependencies#Head er_File_Dependencies)) has some good information.

Here's what it says about forward declarations:
How can we use a class Foo in a header file without access to its definition?

We can declare data members of type Foo* or Foo&.
We can declare (but not define) functions with arguments, and/or return values, of type Foo. (One exception is if an argument Foo or const Foo& has a non-explicit, one-argument constructor, in which case we need the full definition to support automatic type conversion.)
We can declare static data members of type Foo. This is because static data members are defined outside the class definition.

On the other hand, you must include the header file for Foo if your class subclasses Foo or has a data member of type Foo.

tbscope
5th August 2010, 05:51
One golden tip I can give is:
Don't rely on include files including other files.
Always try to explicitly include the header or forward declaration of any class you use.

In that respect, and my personal opinion, the example isn't 100% correct.

Lykurg
5th August 2010, 05:51
You surely find all header files on your disk, where you have installed Qt. That should be the fastest and easiest why to find the files.