How to capture the QDockWidget close button click event

Create a new CloseDockWidget based on DockWidget.

Override the closeEvent() method, but emit an additional closed() signal from there.

widgets/qclosedockwidget.cpp:

#include "qclosedockwidget.h"

namespace Widgets
{
    QCloseDockWidget::QCloseDockWidget(const QString &title, QWidget *parent) 
    : QDockWidget(title, parent)
    {
        // constructor
    }

    void QCloseDockWidget::closeEvent(QCloseEvent *event)
    {
        emit closed(); // <------ signal

        QDockWidget::closeEvent(event);
    }
} // namespace Widgets

widgets/qclosedockwidget.h:

#ifndef QCLOSEDOCKWIDGET_H
#define QCLOSEDOCKWIDGET_H

#include <QDockWidget>

namespace Widgets
{

    class QCloseDockWidget : public QDockWidget
    {
        Q_OBJECT

    public:
        QCloseDockWidget(const QString &title = "", QWidget *parent = nullptr);

    protected:
        void closeEvent(QCloseEvent *event);
    signals:
        void closed();
    };

} // namespace Widgets

#endif // QCLOSEDOCKWIDGET_H

Now you are able to instantiate and connect to the new signal:

auto *dockWidget = new Widgets::QCloseDockWidget("MyDockWidget", this);

connect(dockWidget, &Widgets::QCloseDockWidget::closed, this, &MainWindow::dockWidgetCloseClicked);

Turns out that everything apart from the visibilityChanged signal works!

I added a signal to the overridden closeEvent() method which I could then connect to any slot I wanted.

The actual issue was that within the stacked widget I had another QDockWidget on another page, hence I was adding all of these things to the wrong QDockWidget! (And of course promoted the wrong QDockWidget too doh!).

Hopefully this question can serve as a reference to anyone else that needs to figure out how to do this - rather than why it isn't working.