1

I am trying to build a shared library with Qt 5.6.0 and i would like to use Pimpl.

I have one class to export and this class inherits from QGraphicsScene, which itself inherits from QObject :

// CustomScene.h
#include "customscene_global.h" // recommended header from Qt
#include <QGraphicsScene> // do not compile if not present 

// forward declarations
class CustomSceneImpl;
template <typename T> class QList;
class QString;
template <class Key, class T> class QMap;
// some other classes ...

class CustomScene : public QGraphicsScene
{
  Q_OBJECT

  public :
  // some public functions

  signals:
  // some custom signals

  public slots:
  // some custom public slots

  private:
    CustomSceneImpl *m_pimpl; // pointer to private members

};

Everything's is fine if i #include <QGraphicsScene> in this header but i would prefer to forward declare QGraphicsScene. However i get the following compile error if i do that :

error: invalid use of incomplete type 'struct QGraphicsScene'

I guess i am doing something wrong with the QObject Macro, that is not recognized, and with the moc in general.

I know there are dedicated Qt Macro to use Pimpl ( Q_D, .. ) but i did not really understand how to use them and if it would fix this issue.

Thanks for your help.

Fryz
  • 1,544
  • 17
  • 29
  • Is there anything in your question that is not covered by [this one](http://stackoverflow.com/q/25250171/1329652)? I ask since I'd like the other one to cover all angles, so if there was something unclear please let me know and I'll fix it. – Kuba hasn't forgotten Monica Jun 17 '16 at 16:58
  • Actually, my problem was related to inheritance, and not to the Qt way to implementation pimpl but your explanation looks very fine and i'll have a closer look at it and let you know if something's not clear for me. Thanks. – Fryz Jun 20 '16 at 09:56

3 Answers3

4

Everything's is fine if i #include QGraphicsScene in this header but i would prefer to forward declare QGraphicsScene.

you cannot do it as you inherit from QGraphicsScene:

class CustomScene : public QGraphicsScene

btw. this error has nothing to do with the way you implemented PIMPL idiom (actually it looks fine), inheritance from incomplete type is simply not possible as compiler needs to know the size / layout of your base class.

leemes
  • 42,229
  • 18
  • 115
  • 172
marcinj
  • 44,446
  • 9
  • 70
  • 91
2

Since a forward declared class has no information about the content, you can't use it for inheritance.

JefGli
  • 731
  • 3
  • 15
2

You can use PIMPL idiom for composition, not for inheritance. If you need to inherit from QGraphicsScene, you cannot forward declare it. You have to include its full definition.

el.pescado
  • 17,764
  • 2
  • 43
  • 82