PDA

View Full Version : QWidget and Qt Modules



sandros
21st August 2007, 19:30
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

Michiel
21st August 2007, 19:40
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?

sandros
21st August 2007, 19:53
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

sandros
21st August 2007, 20:15
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

Gopala Krishna
21st August 2007, 20:20
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.

Gopala Krishna
21st August 2007, 20:23
Well nice that you found the forward declaration :)
Anyway forward declaration doesn't mean that library has the symbols for it

jpn
21st August 2007, 21:31
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.

Eldritch
21st August 2007, 23:56
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! :D

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.