2

I'm manually laying out a Qt application in C++ using QMainWindow. I want two side-by-side docked widgets at the bottom of the screen, but I want them to have disproportionate widths. Currently, I can only get them to have the same width. Is there any way to set a stretch factor or other mechanism to get nonuniform dock splits?

Here's how I'm laying them out currently:

OutputPanel* outputPanel = new OutputPanel;
mainWindow.addDockWidget(Qt::BottomDockWidgetArea, outputPanel);

ThumbnailViewer* thumbnailViewer = new ThumbnailViewer;
mainWindow.splitDockWidget(outputPanel, thumbnailViewer, Qt::Horizontal);

Here's an image of what I'd like to achieve: enter image description here

Thanks!

Alex Goldberg
  • 975
  • 11
  • 14

1 Answers1

8

Using QMainWindow::resizeDocks, as you suggested. After setting the resizeDock parameters, the relative proportions are maintained as the main window resizes.

#include "mainwindow.h"

#include <QApplication>
#include <QLabel>
#include <QDockWidget>
#include <QList>

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

    MainWindow w;
    w.setCentralWidget(new QLabel("MAIN WINDOW CONTENTS"));

    QDockWidget* dwa = new QDockWidget("DOCK A");
    dwa->setWidget(new QLabel("DOCK A CONTENTS"));

    QDockWidget* dwb = new QDockWidget("DOCK B");
    dwb->setWidget(new QLabel("DOCK B CONTENTS"));

    w.addDockWidget(Qt::BottomDockWidgetArea, dwa);
    w.addDockWidget(Qt::BottomDockWidgetArea, dwb);

    w.splitDockWidget(dwa, dwb, Qt::Horizontal);

    w.show();

    QList<QDockWidget*> docks = { dwa, dwb };

    float windowWidth = w.size().width();
    int dockWidthA = 0.60 * windowWidth;
    int dockWidthB = 0.40 * windowWidth;

    QList<int> dockSizes = { dockWidthA, dockWidthB };

    w.resizeDocks(docks, dockSizes, Qt::Horizontal);

    return a.exec();
}
vincent
  • 1,370
  • 2
  • 13
  • 29
  • Thanks for the reply! That's an interesting approach - I hadn't considered that. I'm a bit concerned with what would happen if someone undocked one of those two docks (_dockA / _dockB) and redocked elsewhere on the QMainWindow. At minimum, there would be ownership issues. After posting, I stumbled on [QMainWindow::resizeDocks](http://doc.qt.io/qt-5/qmainwindow.html#resizeDocks). Unfortunately, that doesn't work for this use case. Trying to resize my thumbnailViewer in the horizontal direction just resizes it vertically... – Alex Goldberg Feb 24 '17 at 04:49
  • Yea resizeDocks looks more promising. I was able to get that working. I'll update my answer with what I found in case its useful. – vincent Feb 24 '17 at 15:58
  • 1
    Thanks - this is great! resizeDocks didn't work well for me originally - your example made me realize I needed to put my resizeDocks calls after the show() call. Guessing this is because I have side panels involved, and there's auto-sizing concerns here. – Alex Goldberg Feb 27 '17 at 01:11