1

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?

IAmInPLS
  • 4,051
  • 4
  • 24
  • 57
Aleph0
  • 5,816
  • 4
  • 29
  • 80

2 Answers2

1

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);
Aleph0
  • 5,816
  • 4
  • 29
  • 80
IAmInPLS
  • 4,051
  • 4
  • 24
  • 57
  • Thanks for the fast reply. I already tried to hide the central widget. But then you are no longer able to drag the dock widgets around. Also setting the maximum size very small like (0,0) disables the feature of dragging around the dock widgets. – Aleph0 Jun 03 '16 at 10:03
  • What do you mean you are no longer able to drag the dock widgets around? Of course you can – IAmInPLS Jun 03 '16 at 10:04
  • If I change to `auto label = new QLabel("Central Widget"); label->setMaximumSize(0, 0);` I can still drag the dock widgets. That's correct. Now try to align all widgets from left to right. After that you are unable to align then from top to bottom again. – Aleph0 Jun 03 '16 at 10:09
  • Oh, I see. Add this line : `window->setDockNestingEnabled(true);` – IAmInPLS Jun 03 '16 at 10:30
  • Cool! The usage is now a little bit different. I just hid the central widget. `label->hide(); ` After that I can freely arrange my plots as I liked and the nesting seems to be also a nice to have. Many thanks again. – Aleph0 Jun 03 '16 at 10:35
0
centralWidget()->hide(); // enable full dock space

works for me w/ qt 5.8

roberto
  • 577
  • 6
  • 5