Call the mdiSubWindow destroyed slot from inside its class

  • I have an override QMdiSubWindow class as follow:

    // Declaration
    class PSubWindow : public QMdiSubWindow {
        explicit PSubWindow(QMdiArea *parent = 0);
        QMdiArea *parent;
        void closeEvent(QCloseEvent *event);
    protected slots:
        void on_subWindow_destroyed(QObject *window);
    // Definition (Constructor)
    PSubWindow::PSubWindow(QMdiArea *parent) : QMdiSubWindow(parent){
        this->parent = parent;
        static ushort counter = 1;
        this->setObjectName("subWindow" + QString::number(counter));
        this->setWindowTitle("Untitled " + QString::number(counter++));
        // this is the target connection.
        QObject::connect(this, SIGNAL(destroyed(QObject*)), this, SLOT(on_subWindow_destroyed(QObject*)));
    void PSubWindow::on_subWindow_destroyed(QObject *window) {
        qDebug()<< "sub-window destroyed";
    void PSubWindow::closeEvent(QCloseEvent *event) {
        //qDebug()<< "closeEvent";

    And then, I have created an instance of subWindow in another class as follow:

    QMdiSubWindow *PActions::newFile() {
        PSubWindow *newWindow = new PSubWindow(this->mdiareaContainer);
        QMdiSubWindow *subWindow = mdiareaContainer->addSubWindow(newWindow);
        return subWindow;

    The problem is the destroyed connection is not implemented, and it does not report any error.
    is there anything wrong in my code?

  • Lifetime Qt Champion


    What do you mean by not implemented ? Isn't PSubWindow::on_subWindow_destroyed the implementation of that method ?

    On a side note, connecting the destroyed signal from a class to an method of the same class isn't exactly a good idea. You are basically trying to call a method from an object that has been destroyed.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.