Aleph0
Aleph0

Reputation: 6084

Central Widget occupies to much space and Dock Widgets to few after resize

I'm right now using QDockWidget to create a dynamic component for arranging some user defined plots. The plots should be changed in their sizes and can be arranged on top of each other.

The following code snippet illustrates what I'm trying to achieve:

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

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    auto window = new QMainWindow;
    window->setCentralWidget(new QLabel("Central Widget"));

    for (int i = 1; i < 9; i++) {
        auto dock = new QDockWidget(QString("Plot %1").arg(i));
        dock->setWidget(new QLabel(QString("Plot %1").arg(i)));
        dock->setAllowedAreas(Qt::AllDockWidgetAreas);
        window->addDockWidget(Qt::BottomDockWidgetArea, dock);
    }

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

The central widget merely serves as a simple placeholder and is just necessary to allow for dragging and rearranging the QDockWidget.

I'm already very satisfied with the achieved behavior besides a single drawback. After resizing window (making it larger), the central widget consumes all of the newly gained space, whereas the DockWidgets still occupy the same space like before.

The behavior is depicted below:

Application before resize

Application after resize

This is kind of annoying for user, as the central widget is just a placeholder. Actually, I just wanted to have the behavior the other way around, i.e. the central widget should keep its size, whereas the DockWidgets should be enlarged.

How can I achieve this?

Upvotes: 1

Views: 881

Answers (2)

roberto
roberto

Reputation: 577

centralWidget()->hide(); // enable full dock space

works for me w/ qt 5.8

Upvotes: 0

IAmInPLS
IAmInPLS

Reputation: 4125

Just resize your central widget to the desired size. Or even better, hide it (it seems that you only use QDockWidgets besides this one).

QLabel* label = new QLabel("Central Widget");
label->hide();
window->setCentralWidget(label);
window->setDockNestingEnabled(true);

Upvotes: 1

Related Questions