c++qtqmainwindowqdockwidgetqmdiarea

QDockWidget in QMdiArea subwindow not dockable on top and bottom


In my current project, I implemented a Qt application with several MDI forms that are containing several QDockWidgets.

The application looks roughly like that:

Blueprint of my application

Here is the code snippet:

#include <QApplication>
#include <QMainWindow>
#include <QMdiArea>
#include <QDockWidget>
#include <QLabel>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    auto window = new QMainWindow;
    auto mdi = new QMdiArea;
    window->setCentralWidget(mdi);

    auto subWindowWidget = new QMainWindow;
    mdi->addSubWindow(subWindowWidget);

    auto dock1 = new QDockWidget("Dock1");
    dock1->setWidget(new QLabel("Label1"));
    dock1->setAllowedAreas(Qt::DockWidgetArea::AllDockWidgetAreas);
    subWindowWidget->addDockWidget(Qt::DockWidgetArea::BottomDockWidgetArea, dock1);

    auto dock2 = new QDockWidget("Dock2");
    dock2->setWidget(new QLabel("Label2"));
    dock2->setAllowedAreas(Qt::DockWidgetArea::AllDockWidgetAreas);
    subWindowWidget->addDockWidget(Qt::DockWidgetArea::BottomDockWidgetArea, dock2);

    window->show();
    return app.exec();
}

I'm really satisfied how the program behaves and what it does. But there is one drawback : even though I'm able to dock the dock widgets to the left and the right, I'm not able to dock them to the top and the bottom.

Hopefully, there is somebody here that can help me out. I really need this feature.


Solution

  • You didn't set a central widget to your second QMainWindow. To achieve the behavior you want, try to set your first QDockWidget as the central widget.

    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
    
        auto window = new QMainWindow;
        auto mdi = new QMdiArea;
        window->setCentralWidget(mdi);
    
        auto subWindowWidget = new QMainWindow;
        mdi->addSubWindow(subWindowWidget);
    
        auto dock1 = new QDockWidget("Dock1");
        dock1->setWidget(new QLabel("Label1"));
        dock1->setAllowedAreas(Qt::AllDockWidgetAreas);
        subWindowWidget->setCentralWidget(dock1);
    
        auto dock2 = new QDockWidget("Dock2");
        dock2->setWidget(new QLabel("Label2"));
        dock2->setAllowedAreas(Qt::AllDockWidgetAreas);
        subWindowWidget->addDockWidget(Qt::BottomDockWidgetArea, dock2);
    
        window->show();
        return app.exec();
    }