Results 1 to 9 of 9

Thread: Doubt regarding 'Find Files' example - No forward declaration of class?

  1. #1
    Join Date
    Jul 2010
    Posts
    34
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Doubt regarding 'Find Files' example - No forward declaration of class?

    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
    Qt Code:
    1. #ifndef WINDOW_H
    2. #define WINDOW_H
    3.  
    4. #include <QDialog>
    5.  
    6. class QComboBox;
    7. class QDir;
    8. class QLabel;
    9. class QPushButton;
    10. class QTableWidget;
    11.  
    12. class Window : public QDialog
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. Window(QWidget *parent = 0);
    18.  
    19. private slots:
    20. void browse();
    21. void find();
    22.  
    23. private:
    24. QStringList findFiles(const QDir &directory, const QStringList &files,
    25. const QString &text);
    26. void showFiles(const QDir &directory, const QStringList &files);
    27. QPushButton *createButton(const QString &text, const char *member);
    28. QComboBox *createComboBox(const QString &text = QString());
    29. void createFilesTable();
    30.  
    31. QComboBox *fileComboBox;
    32. QComboBox *textComboBox;
    33. QComboBox *directoryComboBox;
    34. QLabel *fileLabel;
    35. QLabel *textLabel;
    36. QLabel *directoryLabel;
    37. QLabel *filesFoundLabel;
    38. QPushButton *browseButton;
    39. QPushButton *findButton;
    40. QTableWidget *filesTable;
    41. };
    42.  
    43. #endif
    To copy to clipboard, switch view to plain text mode 

    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,

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Doubt regarding 'Find Files' example - No forward declaration of class?

    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.

  3. The following user says thank you to Lykurg for this useful post:

    el33t (5th August 2010)

  4. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Doubt regarding 'Find Files' example - No forward declaration of class?

    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

  5. #4
    Join Date
    Jul 2010
    Posts
    34
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Doubt regarding 'Find Files' example - No forward declaration of class?

    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?

  6. #5
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Doubt regarding 'Find Files' example - No forward declaration of class?

    The version of window.h in the 4.6 docs (here) looks like this:
    Qt Code:
    1. #ifndef WINDOW_H
    2. #define WINDOW_H
    3.  
    4. #include <QDialog>
    5. #include <QDir>
    6.  
    7. class QComboBox;
    8. . . .
    To copy to clipboard, switch view to plain text mode 
    And in qdir.h you have:
    Qt Code:
    1. #ifndef QDIR_H
    2. #define QDIR_H
    3.  
    4. #include <QtCore/qstring.h>
    5. #include <QtCore/qfileinfo.h>
    6. #include <QtCore/qstringlist.h>
    To copy to clipboard, switch view to plain text mode 
    That example won't compile on my machine with QDir forward declared.

  7. The following user says thank you to norobro for this useful post:

    el33t (5th August 2010)

  8. #6
    Join Date
    Jul 2010
    Posts
    34
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Doubt regarding 'Find Files' example - No forward declaration of class?

    @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. 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.

  9. #7
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Doubt regarding 'Find Files' example - No forward declaration of class?

    The Google C++ Style Guide (link) 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.

  10. #8
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Doubt regarding 'Find Files' example - No forward declaration of class?

    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.

  11. #9
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Doubt regarding 'Find Files' example - No forward declaration of class?

    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.

Similar Threads

  1. Forward Declaration of struct QCloseEvent ????
    By drake1983 in forum Newbie
    Replies: 2
    Last Post: 3rd February 2009, 15:16
  2. problem with forward declaration
    By MarkoSan in forum General Programming
    Replies: 14
    Last Post: 6th January 2008, 21:45
  3. Forward Class declaration ERROR
    By nleverin in forum Qt Programming
    Replies: 1
    Last Post: 30th July 2007, 08:35
  4. Forward declaration with std namespace
    By Raistlin in forum General Programming
    Replies: 2
    Last Post: 5th March 2007, 20:45
  5. Why forward declaration ?
    By probine in forum General Programming
    Replies: 3
    Last Post: 26th January 2007, 18:22

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.