#include <QtGui>
#include <QDebug>

#define REPRO
#define DO_NOT_USE_QWIDGET_AS_PARENT

/* B */

#ifdef DO_NOT_USE_QWIDGET_AS_PARENT
class B: public QObject
#else
class B: public QLabel
#endif
{
    Q_OBJECT
public:
#ifdef DO_NOT_USE_QWIDGET_AS_PARENT
    B(QObject *parent = 0) : QObject(parent) { qDebug() << "Constructing B"; }
#else
    B(QWidget *parent = 0) : QLabel(parent) { qDebug() << "Constructing B"; }
#endif
   ~B() { qDebug() << "Destructor B"; }
};

/* A */

class A: public B
{
    Q_OBJECT
public:
#ifdef DO_NOT_USE_QWIDGET_AS_PARENT
    A(QObject *parent = 0) : B(parent) { qDebug() << "Constructing A"; }
#else
    A(QWidget *parent = 0) : B(parent) { qDebug() << "Constructing A"; }
#endif
    ~A() { qDebug() << "Destructor A"; }

public slots:
    void otherSlot() { qDebug() << "this should be called when B is destroyed"; }
};

/* main */

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QMainWindow w;

    QWidget *mainWidget = new QWidget(w.parentWidget());
    w.setCentralWidget(mainWidget);

    B *myB = new B(mainWidget);
#ifdef REPRO
    A *myA = new A(myB);
#else
    A *myA = new A(mainWidget);
#endif

    QObject::connect(myB, SIGNAL(destroyed()), myA, SLOT(otherSlot()));

    w.show();

    return a.exec();
}

#include "main.moc"
