Results 1 to 8 of 8

Thread: QWidget and Qt Modules

  1. #1

    Default QWidget and Qt Modules

    Hi all,

    I'm implementing a non-GUI library that links only against QtCore. As a design flaw, there is a method that has a QWidget* as a parameter. I'm wondering why that code compiles fine without a #include <QWidget> or a class forward declaration.

    Furthermore, why is this code linking fine by using just QtCore ?
    I noticed that the same qwidget.h file is present in /usr/local/include/qt4/Qt and /usr/local/include/qt4/QtCore. Why this duplicated file ?

    TIA,
    Sandro

  2. #2
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QWidget and Qt Modules

    Quote Originally Posted by sandros View Post
    I'm implementing a non-GUI library that links only against QtCore. As a design flaw, there is a method that has a QWidget* as a parameter. I'm wondering why that code compiles fine without a #include <QWidget> or a class forward declaration.
    Maybe you include something {that includes something}* that includes QWidget?
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  3. #3

    Default Re: QWidget and Qt Modules

    Hmm, I checked this !

    My code is something like:

    #include <QObject>
    #include <icore.h>

    class A
    {
    public:
    virtual void m1(... QWidget *widget ...) = 0;
    };

    ...

    #include <QtCore/QObject>
    class ICore
    {
    ...
    };

    The g++ parameter are:
    -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtCore -I/usr/include/qt4 -I.
    -L/usr/lib -lQtCore -lpthread

    Thanks,
    Sandro

  4. #4

    Default Re: QWidget and Qt Modules

    I got it !

    qobject.h makes a forward declaration of QWidget.
    So, the questions are:

    1) Why qobject.h forward declarates QWidget ?
    2) Why QWidget* symbol is present in libQtCore.so ?

    Sandro

  5. #5
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QWidget and Qt Modules

    Thats because the objects appearing as parameter of a function declaration(not definition) do not require complete object definition of that object. Since pure virtual functions are only declaration you don't need to link to QtGui library at all and only a forward declaration of QWidget will suffice. I don't know where this forward declaration is though, may be somewhere in qobject.h or in one of file included by it.
    So i suspect you haven't implemented that method( which accepts QWidget* parameter) at all.
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  6. #6
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QWidget and Qt Modules

    Well nice that you found the forward declaration
    Anyway forward declaration doesn't mean that library has the symbols for it
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  7. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QWidget and Qt Modules

    QWidget is a very special QObject so actually I'm not that surprised of such forward declaration. QObject even has a method to ask whether it's a widget or not. However, looks like all what qobject.h does with QWidget is declaring it as a friend class (at least in Qt 4.3.0). As far as I know, this wouldn't even require forward declaring but who knows if some compiler supported by Qt does require it.
    J-P Nurmi

  8. #8
    Join Date
    Aug 2006
    Posts
    44
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QWidget and Qt Modules

    The forward declaration allows interfaces and functions to pass references and pointers w/o the entire definition, as pointed out earlier.

    In the case of QtCore, none of the implementation is allowed to call methods in QWidget if no GUI support is enabled, nor can the code ever dereference a QWidget *. As long as that is true, everything works fine -- the compiler won't let you get away with trying to break that rule. Even if you do pull in QWidget, the linker will catch you if you don't link QtGui and once you do that, you've just gotten yourself a UI!

    Anything that takes a QWidget * or QWidget & argument does nothing in the non-GUI portions of the code, and functions that return a QWidget * must return NULL. Obviously, no QWidget & can be returned.

    This is extremely useful when you use inheritance to specialize implementation depending on how you compile your code.

Similar Threads

  1. Replies: 4
    Last Post: 24th April 2007, 13:18
  2. Replies: 3
    Last Post: 8th March 2007, 14:54
  3. Showing QMainWindow without showing a child QWidget
    By discostu in forum Qt Programming
    Replies: 3
    Last Post: 4th March 2007, 09:03
  4. QTextEdit, sizeHint, QWidget
    By TheKedge in forum Qt Programming
    Replies: 1
    Last Post: 3rd February 2007, 08:25
  5. Replies: 1
    Last Post: 2nd May 2006, 21:11

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.